{"id":18581406,"url":"https://github.com/taskiq-python/aiohttp-deps","last_synced_at":"2025-06-20T06:35:13.084Z","repository":{"id":153793490,"uuid":"630692437","full_name":"taskiq-python/aiohttp-deps","owner":"taskiq-python","description":"Dependency injection for AioHTTP","archived":false,"fork":false,"pushed_at":"2024-12-09T13:31:46.000Z","size":359,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-04T10:58:30.868Z","etag":null,"topics":["aiohttp","aiohttp-server","dependency-injection","fastapi","openapi3","swagger"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taskiq-python.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-21T00:08:54.000Z","updated_at":"2024-12-09T13:31:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"a06b087d-6398-490e-9646-9978050ead4e","html_url":"https://github.com/taskiq-python/aiohttp-deps","commit_stats":{"total_commits":63,"total_committers":1,"mean_commits":63.0,"dds":0.0,"last_synced_commit":"dc80597d5f8d912db636b6d5215434b84a17280f"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/taskiq-python/aiohttp-deps","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Faiohttp-deps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Faiohttp-deps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Faiohttp-deps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Faiohttp-deps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taskiq-python","download_url":"https://codeload.github.com/taskiq-python/aiohttp-deps/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Faiohttp-deps/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259565914,"owners_count":22877417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aiohttp","aiohttp-server","dependency-injection","fastapi","openapi3","swagger"],"created_at":"2024-11-07T00:05:08.836Z","updated_at":"2025-06-20T06:35:08.071Z","avatar_url":"https://github.com/taskiq-python.png","language":"Python","readme":"[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/aiohttp-deps?style=for-the-badge)](https://pypi.org/project/aiohttp-deps/)\n[![PyPI](https://img.shields.io/pypi/v/aiohttp-deps?style=for-the-badge)](https://pypi.org/project/aiohttp-deps/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/aiohttp-deps?style=for-the-badge)](https://pypistats.org/packages/aiohttp-deps)\n\n# AioHTTP deps\n\n\nThis project was initially created to show the abillities of [taskiq-dependencies](https://github.com/taskiq-python/taskiq-dependencies) project, which is used by [taskiq](https://github.com/taskiq-python/taskiq) to provide you with the best experience of sending distributed tasks.\n\nThis project adds [FastAPI](https://github.com/tiangolo/fastapi)-like dependency injection to your [AioHTTP](https://github.com/aio-libs/aiohttp) application and swagger documentation based on types.\n\nTo start using dependency injection, just initialize the injector.\n\n```python\nfrom aiohttp import web\nfrom aiohttp_deps import init as deps_init\n\n\napp = web.Application()\n\n\napp.on_startup.append(deps_init)\n\nweb.run_app(app)\n\n```\n\n\nIf you use mypy, then we have a custom router with propper types.\n\n\n```python\nfrom aiohttp import web\nfrom aiohttp_deps import init as deps_init\nfrom aiohttp_deps import Router\n\nrouter = Router()\n\n\n@router.get(\"/\")\nasync def handler():\n    return web.json_response({})\n\n\napp = web.Application()\n\napp.router.add_routes(router)\n\napp.on_startup.append(deps_init)\n\nweb.run_app(app)\n\n```\n\nAlso, you can nest routers with prefixes,\n\n```python\napi_router = Router()\n\nmemes_router = Router()\n\nmain_router = Router()\n\nmain_router.add_routes(api_router, prefix=\"/api\")\nmain_router.add_routes(memes_router, prefix=\"/memes\")\n```\n\n## Swagger\n\nIf you use dependencies in you handlers, we can easily generate swagger for you.\nWe have some limitations:\n1. We don't support resolving type aliases if hint is a string.\n    If you define variable like this: `myvar = int | None` and then in handler\n    you'd create annotation like this: `param: \"str | myvar\"` it will fail.\n    You need to unquote type hint in order to get it work.\n\nWe will try to fix these limitations later.\n\nTo enable swagger, just add it to your startup.\n\n```python\nfrom aiohttp_deps import init, setup_swagger\n\napp = web.Application()\n\napp.on_startup.extend([init, setup_swagger()])\n```\n\n### Responses\n\nYou can define schema for responses using dataclasses or\npydantic models. This would not affect handlers in any way,\nit's only for documentation purposes, if you want to actually\nvalidate values your handler returns, please write your own wrapper.\n\n```python\nfrom dataclasses import dataclass\n\nfrom aiohttp import web\nfrom pydantic import BaseModel\n\nfrom aiohttp_deps import Router, openapi_response\n\nrouter = Router()\n\n\n@dataclass\nclass Success:\n    data: str\n\n\nclass Unauthorized(BaseModel):\n    why: str\n\n\n@router.get(\"/\")\n@openapi_response(200, Success, content_type=\"application/xml\")\n@openapi_response(200, Success)\n@openapi_response(401, Unauthorized, description=\"When token is not correct\")\nasync def handler() -\u003e web.Response:\n    ...\n```\n\nThis example illustrates how much you can do with this decorator. You\ncan have multiple content-types for a single status, or you can have different\npossble statuses. This function is pretty simple and if you want to make\nyour own decorator for your responses, it won't be hard.\n\n\n## Default dependencies\n\nBy default this library provides only two injectables. `web.Request` and `web.Application`.\n\n```python\n\nasync def handler(app: web.Application = Depends()): ...\n\nasync def handler2(request: web.Request = Depends()): ...\n\n```\n\nIt's super useful, because you can use these dependencies in\nany other dependency. Here's a more complex example of how you can use this library.\n\n\n```python\nfrom aiohttp_deps import Router, Depends\nfrom aiohttp import web\n\nrouter = Router()\n\n\nasync def get_db_session(app: web.Application = Depends()):\n    async with app[web.AppKey(\"db\")] as sess:\n        yield sess\n\n\nclass MyDAO:\n    def __init__(self, session=Depends(get_db_session)):\n        self.session = session\n\n    async def get_objects(self) -\u003e list[object]:\n        return await self.session.execute(\"SELECT 1\")\n\n\n@router.get(\"/\")\nasync def handler(db_session: MyDAO = Depends()):\n    objs = await db_session.get_objects()\n    return web.json_response({\"objects\": objs})\n\n```\n\nIf you do something like this, you would never think about initializing your DAO. You can just inject it and that's it.\n\n\n# Built-in dependencies\n\nThis library also provides you with some default dependencies that can help you in building the best web-service.\n\n## Json\n\nTo parse json, create a pydantic model and add a dependency to your handler.\n\n\n```python\nfrom aiohttp import web\nfrom pydantic import BaseModel\nfrom aiohttp_deps import Router, Json, Depends\n\nrouter = Router()\n\n\nclass UserInfo(BaseModel):\n    name: str\n\n\n@router.post(\"/users\")\nasync def new_data(user: UserInfo = Depends(Json())):\n    return web.json_response({\"user\": user.model_dump()})\n\n```\n\nThis dependency automatically validates data and send\nerrors if the data doesn't orrelate with schema or body is not a valid json.\n\nIf you want to make this data optional, just mark it as optional.\n\n```python\n@router.post(\"/users\")\nasync def new_data(user: Optional[UserInfo] = Depends(Json())):\n    if user is None:\n        return web.json_response({\"user\": None})\n    return web.json_response({\"user\": user.model_dump()})\n\n```\n\n## Headers\n\nYou can get and validate headers using `Header` dependency.\n\nLet's try to build simple example for authorization.\n\n```python\nfrom aiohttp_deps import Router, Header, Depends\nfrom aiohttp import web\n\nrouter = Router()\n\n\ndef decode_token(authorization: str = Depends(Header())) -\u003e str:\n    if authorization == \"secret\":\n        # Let's pretend that here we\n        # decode our token.\n        return authorization\n    raise web.HTTPUnauthorized()\n\n\n@router.get(\"/secret_data\")\nasync def new_data(token: str = Depends(decode_token)) -\u003e web.Response:\n    return web.json_response({\"secret\": \"not a secret\"})\n\n```\n\nAs you can see, header name to parse is equal to the\nname of a parameter that introduces Header dependency.\n\nIf you want to use some name that is not allowed in python, or just want to have different names, you can use alias. Like this:\n\n```python\ndef decode_token(auth: str = Depends(Header(alias=\"Authorization\"))) -\u003e str:\n```\n\nHeaders can also be parsed to types. If you want a header to be parsed as int, just add the typehint.\n\n```python\ndef decode_token(meme_id: int = Depends(Header())) -\u003e str:\n```\n\nIf you want to get list of values of one header, use parameter `multiple=True`.\n\n```python\ndef decode_token(meme_id: list[int] = Depends(Header(multiple=True))) -\u003e str:\n\n```\n\nAnd, of course, you can provide this dependency with default value if the value from user cannot be parsed for some reason.\n\n```python\ndef decode_token(meme_id: str = Depends(Header(default=\"not-a-secret\"))) -\u003e str:\n```\n\n\n## Queries\n\nYou can depend on `Query` to get and parse query parameters.\n\n```python\nfrom aiohttp_deps import Router, Query, Depends\nfrom aiohttp import web\n\nrouter = Router()\n\n\n@router.get(\"/shop\")\nasync def shop(item_id: str = Depends(Query())) -\u003e web.Response:\n    return web.json_response({\"id\": item_id})\n\n```\n\nthe name of the parameter is the same as the name of function parameter.\n\nThe Query dependency is acually the same as the Header dependency, so everything about the `Header` dependency also applies to `Query`.\n\n## Views\n\nIf you use views as handlers, please use View class from `aiohttp_deps`, otherwise the magic won't work.\n\n```python\nfrom aiohttp_deps import Router, View, Depends\nfrom aiohttp import web\n\nrouter = Router()\n\n\n@router.view(\"/view\")\nclass MyView(View):\n    async def get(self, app: web.Application = Depends()):\n        return web.json_response({\"app\": str(app)})\n\n```\n\n\n## Forms\n\nNow you can easiy get and validate form data from your request.\nTo make the magic happen, please add `arbitrary_types_allowed` to the config of your model.\n\n\n```python\nimport pydantic\nfrom aiohttp_deps import Router, Depends, Form\nfrom aiohttp import web\n\nrouter = Router()\n\n\nclass MyForm(pydantic.BaseModel):\n    id: int\n    file: web.FileField\n\n    model_config = pydantic.ConfigDict(arbitrary_types_allowed=True)\n\n\n@router.post(\"/\")\nasync def handler(my_form: MyForm = Depends(Form())):\n    with open(\"my_file\", \"wb\") as f:\n        f.write(my_form.file.file.read())\n    return web.json_response({\"id\": my_form.id})\n\n```\n\n## Path\n\nIf you have path variables, you can also inject them in your handler.\n\n```python\nfrom aiohttp_deps import Router, Path, Depends\nfrom aiohttp import web\n\nrouter = Router()\n\n\n@router.get(\"/view/{var}\")\nasync def my_handler(var: str = Depends(Path())):\n    return web.json_response({\"var\": var})\n\n```\n\n\n## ExtraOpenAPI\n\nThis dependency is used to add additional swagger fields to the endpoint's swagger\nthat is using this dependency. It might be even indirect dependency.\n\nYou can check how this thing can be used in our [examples/swagger_auth.py](https://github.com/taskiq-python/aiohttp-deps/tree/master/examples/swagger_auth.py).\n\n\n## Overriding dependencies\n\nSometimes for tests you don't want to calculate actual functions\nand you want to pass another functions instead.\n\nTo do so, you can add \"dependency_overrides\" or \"values_overrides\" to the aplication's state.\nThese values should be dicts. The keys for these values can be found in `aiohttp_deps.keys` module.\n\nHere's an example.\n\n```python\ndef original_dep() -\u003e int:\n    return 1\n\nclass MyView(View):\n    async def get(self, num: int = Depends(original_dep)):\n        \"\"\"Nothing.\"\"\"\n        return web.json_response({\"request\": num})\n```\n\nImagine you have a handler that depends on some function,\nbut instead of `1` you want to have `2` in your tests.\n\nTo do it, jsut add `dependency_overrides` somewhere,\nwhere you create your application. And make sure that keys\nof that dict are actual function that are being replaced.\n\n```python\nfrom aiohttp_deps import VALUES_OVERRIDES_KEY\n\nmy_app[VALUES_OVERRIDES_KEY] = {original_dep: 2}\n```\n\nBut `values_overrides` only overrides returned values. If you want to\noverride functions, you have to use `dependency_overrides`. Here's an example:\n\n```python\nfrom aiohttp_deps import DEPENDENCY_OVERRIDES_KEY\n\ndef replacing_function() -\u003e int:\n    return 2\n\n\nmy_app[DEPENDENCY_OVERRIDES_KEY] = {original_dep: replacing_function}\n```\n\nThe cool point about `dependency_overrides`, is that it recalculates graph and\nyou can use dependencies in function that replaces the original.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaskiq-python%2Faiohttp-deps","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaskiq-python%2Faiohttp-deps","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaskiq-python%2Faiohttp-deps/lists"}