Skip to content

Latest commit

 

History

History
87 lines (46 loc) · 4.09 KB

README.md

File metadata and controls

87 lines (46 loc) · 4.09 KB

ENTS Backend API

Introduction

The ENTS backend API is built using the Flask factory app pattern. All modules revolve around the running Flask context.

API Reference

The ENTS API is organized around REST. The API uses predictable resource-oriented URLs, accepts form-encoded request bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs.

The ENTS backend API doesn’t support bulk updates. You can work on only one object per request. I.e., one data point can be updated per request.

Authentication

The ENTS API handles users authentication using a refresh token flow. Users are given an access token to the API and a refresh token to designate access time. Currently, still under construction

For external devices ENTS backend plans utilize API Keys to authenticate requests

The authentication module is located under auth

Protecting Endpoints

To add user authentication to endpoints, add an authentication decorator like so (based on Flask-RESTful syntax)

Example:

from auth import authenticate

class User_Data(Resource):
    method_decorators = [‘get’: authenticate]

    def get(self, user):
        user = User.get_user(user.id)
        return user_schema.dump(user)

Resources

ENTS API utilizes flaskRESTful to abstract construction of a REST Api. Endpoints are imported into the app when the app is created and are stored under the resources folder

Resource authentication is handled by utilzing a resource decorator to control access to an endpoint

Validation

For validatiaon, ENTS API utilizes marshmallow to check if the request is formmated correctly and with the correct types. Schemas are created under the schemas folder and imported into various resources as needed.

Async Workers

To handle long running tasks, ENTS API uses Celery as a task queue and Redis as a message broker. A Celery worker configuration is handled under backend/__init__.py and is built under a seperate flag in the dockerfile named, prodworker and devworker.

Testing

Testing is conducted using pytest and testing fixtures are spun up within the factory app pattern. Flask uses the testing configuration as defined under api/config.py. The testing fixtures are defined under tests/conftest.py.

Linting

Files are to be linted using ruff.

After installing ruff run below to lint

ruff check ./backend

Formatting

Files are to be formatted using black.

After installing ruff run below to format

black ./backend

Docker Builds

There are two targets for building the api, development and production. In the development target, hot reload is avaliable as well as the running flask in developement mode (Debug logs print to stderr). In the production target, gunicorn is ran in front of flask and the env is set to production env vars.

Production

To support the demands of deployment, ENTS API utilizes gunicorn as a WSGI HTTP server. The configuration is located at gunicorn.conf.py. Gunicorn workers at set to scale according to CPU_COUNT * 2 + 1 as per gunicorn docs. Monkey patching is also done for performace, which is patched before gunicorn. Note: additional librarys need monkey patching support or you may encounter unintended errors