diff --git a/.gitignore b/.gitignore index e96a1739..80f81dcc 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ venv.bak/ .mypy_cache/ +service/keysconfig.py diff --git a/poetry.lock b/poetry.lock index 3d6f99a6..49920572 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,10 +1,9 @@ -# This file is automatically @generated by Poetry 1.4.0 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. [[package]] name = "annotated-types" version = "0.6.0" description = "Reusable constraint types to use with typing.Annotated" -category = "main" optional = false python-versions = ">=3.8" files = [ @@ -19,7 +18,6 @@ typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.9\""} name = "anyio" version = "3.7.1" description = "High level compatibility layer for multiple asynchronous event loop implementations" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -41,7 +39,6 @@ trio = ["trio (<0.22)"] name = "astroid" version = "3.0.1" description = "An abstract syntax tree for Python with inference support." -category = "dev" optional = false python-versions = ">=3.8.0" files = [ @@ -56,7 +53,6 @@ typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.11\""} name = "bandit" version = "1.7.5" description = "Security oriented static analyser for python code." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -80,7 +76,6 @@ yaml = ["PyYAML"] name = "black" version = "23.10.1" description = "The uncompromising code formatter." -category = "dev" optional = false python-versions = ">=3.8" files = [ @@ -123,7 +118,6 @@ uvloop = ["uvloop (>=0.15.2)"] name = "certifi" version = "2023.7.22" description = "Python package for providing Mozilla's CA Bundle." -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -135,7 +129,6 @@ files = [ name = "charset-normalizer" version = "3.3.2" description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet." -category = "main" optional = false python-versions = ">=3.7.0" files = [ @@ -235,7 +228,6 @@ files = [ name = "click" version = "8.1.7" description = "Composable command line interface toolkit" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -250,7 +242,6 @@ colorama = {version = "*", markers = "platform_system == \"Windows\""} name = "colorama" version = "0.4.6" description = "Cross-platform colored terminal text." -category = "main" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" files = [ @@ -262,7 +253,6 @@ files = [ name = "dill" version = "0.3.7" description = "serialize all of Python" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -277,7 +267,6 @@ graph = ["objgraph (>=1.7.2)"] name = "exceptiongroup" version = "1.1.3" description = "Backport of PEP 654 (exception groups)" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -292,7 +281,6 @@ test = ["pytest (>=6)"] name = "fastapi" version = "0.104.1" description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production" -category = "main" optional = false python-versions = ">=3.8" files = [ @@ -313,7 +301,6 @@ all = ["email-validator (>=2.0.0)", "httpx (>=0.23.0)", "itsdangerous (>=1.1.0)" name = "flake8" version = "6.1.0" description = "the modular source code checker: pep8 pyflakes and co" -category = "dev" optional = false python-versions = ">=3.8.1" files = [ @@ -330,7 +317,6 @@ pyflakes = ">=3.1.0,<3.2.0" name = "gitdb" version = "4.0.11" description = "Git Object Database" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -345,7 +331,6 @@ smmap = ">=3.0.1,<6" name = "gitpython" version = "3.1.40" description = "GitPython is a Python library used to interact with Git repositories" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -363,7 +348,6 @@ test = ["black", "coverage[toml]", "ddt (>=1.1.1,!=1.4.3)", "mock", "mypy", "pre name = "gunicorn" version = "21.2.0" description = "WSGI HTTP Server for UNIX" -category = "main" optional = false python-versions = ">=3.5" files = [ @@ -384,7 +368,6 @@ tornado = ["tornado (>=0.2)"] name = "h11" version = "0.12.0" description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1" -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -396,7 +379,6 @@ files = [ name = "httpcore" version = "0.14.7" description = "A minimal low-level HTTP client." -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -405,20 +387,19 @@ files = [ ] [package.dependencies] -anyio = ">=3.0.0,<4.0.0" +anyio = "==3.*" certifi = "*" h11 = ">=0.11,<0.13" -sniffio = ">=1.0.0,<2.0.0" +sniffio = "==1.*" [package.extras] http2 = ["h2 (>=3,<5)"] -socks = ["socksio (>=1.0.0,<2.0.0)"] +socks = ["socksio (==1.*)"] [[package]] name = "httpx" version = "0.22.0" description = "The next generation HTTP client." -category = "main" optional = false python-versions = ">=3.6" files = [ @@ -435,15 +416,14 @@ sniffio = "*" [package.extras] brotli = ["brotli", "brotlicffi"] -cli = ["click (>=8.0.0,<9.0.0)", "pygments (>=2.0.0,<3.0.0)", "rich (>=10.0.0,<11.0.0)"] +cli = ["click (==8.*)", "pygments (==2.*)", "rich (==10.*)"] http2 = ["h2 (>=3,<5)"] -socks = ["socksio (>=1.0.0,<2.0.0)"] +socks = ["socksio (==1.*)"] [[package]] name = "idna" version = "3.4" description = "Internationalized Domain Names in Applications (IDNA)" -category = "main" optional = false python-versions = ">=3.5" files = [ @@ -455,7 +435,6 @@ files = [ name = "iniconfig" version = "2.0.0" description = "brain-dead simple config-ini parsing" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -467,7 +446,6 @@ files = [ name = "isort" version = "5.12.0" description = "A Python utility / library to sort Python imports." -category = "dev" optional = false python-versions = ">=3.8.0" files = [ @@ -485,7 +463,6 @@ requirements-deprecated-finder = ["pip-api", "pipreqs"] name = "markdown-it-py" version = "3.0.0" description = "Python port of markdown-it. Markdown parsing, done right!" -category = "dev" optional = false python-versions = ">=3.8" files = [ @@ -510,7 +487,6 @@ testing = ["coverage", "pytest", "pytest-cov", "pytest-regressions"] name = "mccabe" version = "0.7.0" description = "McCabe checker, plugin for flake8" -category = "dev" optional = false python-versions = ">=3.6" files = [ @@ -522,7 +498,6 @@ files = [ name = "mdurl" version = "0.1.2" description = "Markdown URL utilities" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -534,7 +509,6 @@ files = [ name = "mypy" version = "1.6.1" description = "Optional static typing for Python" -category = "dev" optional = false python-versions = ">=3.8" files = [ @@ -581,7 +555,6 @@ reports = ["lxml"] name = "mypy-extensions" version = "1.0.0" description = "Type system extensions for programs checked with the mypy type checker." -category = "dev" optional = false python-versions = ">=3.5" files = [ @@ -593,7 +566,6 @@ files = [ name = "orjson" version = "3.9.10" description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy" -category = "main" optional = false python-versions = ">=3.8" files = [ @@ -653,7 +625,6 @@ files = [ name = "packaging" version = "23.2" description = "Core utilities for Python packages" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -665,7 +636,6 @@ files = [ name = "pathspec" version = "0.11.2" description = "Utility library for gitignore style pattern matching of file paths." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -677,7 +647,6 @@ files = [ name = "pbr" version = "5.11.1" description = "Python Build Reasonableness" -category = "dev" optional = false python-versions = ">=2.6" files = [ @@ -689,7 +658,6 @@ files = [ name = "platformdirs" version = "3.11.0" description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -705,7 +673,6 @@ test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4)", "pytest-co name = "pluggy" version = "1.3.0" description = "plugin and hook calling mechanisms for python" -category = "dev" optional = false python-versions = ">=3.8" files = [ @@ -721,7 +688,6 @@ testing = ["pytest", "pytest-benchmark"] name = "pycodestyle" version = "2.11.1" description = "Python style guide checker" -category = "dev" optional = false python-versions = ">=3.8" files = [ @@ -733,7 +699,6 @@ files = [ name = "pydantic" version = "2.4.2" description = "Data validation using Python type hints" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -753,7 +718,6 @@ email = ["email-validator (>=2.0.0)"] name = "pydantic-core" version = "2.10.1" description = "" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -872,7 +836,6 @@ typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0" name = "pydantic-settings" version = "2.0.3" description = "Settings management using Pydantic" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -888,7 +851,6 @@ python-dotenv = ">=0.21.0" name = "pyflakes" version = "3.1.0" description = "passive checker of Python programs" -category = "dev" optional = false python-versions = ">=3.8" files = [ @@ -900,7 +862,6 @@ files = [ name = "pygments" version = "2.16.1" description = "Pygments is a syntax highlighting package written in Python." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -915,7 +876,6 @@ plugins = ["importlib-metadata"] name = "pylint" version = "3.0.2" description = "python code static checker" -category = "dev" optional = false python-versions = ">=3.8.0" files = [ @@ -928,8 +888,8 @@ astroid = ">=3.0.1,<=3.1.0-dev0" colorama = {version = ">=0.4.5", markers = "sys_platform == \"win32\""} dill = [ {version = ">=0.2", markers = "python_version < \"3.11\""}, - {version = ">=0.3.6", markers = "python_version >= \"3.11\""}, {version = ">=0.3.7", markers = "python_version >= \"3.12\""}, + {version = ">=0.3.6", markers = "python_version >= \"3.11\" and python_version < \"3.12\""}, ] isort = ">=4.2.5,<6" mccabe = ">=0.6,<0.8" @@ -946,7 +906,6 @@ testutils = ["gitpython (>3)"] name = "pytest" version = "7.4.3" description = "pytest: simple powerful testing with Python" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -969,7 +928,6 @@ testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "no name = "python-dotenv" version = "1.0.0" description = "Read key-value pairs from a .env file and set them as environment variables" -category = "main" optional = false python-versions = ">=3.8" files = [ @@ -984,7 +942,6 @@ cli = ["click (>=5.0)"] name = "pyyaml" version = "6.0.1" description = "YAML parser and emitter for Python" -category = "dev" optional = false python-versions = ">=3.6" files = [ @@ -1044,7 +1001,6 @@ files = [ name = "requests" version = "2.31.0" description = "Python HTTP for Humans." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1066,7 +1022,6 @@ use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] name = "rfc3986" version = "1.5.0" description = "Validating URI References per RFC 3986" -category = "main" optional = false python-versions = "*" files = [ @@ -1084,7 +1039,6 @@ idna2008 = ["idna"] name = "rich" version = "13.6.0" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" -category = "dev" optional = false python-versions = ">=3.7.0" files = [ @@ -1104,7 +1058,6 @@ jupyter = ["ipywidgets (>=7.5.1,<9)"] name = "smmap" version = "5.0.1" description = "A pure Python implementation of a sliding window memory map manager" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1116,7 +1069,6 @@ files = [ name = "sniffio" version = "1.3.0" description = "Sniff out which async library your code is running under" -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -1128,7 +1080,6 @@ files = [ name = "starlette" version = "0.27.0" description = "The little ASGI library that shines." -category = "main" optional = false python-versions = ">=3.7" files = [ @@ -1147,7 +1098,6 @@ full = ["httpx (>=0.22.0)", "itsdangerous", "jinja2", "python-multipart", "pyyam name = "stevedore" version = "5.1.0" description = "Manage dynamic plugins for Python applications" -category = "dev" optional = false python-versions = ">=3.8" files = [ @@ -1162,7 +1112,6 @@ pbr = ">=2.0.0,<2.1.0 || >2.1.0" name = "tomli" version = "2.0.1" description = "A lil' TOML parser" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1174,7 +1123,6 @@ files = [ name = "tomlkit" version = "0.12.2" description = "Style preserving TOML library" -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1186,7 +1134,6 @@ files = [ name = "typing-extensions" version = "4.8.0" description = "Backported and Experimental Type Hints for Python 3.8+" -category = "main" optional = false python-versions = ">=3.8" files = [ @@ -1198,7 +1145,6 @@ files = [ name = "urllib3" version = "2.0.7" description = "HTTP library with thread-safe connection pooling, file post, and more." -category = "dev" optional = false python-versions = ">=3.7" files = [ @@ -1216,7 +1162,6 @@ zstd = ["zstandard (>=0.18.0)"] name = "uvicorn" version = "0.23.2" description = "The lightning-fast ASGI server." -category = "main" optional = false python-versions = ">=3.8" files = [ @@ -1232,52 +1177,7 @@ typing-extensions = {version = ">=4.0", markers = "python_version < \"3.11\""} [package.extras] standard = ["colorama (>=0.4)", "httptools (>=0.5.0)", "python-dotenv (>=0.13)", "pyyaml (>=5.1)", "uvloop (>=0.14.0,!=0.15.0,!=0.15.1)", "watchfiles (>=0.13)", "websockets (>=10.4)"] -[[package]] -name = "uvloop" -version = "0.19.0" -description = "Fast implementation of asyncio event loop on top of libuv" -category = "main" -optional = false -python-versions = ">=3.8.0" -files = [ - {file = "uvloop-0.19.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:de4313d7f575474c8f5a12e163f6d89c0a878bc49219641d49e6f1444369a90e"}, - {file = "uvloop-0.19.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:5588bd21cf1fcf06bded085f37e43ce0e00424197e7c10e77afd4bbefffef428"}, - {file = "uvloop-0.19.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7b1fd71c3843327f3bbc3237bedcdb6504fd50368ab3e04d0410e52ec293f5b8"}, - {file = "uvloop-0.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5a05128d315e2912791de6088c34136bfcdd0c7cbc1cf85fd6fd1bb321b7c849"}, - {file = "uvloop-0.19.0-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:cd81bdc2b8219cb4b2556eea39d2e36bfa375a2dd021404f90a62e44efaaf957"}, - {file = "uvloop-0.19.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:5f17766fb6da94135526273080f3455a112f82570b2ee5daa64d682387fe0dcd"}, - {file = "uvloop-0.19.0-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:4ce6b0af8f2729a02a5d1575feacb2a94fc7b2e983868b009d51c9a9d2149bef"}, - {file = "uvloop-0.19.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:31e672bb38b45abc4f26e273be83b72a0d28d074d5b370fc4dcf4c4eb15417d2"}, - {file = "uvloop-0.19.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:570fc0ed613883d8d30ee40397b79207eedd2624891692471808a95069a007c1"}, - {file = "uvloop-0.19.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5138821e40b0c3e6c9478643b4660bd44372ae1e16a322b8fc07478f92684e24"}, - {file = "uvloop-0.19.0-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:91ab01c6cd00e39cde50173ba4ec68a1e578fee9279ba64f5221810a9e786533"}, - {file = "uvloop-0.19.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:47bf3e9312f63684efe283f7342afb414eea4d3011542155c7e625cd799c3b12"}, - {file = "uvloop-0.19.0-cp312-cp312-macosx_10_9_universal2.whl", hash = "sha256:da8435a3bd498419ee8c13c34b89b5005130a476bda1d6ca8cfdde3de35cd650"}, - {file = "uvloop-0.19.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:02506dc23a5d90e04d4f65c7791e65cf44bd91b37f24cfc3ef6cf2aff05dc7ec"}, - {file = "uvloop-0.19.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2693049be9d36fef81741fddb3f441673ba12a34a704e7b4361efb75cf30befc"}, - {file = "uvloop-0.19.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7010271303961c6f0fe37731004335401eb9075a12680738731e9c92ddd96ad6"}, - {file = "uvloop-0.19.0-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:5daa304d2161d2918fa9a17d5635099a2f78ae5b5960e742b2fcfbb7aefaa593"}, - {file = "uvloop-0.19.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:7207272c9520203fea9b93843bb775d03e1cf88a80a936ce760f60bb5add92f3"}, - {file = "uvloop-0.19.0-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:78ab247f0b5671cc887c31d33f9b3abfb88d2614b84e4303f1a63b46c046c8bd"}, - {file = "uvloop-0.19.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:472d61143059c84947aa8bb74eabbace30d577a03a1805b77933d6bd13ddebbd"}, - {file = "uvloop-0.19.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:45bf4c24c19fb8a50902ae37c5de50da81de4922af65baf760f7c0c42e1088be"}, - {file = "uvloop-0.19.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:271718e26b3e17906b28b67314c45d19106112067205119dddbd834c2b7ce797"}, - {file = "uvloop-0.19.0-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:34175c9fd2a4bc3adc1380e1261f60306344e3407c20a4d684fd5f3be010fa3d"}, - {file = "uvloop-0.19.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:e27f100e1ff17f6feeb1f33968bc185bf8ce41ca557deee9d9bbbffeb72030b7"}, - {file = "uvloop-0.19.0-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:13dfdf492af0aa0a0edf66807d2b465607d11c4fa48f4a1fd41cbea5b18e8e8b"}, - {file = "uvloop-0.19.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:6e3d4e85ac060e2342ff85e90d0c04157acb210b9ce508e784a944f852a40e67"}, - {file = "uvloop-0.19.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8ca4956c9ab567d87d59d49fa3704cf29e37109ad348f2d5223c9bf761a332e7"}, - {file = "uvloop-0.19.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f467a5fd23b4fc43ed86342641f3936a68ded707f4627622fa3f82a120e18256"}, - {file = "uvloop-0.19.0-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:492e2c32c2af3f971473bc22f086513cedfc66a130756145a931a90c3958cb17"}, - {file = "uvloop-0.19.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:2df95fca285a9f5bfe730e51945ffe2fa71ccbfdde3b0da5772b4ee4f2e770d5"}, - {file = "uvloop-0.19.0.tar.gz", hash = "sha256:0246f4fd1bf2bf702e06b0d45ee91677ee5c31242f39aab4ea6fe0c51aedd0fd"}, -] - -[package.extras] -docs = ["Sphinx (>=4.1.2,<4.2.0)", "sphinx-rtd-theme (>=0.5.2,<0.6.0)", "sphinxcontrib-asyncio (>=0.3.0,<0.4.0)"] -test = ["Cython (>=0.29.36,<0.30.0)", "aiohttp (==3.9.0b0)", "aiohttp (>=3.8.1)", "flake8 (>=5.0,<6.0)", "mypy (>=0.800)", "psutil", "pyOpenSSL (>=23.0.0,<23.1.0)", "pycodestyle (>=2.9.0,<2.10.0)"] - [metadata] lock-version = "2.0" python-versions = "^3.8.1" -content-hash = "add0a32ac2cde7c15ee5132b2b1ea1216dab96d9a3d01238822ff4b94e7800a5" +content-hash = "5a4958785966af0fa99213ad99312ddafdead4a2b5fc632b638981b31543fbef" diff --git a/pyproject.toml b/pyproject.toml index 87204682..f420c838 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,6 @@ python = "^3.8.1" fastapi = "^0.104.0" pydantic = "^2.4.2" gunicorn = "^21.2.0" -uvloop = "^0.19.0" uvicorn = "^0.23.0" orjson = "^3.9.10" starlette = "^0.27.0" diff --git a/service/api/app.py b/service/api/app.py index 838427f7..dacf838c 100644 --- a/service/api/app.py +++ b/service/api/app.py @@ -2,7 +2,7 @@ from concurrent.futures.thread import ThreadPoolExecutor from typing import Any, Dict -import uvloop + from fastapi import FastAPI from ..log import app_logger, setup_logging @@ -15,7 +15,6 @@ def setup_asyncio(thread_name_prefix: str) -> None: - uvloop.install() loop = asyncio.get_event_loop() diff --git a/service/api/views.py b/service/api/views.py index 24cf4a7f..1cc49ea2 100644 --- a/service/api/views.py +++ b/service/api/views.py @@ -1,18 +1,48 @@ from typing import List - +from http import HTTPStatus from fastapi import APIRouter, FastAPI, Request -from pydantic import BaseModel - +from fastapi import HTTPException, Security +from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer +from pydantic import BaseModel, Field +import random from service.api.exceptions import UserNotFoundError from service.log import app_logger +from service.keysconfig import api_key class RecoResponse(BaseModel): user_id: int items: List[int] +ErrorResponseModel = BaseModel.model_construct(detail=Field(...)) + +common_responses = { + 200: { + "description": "Success!", + "model": RecoResponse, + }, + 404: { + "description": "Your model or user id was not found", + "model": BaseModel.model_construct(detail=Field(...)), + }, + 401: { + "description": "Unauthorized access", + "model": BaseModel.model_construct(detail=Field(...)), + }, + 403: { + "description": "Unauthorized access", + "model": BaseModel.model_construct(detail=Field(...)), + } +} router = APIRouter() +security = HTTPBearer() + +async def verify_token(http_authorization_credentials: HTTPAuthorizationCredentials = Security(security)): + token = http_authorization_credentials.credentials + if token != api_key: + raise HTTPException(status_code=HTTPStatus.UNAUTHORIZED, detail="Invalid token") + @router.get( @@ -27,6 +57,8 @@ async def health() -> str: path="/reco/{model_name}/{user_id}", tags=["Recommendations"], response_model=RecoResponse, + dependencies=[Security(verify_token)], + responses = common_responses ) async def get_reco( request: Request, @@ -35,13 +67,18 @@ async def get_reco( ) -> RecoResponse: app_logger.info(f"Request for model: {model_name}, user_id: {user_id}") - # Write your code here + + if user_id > 10**9: raise UserNotFoundError(error_message=f"User {user_id} not found") - - k_recs = request.app.state.k_recs - reco = list(range(k_recs)) + if model_name != 'random': + raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail=f"There is no model named {model_name}") + if model_name == "random": + reco = random.sample(range(1, 10), 10) + else: + k_recs = request.app.state.k_recs + reco = list(range(k_recs)) return RecoResponse(user_id=user_id, items=reco) diff --git a/tests/api/test_views.py b/tests/api/test_views.py index 50516b47..8dcd09e8 100644 --- a/tests/api/test_views.py +++ b/tests/api/test_views.py @@ -1,11 +1,11 @@ from http import HTTPStatus from starlette.testclient import TestClient - +from service.keysconfig import api_key from service.settings import ServiceConfig GET_RECO_PATH = "/reco/{model_name}/{user_id}" - +HEADERS = {f"Your key is {api_key}"} def test_health( client: TestClient, @@ -21,8 +21,8 @@ def test_get_reco_success( ) -> None: user_id = 123 path = GET_RECO_PATH.format(model_name="some_model", user_id=user_id) - with client: - response = client.get(path) + + response = client.get(path, headers=HEADERS) assert response.status_code == HTTPStatus.OK response_json = response.json() assert response_json["user_id"] == user_id @@ -35,7 +35,31 @@ def test_get_reco_for_unknown_user( ) -> None: user_id = 10**10 path = GET_RECO_PATH.format(model_name="some_model", user_id=user_id) - with client: - response = client.get(path) - assert response.status_code == HTTPStatus.NOT_FOUND + response = client.get(path, headers=HEADERS) + assert response.status_code == HTTPStatus.NOT_FOUND #404 assert response.json()["errors"][0]["error_key"] == "user_not_found" + +def test_get_reco_invalid_model(client: TestClient) -> None: + user_id = 123 + path = GET_RECO_PATH.format(model_name='dsa', user_id=user_id) + response = client.get(path, headers=HEADERS) + assert response.status_code == HTTPStatus.NOT_FOUND #404 + + +def test_get_reco_unauthorized(client: TestClient) -> None: + user_id = 123 + path = GET_RECO_PATH.format(model_name="some_model", user_id=user_id) + response = client.get(path) + assert response.status_code == HTTPStatus.FORBIDDEN #403 + assert response.json()["errors"][0]["error_key"] == "user_not_authorized" + + +def test_get_reco_authorized(client: TestClient) -> None: + user_id = 123 + path = GET_RECO_PATH.format(model_name="some_model", user_id=user_id) + response = client.get(path, headers=HEADERS) + assert response.status_code == HTTPStatus.OK + response_json = response.json() + assert response_json["user_id"] == user_id + +