Add base code for REST API using Flask
This commit is contained in:
parent
0be947b3d1
commit
2da6e43797
12 changed files with 266 additions and 14 deletions
1
tofu_api/api/tasks/__init__.py
Normal file
1
tofu_api/api/tasks/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .task_api import TaskApiBlueprint
|
||||
84
tofu_api/api/tasks/task_api.py
Normal file
84
tofu_api/api/tasks/task_api.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue