The ENTS backend API is built using the Flask factory app pattern. All modules revolve around the running Flask context.
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.
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
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)
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
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.
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 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
.
Files are to be linted using ruff.
After installing ruff run below to lint
ruff check ./backend
Files are to be formatted using black.
After installing ruff run below to format
black ./backend
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.
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