From f92946e12f9acfa394c04a140a2a4d025a5fe3ca Mon Sep 17 00:00:00 2001 From: lmn Date: Tue, 13 Feb 2018 13:11:57 +0100 Subject: 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 --- pysite/base_route.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'pysite/base_route.py') diff --git a/pysite/base_route.py b/pysite/base_route.py index a3e8615b..c705a350 100644 --- a/pysite/base_route.py +++ b/pysite/base_route.py @@ -1,4 +1,8 @@ # coding=utf-8 +import os +import random +import string + from flask import Blueprint, jsonify, render_template from flask.views import MethodView @@ -27,7 +31,17 @@ class RouteView(BaseView): class APIView(RouteView): + def validate_key(self, api_key: str): + """ Placeholder! """ + return api_key == os.environ("API_KEY") + + def generate_api_key(self): + """ Generate a random string of n characters. """ + pool = random.choices(string.ascii_letters + string.digits, k=32) + return "".join(pool) + def error(self, error_code: ErrorCodes): + data = { "error_code": error_code.value, "error_message": "Unknown error" @@ -40,7 +54,12 @@ class APIView(RouteView): http_code = 404 elif error_code is ErrorCodes.unauthorized: data["error_message"] = "Unauthorized" - http_code = 403 + http_code = 401 + elif error_code is ErrorCodes.invalid_api_key: + data["error_message"] = "Invalid API-key" + http_code = 401 + elif error_code is ErrorCodes.missing_parameters: + data["error_message"] = "Not all required parameters were provided" response = jsonify(data) response.status_code = http_code -- cgit v1.2.3