flask-webhook/flask_demo_api/routers/webhook.py

29 lines
872 B
Python

from dishka.integrations.flask import FromDishka, inject
from flask import Blueprint, abort, jsonify, request
from flask_demo_api.protocols.models import PeopleRequest
from flask_demo_api.usecase.get_gender import GetGender
wh_bp = Blueprint("gender", __name__)
@wh_bp.route("/webhook", methods=["POST"])
@inject
def gender_request_webhook(usecase: FromDishka[GetGender]):
json_data = request.get_json()
if not json_data.get("id"):
abort(400, "'id' required")
elif not json_data.get("name"):
abort(400, "'name' required")
if json_data:
result = usecase(request=PeopleRequest(id=json_data.get("id"), name=json_data.get("name")))
if result is None:
abort(404, "Gender unknown")
return jsonify({"id": result.id, "gender": result.gender.value}), 200
else:
abort(400, "Invalid json data")