diff options
| author | 2018-02-13 13:11:57 +0100 | |
|---|---|---|
| committer | 2018-02-13 12:11:57 +0000 | |
| commit | f92946e12f9acfa394c04a140a2a4d025a5fe3ca (patch) | |
| tree | a74a0874c842952899f3d10ff2df8397fb89560d /pysite/views/api | |
| parent | Websocket echo test (diff) | |
RethinkDB API Views #yqhg
* Refactoring the database implementation into a class of its own.
* Refactoring the database implementation into a class of its own.
* healthcheck should belong to the API.
* dynamic subdomain loading, setting up basic handling for staff.pythondiscord.com, and started on a TagView for a bot tag feature.
* Oops, forgot to fix some merges.
* Some quality of life updates - default values for env variables that aren't secret, and starting to get through the tag view.
* Refactoring the database implementation into a class of its own.
* healthcheck should belong to the API.
* dynamic subdomain loading, setting up basic handling for staff.pythondiscord.com, and started on a TagView for a bot tag feature.
* Oops, forgot to fix some merges.
* Some quality of life updates - default values for env variables that aren't secret, and starting to get through the tag view.
* API validation added to the APIView class, TagView should be finished as well.
* super important commit you guys
* fixed a bug with the RethinkDB class where host and port attributes were accessed before being created
* Fixed my editor now you guys
* Fixing up some of the problems brought up in gdude's review
* Handling GET requests with param indata and POST with JSON. Fixed error handling to use the constants and the baseclass self.error().
* Get API-key from headers, context manage the db assignment, and default env var values
* Changed API-KEY header to X_API_Key. Added a default for secret key. it should no longer be necessary with environment variables to run this system locally.
* Changing back the nav to have relative paths
* Why am I like this
Diffstat (limited to 'pysite/views/api')
| -rw-r--r-- | pysite/views/api/bot/__init__.py | 0 | ||||
| -rw-r--r-- | pysite/views/api/bot/tag.py | 66 | ||||
| -rw-r--r-- | pysite/views/api/healthcheck.py | 4 | 
3 files changed, 68 insertions, 2 deletions
| diff --git a/pysite/views/api/bot/__init__.py b/pysite/views/api/bot/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/pysite/views/api/bot/__init__.py diff --git a/pysite/views/api/bot/tag.py b/pysite/views/api/bot/tag.py new file mode 100644 index 00000000..acbdbc0c --- /dev/null +++ b/pysite/views/api/bot/tag.py @@ -0,0 +1,66 @@ +# coding=utf-8 + +from flask import g, jsonify, request + +import rethinkdb + +from pysite.base_route import APIView +from pysite.constants import ErrorCodes + + +class TagView(APIView): +    path = '/tag' +    name = 'tag' +    table = 'tag' + +    def __init__(self): +        # make sure the table exists +        with g.db.get_connection() as conn: +            try: +                rethinkdb.db(g.db.database).table_create(self.table, {'primary_key': 'tag_name'}).run(conn) +            except rethinkdb.RqlRuntimeError: +                print(f'Table {self.table} exists') + +    def get(self): +        """ +        Indata must be provided as params, +        API key must be provided as header +        """ +        rdb = rethinkdb.table(self.table) +        api_key = request.headers.get('X-API-Key') +        tag_name = request.args.get('tag_name') + +        if self.validate_key(api_key): +            if tag_name: +                data = rdb.get(tag_name).run(g.db.conn) +                data = dict(data) if data else {} +            else: +                data = rdb.pluck('tag_name').run(g.db.conn) +                data = list(data) if data else [] +        else: +            self.error(ErrorCodes.invalid_api_key) + +        return jsonify(data) + +    def post(self): +        """ Indata must be provided as JSON. """ +        rdb = rethinkdb.table(self.table) +        indata = request.get_json() +        tag_name = indata.get('tag_name') +        tag_content = indata.get('tag_content') +        tag_category = indata.get('tag_category') +        api_key = indata.get('api_key') + +        if self.validate_key(api_key): +            if tag_name and tag_content: +                rdb.insert({ +                    'tag_name': tag_name, +                    'tag_content': tag_content, +                    'tag_category': tag_category +                }).run(g.db.conn) +            else: +                self.error(ErrorCodes.missing_parameters) +        else: +            self.error(ErrorCodes.invalid_api_key) + +        return jsonify({'success': True}) diff --git a/pysite/views/api/healthcheck.py b/pysite/views/api/healthcheck.py index 9d1f681a..2ff5dfb0 100644 --- a/pysite/views/api/healthcheck.py +++ b/pysite/views/api/healthcheck.py @@ -1,10 +1,10 @@  # coding=utf-8  from flask import jsonify -from pysite.base_route import RouteView +from pysite.base_route import APIView -class IndexView(RouteView): +class HealthCheckView(APIView):      path = "/healthcheck"      name = "healthcheck" | 
