Update front example

This commit is contained in:
Roman Mogylatov 2021-09-29 16:30:46 -04:00
parent 316799ae6d
commit 43a1a6cf39
6 changed files with 34 additions and 35 deletions

View File

@ -80,7 +80,7 @@ Key features of the ``Dependency Injector``:
.. code-block:: python .. code-block:: python
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide from dependency_injector.wiring import Provide, inject
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
@ -104,11 +104,11 @@ Key features of the ``Dependency Injector``:
... ...
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
container.config.api_key.from_env('API_KEY') container.config.api_key.from_env("API_KEY")
container.config.timeout.from_env('TIMEOUT') container.config.timeout.from_env("TIMEOUT")
container.wire(modules=[sys.modules[__name__]]) container.wire(modules=[__name__])
main() # <-- dependency is injected automatically main() # <-- dependency is injected automatically

View File

@ -86,7 +86,7 @@ Key features of the ``Dependency Injector``:
.. code-block:: python .. code-block:: python
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide from dependency_injector.wiring import Provide, inject
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
@ -110,11 +110,11 @@ Key features of the ``Dependency Injector``:
... ...
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
container.config.api_key.from_env('API_KEY') container.config.api_key.from_env("API_KEY")
container.config.timeout.from_env('TIMEOUT') container.config.timeout.from_env("TIMEOUT")
container.wire(modules=[sys.modules[__name__]]) container.wire(modules=[__name__])
main() # <-- dependency is injected automatically main() # <-- dependency is injected automatically

View File

@ -67,8 +67,8 @@ Before:
class ApiClient: class ApiClient:
def __init__(self): def __init__(self):
self.api_key = os.getenv('API_KEY') # <-- dependency self.api_key = os.getenv("API_KEY") # <-- dependency
self.timeout = os.getenv('TIMEOUT') # <-- dependency self.timeout = os.getenv("TIMEOUT") # <-- dependency
class Service: class Service:
@ -82,7 +82,7 @@ Before:
... ...
if __name__ == '__main__': if __name__ == "__main__":
main() main()
After: After:
@ -109,12 +109,12 @@ After:
... ...
if __name__ == '__main__': if __name__ == "__main__":
main( main(
service=Service( service=Service(
api_client=ApiClient( api_client=ApiClient(
api_key=os.getenv('API_KEY'), api_key=os.getenv("API_KEY"),
timeout=os.getenv('TIMEOUT'), timeout=os.getenv("TIMEOUT"),
), ),
), ),
) )
@ -136,8 +136,8 @@ Now you need to assemble and inject the objects like this:
main( main(
service=Service( service=Service(
api_client=ApiClient( api_client=ApiClient(
api_key=os.getenv('API_KEY'), api_key=os.getenv("API_KEY"),
timeout=os.getenv('TIMEOUT'), timeout=os.getenv("TIMEOUT"),
), ),
), ),
) )
@ -162,7 +162,7 @@ the dependency.
.. code-block:: python .. code-block:: python
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide from dependency_injector.wiring import Provide, inject
class Container(containers.DeclarativeContainer): class Container(containers.DeclarativeContainer):
@ -186,11 +186,11 @@ the dependency.
... ...
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
container.config.api_key.from_env('API_KEY') container.config.api_key.from_env("API_KEY")
container.config.timeout.from_env('TIMEOUT') container.config.timeout.from_env("TIMEOUT")
container.wire(modules=[sys.modules[__name__]]) container.wire(modules=[__name__])
main() # <-- dependency is injected automatically main() # <-- dependency is injected automatically

View File

@ -18,12 +18,12 @@ def main(service: Service): # <-- dependency is injected
... ...
if __name__ == '__main__': if __name__ == "__main__":
main( main(
service=Service( service=Service(
api_client=ApiClient( api_client=ApiClient(
api_key=os.getenv('API_KEY'), api_key=os.getenv("API_KEY"),
timeout=os.getenv('TIMEOUT'), timeout=os.getenv("TIMEOUT"),
), ),
), ),
) )

View File

@ -4,8 +4,8 @@ import os
class ApiClient: class ApiClient:
def __init__(self): def __init__(self):
self.api_key = os.getenv('API_KEY') # <-- dependency self.api_key = os.getenv("API_KEY") # <-- dependency
self.timeout = os.getenv('TIMEOUT') # <-- dependency self.timeout = os.getenv("TIMEOUT") # <-- dependency
class Service: class Service:
@ -19,5 +19,5 @@ def main() -> None:
... ...
if __name__ == '__main__': if __name__ == "__main__":
main() main()

View File

@ -1,8 +1,7 @@
import sys
from unittest import mock from unittest import mock
from dependency_injector import containers, providers from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide from dependency_injector.wiring import Provide, inject
from after import ApiClient, Service from after import ApiClient, Service
@ -28,11 +27,11 @@ def main(service: Service = Provide[Container.service]):
... ...
if __name__ == '__main__': if __name__ == "__main__":
container = Container() container = Container()
container.config.api_key.from_env('API_KEY') container.config.api_key.from_env("API_KEY")
container.config.timeout.from_env('TIMEOUT') container.config.timeout.from_env("TIMEOUT")
container.wire(modules=[sys.modules[__name__]]) container.wire(modules=[__name__])
main() # <-- dependency is injected automatically main() # <-- dependency is injected automatically