flask-demo-api/flask_demo_api/routers/key.py

53 lines
1.5 KiB
Python
Raw Normal View History

2024-04-15 09:32:17 +03:00
from dishka.integrations.flask import FromDishka, inject
2024-04-15 02:16:07 +03:00
from flask import Blueprint, jsonify, request
2024-04-15 09:32:17 +03:00
from protocols.models import KeyDTO
from usecase.add import PostKey
from usecase.get import GetKey
from usecase.put import PutKey
2024-04-15 02:16:07 +03:00
key_bp = Blueprint("key_bp", __name__)
@key_bp.route("/", methods=["POST"])
2024-04-15 09:32:17 +03:00
def past_key(usecase: FromDishka[PostKey]):
2024-04-15 02:16:07 +03:00
json_data = request.get_json()
if json_data:
2024-04-15 09:32:17 +03:00
result = usecase(request=KeyDTO(key=json_data))
2024-04-15 02:16:07 +03:00
return (
2024-04-15 09:32:17 +03:00
jsonify({"message": "Received JSON data successfully", "data": result}),
2024-04-15 02:16:07 +03:00
200,
)
else:
return jsonify({"message": "No JSON data received"}), 400
@key_bp.route("/", methods=["GET"])
2024-04-15 09:32:17 +03:00
@inject
def get_key(usecase: FromDishka[GetKey]):
request_data = request.args.get("key")
2024-04-15 02:16:07 +03:00
2024-04-15 09:32:17 +03:00
if request_data:
result = usecase(request=KeyDTO(key=request_data))
2024-04-15 02:16:07 +03:00
return (
2024-04-15 09:32:17 +03:00
jsonify({"message": "Received JSON data successfully", "data": result}),
2024-04-15 02:16:07 +03:00
200,
)
2024-04-15 09:32:17 +03:00
2024-04-15 02:16:07 +03:00
else:
2024-04-15 09:32:17 +03:00
return jsonify({"message": "No GET parameters received"}), 400
2024-04-15 02:16:07 +03:00
@key_bp.route("/", methods=["PUT"])
2024-04-15 09:32:17 +03:00
def put_key(usecase: FromDishka[PutKey]):
2024-04-15 02:16:07 +03:00
json_data = request.get_json()
if json_data:
2024-04-15 09:32:17 +03:00
result = usecase(request=KeyDTO(key=json_data))
2024-04-15 02:16:07 +03:00
return (
2024-04-15 09:32:17 +03:00
jsonify({"message": "Received JSON data successfully", "data": result}),
2024-04-15 02:16:07 +03:00
200,
)
else:
return jsonify({"message": "No JSON data received"}), 400