{"id":313350,"date":"2020-11-18T09:01:00","date_gmt":"2020-11-18T09:01:00","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=313350"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=313350","title":{"rendered":"FastAPI + Dependency Injector"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/a93\/37f\/080\/a9337f080e8bc14b3f425debb5c756ae.svg\"><\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0442,<\/p>\n<p>  \u042f \u0432\u044b\u043f\u0443\u0441\u0442\u0438\u043b \u043d\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e <a href=\"https:\/\/github.com\/ets-labs\/python-dependency-injector\" rel=\"nofollow\">Dependency Injector<\/a> 4.4. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Dependency Injector \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 <a href=\"https:\/\/fastapi.tiangolo.com\/\" rel=\"nofollow\">FastAPI<\/a>. \u0412 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u0442\u0435 \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438: \u043f\u043e\u0434\u0440\u0443\u0436\u0438\u0442\u044c \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u0443 <code>Depends<\/code> FastAPI c \u043c\u0430\u0440\u043a\u0435\u0440\u0430\u043c\u0438 <code>Provide<\/code> \u0438 <code>Provider<\/code> Dependency Injector&#8217;a.<\/p>\n<p>  \u0418\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438 \u0434\u043e \u0432\u0435\u0440\u0441\u0438\u0438 DI 4.4 \u044d\u0442\u043e \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e. FastAPI \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u044e \u0438 Pydantic \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0438 \u043e\u0442\u0432\u0435\u0442\u0430. \u041c\u0430\u0440\u043a\u0435\u0440\u044b Dependency Injector&#8217;\u0430 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043b\u0438 \u0435\u0433\u043e \u0432 \u043d\u0435\u0434\u043e\u0443\u043c\u0435\u043d\u0438\u0435.<\/p>\n<p>  \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u0448\u043b\u043e \u043f\u043e\u0441\u043b\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 FastAPI. \u041f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u043d\u0438\u044f (wiring) Dependency Injector&#8217;\u0430. \u0414\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u0430 <code>Depends<\/code> \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043c\u0430\u0440\u043a\u0435\u0440\u0430\u043c\u0438 <code>Provide<\/code> \u0438 <code>Provider<\/code>.<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440<\/h3>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0444\u0430\u0439\u043b <code>fastapi_di_example.py<\/code> \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u0435 \u0432 \u043d\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<pre><code class=\"python\">import sys  from fastapi import FastAPI, Depends from dependency_injector import containers, providers from dependency_injector.wiring import inject, Provide   class Service:     async def process(self) -&gt; str:         return 'Ok'   class Container(containers.DeclarativeContainer):      service = providers.Factory(Service)   app = FastAPI()   @app.api_route('\/') @inject async def index(service: Service = Depends(Provide[Container.service])):     result = await service.process()     return {'result': result}   container = Container() container.wire(modules=[sys.modules[__name__]]) <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"bash\">pip install fastapi dependency-injector uvicorn <\/code><\/pre>\n<p>  \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 <code>uvicorn<\/code>:<\/p>\n<pre><code class=\"bash\">uvicorn fastapi_di_example:app --reload <\/code><\/pre>\n<p>  \u0412 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435:<\/p>\n<pre><code class=\"bash\">INFO:     Uvicorn running on http:\/\/127.0.0.1:8000 (Press CTRL+C to quit) INFO:     Started reloader process [11910] using watchgod INFO:     Started server process [11912] INFO:     Waiting for application startup. INFO:     Application startup complete. <\/code><\/pre>\n<p>  \u0430 <code>http:\/\/127.0.0.1:8000<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c:<\/p>\n<pre><code class=\"bash\">{     &quot;result&quot;: &quot;Ok&quot; } <\/code><\/pre>\n<h3>\u041a\u0430\u043a \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c?<\/h3>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u0440\u044f\u0434\u043e\u043c \u0444\u0430\u0439\u043b <code>tests.py<\/code> \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u0435 \u0432 \u043d\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<pre><code class=\"python\">from unittest import mock  import pytest from httpx import AsyncClient  from fastapi_di_example import app, container, Service   @pytest.fixture def client(event_loop):     client = AsyncClient(app=app, base_url='http:\/\/test')     yield client     event_loop.run_until_complete(client.aclose())   @pytest.mark.asyncio async def test_index(client):     service_mock = mock.AsyncMock(spec=Service)     service_mock.process.return_value = 'Foo'      with container.service.override(service_mock):         response = await client.get('\/')      assert response.status_code == 200     assert response.json() == {'result': 'Foo'} <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u0441\u0442\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"bash\">pip install pytest pytest-asyncio httpx <\/code><\/pre>\n<p>  \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 <code>pytest<\/code>:<\/p>\n<pre><code class=\"bash\">pytest tests.py <\/code><\/pre>\n<p>  \u0412 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"bash\">======= test session starts ======= platform darwin -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: ... plugins: asyncio-0.14.0 collected 1 item  tests.py .                                                      [100%]  ======= 1 passed in 0.17s ======= <\/code><\/pre>\n<p>  <\/p>\n<h3>\u0427\u0442\u043e \u0434\u0430\u0435\u0442 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f?<\/h3>\n<p>  FastAPI \u2014 \u043a\u043b\u0430\u0441\u0441\u043d\u044b\u0439 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f API. \u0412 \u043d\u0435\u0433\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c dependency injection.<\/p>\n<p>  \u042d\u0442\u0430 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 dependency injection \u0432 FastAPI. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0451\u043c \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043d\u0438\u0435, \u043a\u043e\u043d\u0444\u0438\u0433 \u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u044b Dependency Injector&#8217;\u0430.<\/p>\n<h3>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435?<\/h3>\n<p>  <\/p>\n<ul>\n<li>\u0417\u0430\u0433\u043b\u044f\u043d\u0438 \u043d\u0430 <a href=\"https:\/\/github.com\/ets-labs\/python-dependency-injector\" rel=\"nofollow\">Github \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/a><\/li>\n<li>\u0417\u0430\u0433\u043b\u044f\u043d\u0438 \u0432 <a href=\"http:\/\/python-dependency-injector.ets-labs.org\/\" rel=\"nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a><\/li>\n<li>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438 <a href=\"https:\/\/python-dependency-injector.ets-labs.org\/examples\/fastapi.html\" rel=\"nofollow\">\u043f\u0440\u0438\u043c\u0435\u0440 FastAPI \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438<\/a><\/li>\n<\/ul>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/528634\/\"> https:\/\/habr.com\/ru\/post\/528634\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/a93\/37f\/080\/a9337f080e8bc14b3f425debb5c756ae.svg\"><\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0442,<\/p>\n<p>  \u042f \u0432\u044b\u043f\u0443\u0441\u0442\u0438\u043b \u043d\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e <a href=\"https:\/\/github.com\/ets-labs\/python-dependency-injector\" rel=\"nofollow\">Dependency Injector<\/a> 4.4. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Dependency Injector \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 <a href=\"https:\/\/fastapi.tiangolo.com\/\" rel=\"nofollow\">FastAPI<\/a>. \u0412 \u044d\u0442\u043e\u043c \u043f\u043e\u0441\u0442\u0435 \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-313350","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313350"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313350\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}