main
Сергей Ванюшкин 2024-04-14 23:16:07 +00:00
parent 24d1a80164
commit 3335ee5e22
14 changed files with 122 additions and 1 deletions

View File

@ -1,6 +1,13 @@
from flask import Flask from flask import Flask
from routers.key import key_bp
def app_factory() -> Flask: def app_factory() -> Flask:
app = Flask(__name__) app = Flask(__name__)
app.register_blueprint(key_bp)
return app return app
if __name__ == "__main__":
app = app_factory()
app.run(debug=True)

View File

View File

@ -0,0 +1,7 @@
from dataclasses import dataclass
@dataclass()
class KeyDTO:
key: str
val: str

View File

@ -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

View File

View File

@ -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")

View File

View File

@ -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

View File

View File

View File

@ -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)

View File

13
poetry.lock generated
View File

@ -58,6 +58,17 @@ files = [
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, {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]] [[package]]
name = "distlib" name = "distlib"
version = "0.3.8" version = "0.3.8"
@ -399,4 +410,4 @@ watchdog = ["watchdog (>=2.3)"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.11" python-versions = "^3.11"
content-hash = "7d672e07f8fb8e47cea10a994c28d2a312700932f1675602740638ca10ca0e20" content-hash = "1e2a8795b8f7a080a95a4983bef9fd54096d0ce2668526045de78c8808678987"

View File

@ -9,6 +9,7 @@ readme = "README.md"
python = "^3.11" python = "^3.11"
flask = "^3.0.3" flask = "^3.0.3"
redis = "^5.0.3" redis = "^5.0.3"
dishka = "^1.0.0"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]