From 3335ee5e22f4b0b95756cc35946dae18efd5d385 Mon Sep 17 00:00:00 2001 From: Sergey Vanyushkin Date: Sun, 14 Apr 2024 23:16:07 +0000 Subject: [PATCH] init --- flask_demo_api/main.py | 7 +++++ flask_demo_api/protocols/__init__.py | 0 flask_demo_api/protocols/models.py | 7 +++++ flask_demo_api/protocols/repository.py | 18 +++++++++++ flask_demo_api/repository/__init__.py | 0 flask_demo_api/repository/redis.py | 22 ++++++++++++++ flask_demo_api/routers/__init__.py | 0 flask_demo_api/routers/key.py | 42 ++++++++++++++++++++++++++ flask_demo_api/usecase/__init__.py | 0 flask_demo_api/usecase/add.py | 0 flask_demo_api/usecase/get.py | 13 ++++++++ flask_demo_api/usecase/put.py | 0 poetry.lock | 13 +++++++- pyproject.toml | 1 + 14 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 flask_demo_api/protocols/__init__.py create mode 100644 flask_demo_api/protocols/models.py create mode 100644 flask_demo_api/protocols/repository.py create mode 100644 flask_demo_api/repository/__init__.py create mode 100644 flask_demo_api/repository/redis.py create mode 100644 flask_demo_api/routers/__init__.py create mode 100644 flask_demo_api/routers/key.py create mode 100644 flask_demo_api/usecase/__init__.py create mode 100644 flask_demo_api/usecase/add.py create mode 100644 flask_demo_api/usecase/get.py create mode 100644 flask_demo_api/usecase/put.py diff --git a/flask_demo_api/main.py b/flask_demo_api/main.py index 146a55c..7baa107 100644 --- a/flask_demo_api/main.py +++ b/flask_demo_api/main.py @@ -1,6 +1,13 @@ from flask import Flask +from routers.key import key_bp def app_factory() -> Flask: app = Flask(__name__) + app.register_blueprint(key_bp) return app + + +if __name__ == "__main__": + app = app_factory() + app.run(debug=True) diff --git a/flask_demo_api/protocols/__init__.py b/flask_demo_api/protocols/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/flask_demo_api/protocols/models.py b/flask_demo_api/protocols/models.py new file mode 100644 index 0000000..208f033 --- /dev/null +++ b/flask_demo_api/protocols/models.py @@ -0,0 +1,7 @@ +from dataclasses import dataclass + + +@dataclass() +class KeyDTO: + key: str + val: str diff --git a/flask_demo_api/protocols/repository.py b/flask_demo_api/protocols/repository.py new file mode 100644 index 0000000..988906f --- /dev/null +++ b/flask_demo_api/protocols/repository.py @@ -0,0 +1,18 @@ +from abc import abstractmethod +from typing import Protocol + +from flask_demo_api.protocols.models import KeyDTO + + +class Repository(Protocol): + @abstractmethod + def get_key(self, obj: KeyDTO) -> KeyDTO | None: + raise NotImplementedError + + @abstractmethod + def add_key(self, obj: KeyDTO) -> KeyDTO: + raise NotImplementedError + + @abstractmethod + def put_key(self, obj: KeyDTO) -> KeyDTO: + raise NotImplementedError diff --git a/flask_demo_api/repository/__init__.py b/flask_demo_api/repository/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/flask_demo_api/repository/redis.py b/flask_demo_api/repository/redis.py new file mode 100644 index 0000000..de58d1e --- /dev/null +++ b/flask_demo_api/repository/redis.py @@ -0,0 +1,22 @@ +import redis + +from flask_demo_api.protocols.models import KeyDTO +from flask_demo_api.protocols.repository import Repository + + +class RedisRepository(Repository): + def __init__(self, redis_pool: redis.Redis) -> None: + 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)) + + def add_key(self, obj: KeyDTO) -> KeyDTO: + self.pool.set(obj.key, obj.val) + return KeyDTO(key="3", val="4") + + def put_key(self, obj: KeyDTO) -> KeyDTO: + return KeyDTO(key="5", val="6") diff --git a/flask_demo_api/routers/__init__.py b/flask_demo_api/routers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/flask_demo_api/routers/key.py b/flask_demo_api/routers/key.py new file mode 100644 index 0000000..a35f86b --- /dev/null +++ b/flask_demo_api/routers/key.py @@ -0,0 +1,42 @@ +from flask import Blueprint, jsonify, request + +key_bp = Blueprint("key_bp", __name__) + + +@key_bp.route("/", methods=["POST"]) +def past_key(): + json_data = request.get_json() + + if json_data: + return ( + jsonify({"message": "Received JSON data successfully", "data": json_data}), + 200, + ) + else: + return jsonify({"message": "No JSON data received"}), 400 + + +@key_bp.route("/", methods=["GET"]) +def get_key(): + json_data = request.args.get("key") + + if json_data: + return ( + jsonify({"message": "Received JSON data successfully", "data": json_data}), + 200, + ) + else: + return jsonify({"message": "No JSON data received"}), 400 + + +@key_bp.route("/", methods=["PUT"]) +def put_key(): + json_data = request.get_json() + + if json_data: + return ( + jsonify({"message": "Received JSON data successfully", "data": json_data}), + 200, + ) + else: + return jsonify({"message": "No JSON data received"}), 400 diff --git a/flask_demo_api/usecase/__init__.py b/flask_demo_api/usecase/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/flask_demo_api/usecase/add.py b/flask_demo_api/usecase/add.py new file mode 100644 index 0000000..e69de29 diff --git a/flask_demo_api/usecase/get.py b/flask_demo_api/usecase/get.py new file mode 100644 index 0000000..3804c3c --- /dev/null +++ b/flask_demo_api/usecase/get.py @@ -0,0 +1,13 @@ +from flask_demo_api.protocols.models import KeyDTO +from flask_demo_api.protocols.repository import Repository + + +class GetKey: + def __init__( + self, + repository: Repository, + ) -> None: + self.repository = repository + + def __call__(self, request: KeyDTO) -> KeyDTO | None: + return self.repository.get_key(obj=request) diff --git a/flask_demo_api/usecase/put.py b/flask_demo_api/usecase/put.py new file mode 100644 index 0000000..e69de29 diff --git a/poetry.lock b/poetry.lock index 0056091..5180ea4 100644 --- a/poetry.lock +++ b/poetry.lock @@ -58,6 +58,17 @@ files = [ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +[[package]] +name = "dishka" +version = "1.0.0" +description = "Minimal DI framework" +optional = false +python-versions = ">=3.10" +files = [ + {file = "dishka-1.0.0-py3-none-any.whl", hash = "sha256:d664a8f0aa26d84ca5358ecbccbd35af764cd4390b003c7aad93b7918d76ec94"}, + {file = "dishka-1.0.0.tar.gz", hash = "sha256:faed0353e43515d3485557037ce4cb63a3acf747e32f6c6b3bf1ff286cbf689b"}, +] + [[package]] name = "distlib" version = "0.3.8" @@ -399,4 +410,4 @@ watchdog = ["watchdog (>=2.3)"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "7d672e07f8fb8e47cea10a994c28d2a312700932f1675602740638ca10ca0e20" +content-hash = "1e2a8795b8f7a080a95a4983bef9fd54096d0ce2668526045de78c8808678987" diff --git a/pyproject.toml b/pyproject.toml index 327f098..a91fec4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ readme = "README.md" python = "^3.11" flask = "^3.0.3" redis = "^5.0.3" +dishka = "^1.0.0" [tool.poetry.group.dev.dependencies]