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

53 lines
1.5 KiB
Python

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