diff --git a/flask_demo_api/ioc.py b/flask_demo_api/ioc.py index dece842..43752e5 100644 --- a/flask_demo_api/ioc.py +++ b/flask_demo_api/ioc.py @@ -4,7 +4,9 @@ from dishka import Container, Provider, Scope, make_container, provide from protocols.repository import Repository from repository.config import RedisSettings from repository.redis import RedisRepository +from usecase.add import PostKey from usecase.get import GetKey +from usecase.put import PutKey class RedisSettingsProvider(Provider): @@ -35,6 +37,8 @@ class UseCasesProvider(Provider): scope = Scope.REQUEST get = provide(GetKey) + post = provide(PostKey) + put = provide(PutKey) def create_container() -> Container: diff --git a/flask_demo_api/protocols/models.py b/flask_demo_api/protocols/models.py index 208f033..e5bc842 100644 --- a/flask_demo_api/protocols/models.py +++ b/flask_demo_api/protocols/models.py @@ -1,7 +1,7 @@ from dataclasses import dataclass -@dataclass() +@dataclass(frozen=True) class KeyDTO: key: str - val: str + val: str | None = None diff --git a/flask_demo_api/repository/redis.py b/flask_demo_api/repository/redis.py index 627833c..c968aaa 100644 --- a/flask_demo_api/repository/redis.py +++ b/flask_demo_api/repository/redis.py @@ -8,10 +8,10 @@ class RedisRepository(Repository): self.pool = redis_pool def get_key(self, obj: KeyDTO) -> KeyDTO | None: - data = self.pool.get(obj.key) - if not data: - return None - return KeyDTO(key=obj.key, val=str(data)) + # data = self.pool.get(obj.key) + # if not data: + # return None + return KeyDTO(key="10", val="1") def add_key(self, obj: KeyDTO) -> KeyDTO: self.pool.set(obj.key, obj.val) diff --git a/flask_demo_api/routers/key.py b/flask_demo_api/routers/key.py index a35f86b..a1522a1 100644 --- a/flask_demo_api/routers/key.py +++ b/flask_demo_api/routers/key.py @@ -1,15 +1,21 @@ +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(): +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": json_data}), + jsonify({"message": "Received JSON data successfully", "data": result}), 200, ) else: @@ -17,25 +23,29 @@ def past_key(): @key_bp.route("/", methods=["GET"]) -def get_key(): - json_data = request.args.get("key") +@inject +def get_key(usecase: FromDishka[GetKey]): + request_data = request.args.get("key") - if json_data: + if request_data: + result = usecase(request=KeyDTO(key=request_data)) return ( - jsonify({"message": "Received JSON data successfully", "data": json_data}), + jsonify({"message": "Received JSON data successfully", "data": result}), 200, ) + else: - return jsonify({"message": "No JSON data received"}), 400 + return jsonify({"message": "No GET parameters received"}), 400 @key_bp.route("/", methods=["PUT"]) -def put_key(): +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": json_data}), + jsonify({"message": "Received JSON data successfully", "data": result}), 200, ) else: diff --git a/flask_demo_api/usecase/add.py b/flask_demo_api/usecase/add.py index e69de29..6b77ee5 100644 --- a/flask_demo_api/usecase/add.py +++ b/flask_demo_api/usecase/add.py @@ -0,0 +1,13 @@ +from protocols.models import KeyDTO +from protocols.repository import Repository + + +class PostKey: + def __init__( + self, + repository: Repository, + ) -> None: + self.__repository = repository + + def __call__(self, request: KeyDTO) -> KeyDTO: + return self.__repository.add_key(obj=request) diff --git a/flask_demo_api/usecase/get.py b/flask_demo_api/usecase/get.py index f382e19..ec4c01c 100644 --- a/flask_demo_api/usecase/get.py +++ b/flask_demo_api/usecase/get.py @@ -7,7 +7,7 @@ class GetKey: self, repository: Repository, ) -> None: - self.repository = repository + self.__repository = repository def __call__(self, request: KeyDTO) -> KeyDTO | None: - return self.repository.get_key(obj=request) + return self.__repository.get_key(obj=request) diff --git a/flask_demo_api/usecase/put.py b/flask_demo_api/usecase/put.py index e69de29..d2abd6f 100644 --- a/flask_demo_api/usecase/put.py +++ b/flask_demo_api/usecase/put.py @@ -0,0 +1,13 @@ +from protocols.models import KeyDTO +from protocols.repository import Repository + + +class PutKey: + def __init__( + self, + repository: Repository, + ) -> None: + self.__repository = repository + + def __call__(self, request: KeyDTO) -> KeyDTO: + return self.__repository.put_key(obj=request)