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/__init__.py
Normal file
1
tofu_api/api/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .rest_api import TofuApiBlueprint
|
||||
14
tofu_api/api/rest_api.py
Normal file
14
tofu_api/api/rest_api.py
Normal 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())
|
||||
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
|
||||
|
|
@ -1,10 +1,32 @@
|
|||
def app(environ, start_response):
|
||||
"""Simplest possible application object"""
|
||||
data = b'Hello, wooorld!\n'
|
||||
status = '200 OK'
|
||||
response_headers = [
|
||||
('Content-type', 'text/plain'),
|
||||
('Content-Length', str(len(data)))
|
||||
]
|
||||
start_response(status, response_headers)
|
||||
return iter([data])
|
||||
import os
|
||||
|
||||
import yaml
|
||||
from flask import Flask
|
||||
|
||||
from tofu_api.api import TofuApiBlueprint
|
||||
from tofu_api.config import DefaultConfig
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
"""
|
||||
App factory, returns a Flask app object.
|
||||
"""
|
||||
# Set instance path to the project root directory
|
||||
project_root_dir = os.path.abspath('.')
|
||||
|
||||
# Create and configure the app
|
||||
app = Flask(
|
||||
'tofu_api',
|
||||
instance_path=project_root_dir,
|
||||
instance_relative_config=True,
|
||||
)
|
||||
app.config.from_object(DefaultConfig)
|
||||
|
||||
# Load app configuration from YAML file
|
||||
app.config.from_file(os.getenv('FLASK_CONFIG_FILE', default='config.yml'), load=yaml.safe_load)
|
||||
|
||||
# Register blueprints
|
||||
app.register_blueprint(TofuApiBlueprint())
|
||||
|
||||
# Return WSGI app
|
||||
return app
|
||||
|
|
|
|||
5
tofu_api/config.py
Normal file
5
tofu_api/config.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class DefaultConfig:
|
||||
"""
|
||||
This class defined default values for the app configuration.
|
||||
"""
|
||||
# TODO
|
||||
Loading…
Add table
Add a link
Reference in a new issue