Add base code for REST API using Flask

This commit is contained in:
Lexi / Zoe 2022-03-26 01:07:47 +01:00
parent 0be947b3d1
commit 2da6e43797
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
12 changed files with 266 additions and 14 deletions

1
tofu_api/api/__init__.py Normal file
View file

@ -0,0 +1 @@
from .rest_api import TofuApiBlueprint

14
tofu_api/api/rest_api.py Normal file
View file

@ -0,0 +1,14 @@
from flask import Blueprint
from .tasks import TaskApiBlueprint
class TofuApiBlueprint(Blueprint):
"""
Main blueprint for the Tofu REST API.
"""
def __init__(self):
super().__init__('rest_api', __name__, url_prefix='/api')
self.register_blueprint(TaskApiBlueprint())

View file

@ -0,0 +1 @@
from .task_api import TaskApiBlueprint

View file

@ -0,0 +1,84 @@
from flask import Blueprint, jsonify
from flask.views import MethodView
class TaskApiBlueprint(Blueprint):
"""
Blueprint for the tasks REST API.
"""
def __init__(self):
super().__init__('rest_api_tasks', __name__, url_prefix='/tasks')
self.add_url_rule(
'/',
view_func=TaskCollectionView.as_view(TaskCollectionView.__name__),
methods=['GET', 'POST'],
)
self.add_url_rule(
'/<int:task_id>',
view_func=TaskItemView.as_view(TaskItemView.__name__),
methods=['GET', 'PATCH', 'DELETE'],
)
class TaskCollectionView(MethodView):
"""
View class for `/tasks` endpoint.
"""
def get(self):
"""
Get list of all tasks.
"""
# TODO: Get actual data
return jsonify({
'count': 1,
'items': [
{
'id': 1,
'title': 'Do stuff',
'description': '',
'status': 'open',
},
],
}), 200
def post(self):
"""
Create a new task.
"""
# TODO: Implement
raise NotImplementedError
class TaskItemView(MethodView):
"""
View class for `/tasks/<int:task_id>` endpoint.
"""
def get(self, task_id: int):
"""
Get a single task by ID.
"""
# TODO: Get actual data
return jsonify({
'id': task_id,
'title': 'Do stuff',
'description': '',
'status': 'open',
}), 200
def patch(self, task_id: int):
"""
Update a single task by ID.
"""
# TODO: Implement
raise NotImplementedError
def delete(self, task_id: int):
"""
Delete a single task by ID.
"""
# TODO: Implement
raise NotImplementedError