From 9788a1888f3ea1a2ba7f992a2153faa253261640 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Thu, 25 Feb 2021 10:51:40 -0500 Subject: [PATCH] Add ``boto3`` example --- README.rst | 1 + docs/examples/boto3.rst | 20 ++++++ docs/examples/index.rst | 1 + docs/introduction/di_in_python.rst | 1 + docs/main/changelog.rst | 1 + docs/wiring.rst | 1 + examples/miniapps/boto3-session/README.rst | 14 ++++ .../boto3-session/boto3_session_example.py | 72 +++++++++++++++++++ 8 files changed, 111 insertions(+) create mode 100644 docs/examples/boto3.rst create mode 100644 examples/miniapps/boto3-session/README.rst create mode 100644 examples/miniapps/boto3-session/boto3_session_example.py diff --git a/README.rst b/README.rst index 6eba4ce6..f6f27bbc 100644 --- a/README.rst +++ b/README.rst @@ -155,6 +155,7 @@ Choose one of the following: - `Application example (single container) `_ - `Application example (multiple containers) `_ - `Decoupled packages example (multiple containers) `_ +- `Boto3 example `_ - `Django example `_ - `Flask example `_ - `Aiohttp example `_ diff --git a/docs/examples/boto3.rst b/docs/examples/boto3.rst new file mode 100644 index 00000000..8dc4bb6b --- /dev/null +++ b/docs/examples/boto3.rst @@ -0,0 +1,20 @@ +.. _boto3-example: + +Boto3 example +============= + +.. meta:: + :keywords: Python,Dependency Injection,Boto3,AWS,Amazon Web Services,S3,SQS,Rout53,EC2,Lambda,Example + :description: This example demonstrates a usage of Boto3 AWS client and Dependency Injector. + + +This example shows how to use ``Dependency Injector`` with `Boto3 `_. + +The source code is available on the `Github `_. + +Listing of ``boto3_session_example.py``: + +.. literalinclude:: ../../examples/miniapps/boto3-session/boto3_session_example.py + :language: python + +.. disqus:: diff --git a/docs/examples/index.rst b/docs/examples/index.rst index 6ec55c23..93595380 100644 --- a/docs/examples/index.rst +++ b/docs/examples/index.rst @@ -13,6 +13,7 @@ Explore the examples to see the ``Dependency Injector`` in action. application-single-container application-multiple-containers decoupled-packages + boto3 django flask flask-blueprints diff --git a/docs/introduction/di_in_python.rst b/docs/introduction/di_in_python.rst index 23ba547e..f19455e5 100644 --- a/docs/introduction/di_in_python.rst +++ b/docs/introduction/di_in_python.rst @@ -281,6 +281,7 @@ Choose one of the following as a next step: - :ref:`application-single-container` - :ref:`application-multiple-containers` - :ref:`decoupled-packages` + - :ref:`boto3` - :ref:`django-example` - :ref:`flask-example` - :ref:`flask-blueprints-example` diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 5204dc35..2b9830f5 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -9,6 +9,7 @@ follows `Semantic versioning`_ Development version ------------------- +- Add ``boto3`` example. - Add tests for ``.as_float()`` modifier usage with wiring. - Make refactoring of wiring module and tests. See PR # `#406 `_. diff --git a/docs/wiring.rst b/docs/wiring.rst index 195f469e..d4e2d6a6 100644 --- a/docs/wiring.rst +++ b/docs/wiring.rst @@ -405,6 +405,7 @@ Take a look at other application examples: - :ref:`application-single-container` - :ref:`application-multiple-containers` - :ref:`decoupled-packages` +- :ref:`boto3` - :ref:`django-example` - :ref:`flask-example` - :ref:`flask-blueprints-example` diff --git a/examples/miniapps/boto3-session/README.rst b/examples/miniapps/boto3-session/README.rst new file mode 100644 index 00000000..4c66ef92 --- /dev/null +++ b/examples/miniapps/boto3-session/README.rst @@ -0,0 +1,14 @@ +Boto3 Session Example +===================== + +This is a `Boto3 `_ session + +`Dependency Injector `_ example. + +Run +--- + +To run the application do: + +.. code-block:: bash + + python boto3_session_example.py diff --git a/examples/miniapps/boto3-session/boto3_session_example.py b/examples/miniapps/boto3-session/boto3_session_example.py new file mode 100644 index 00000000..33277513 --- /dev/null +++ b/examples/miniapps/boto3-session/boto3_session_example.py @@ -0,0 +1,72 @@ +"""Boto3 session example.""" + +import boto3.session +from dependency_injector import containers, providers + + +class Service: + def __init__(self, s3_client, sqs_client): + self.s3_client = s3_client + self.sqs_client = sqs_client + + +class Container(containers.DeclarativeContainer): + + config = providers.Configuration() + + session = providers.Resource( + boto3.session.Session, + aws_access_key_id=config.aws_access_key_id, + aws_secret_access_key=config.aws_secret_access_key, + aws_session_token=config.aws_session_token, + ) + + s3_client = providers.Resource( + session.provided.client.call(), + service_name='s3', + ) + + sqs_client = providers.Resource( + providers.MethodCaller(session.provided.client), # Alternative syntax + service_name='sqs', + ) + + service1 = providers.Factory( + Service, + s3_client=s3_client, + sqs_client=sqs_client, + ) + + service2 = providers.Factory( + Service, + s3_client=session.provided.client.call(service_name='s3'), # Alternative inline syntax + sqs_client=session.provided.client.call(service_name='sqs'), # Alternative inline syntax + ) + + +def main(): + container = Container() + container.config.aws_access_key_id.from_env('AWS_ACCESS_KEY_ID') + container.config.aws_secret_access_key.from_env('AWS_SECRET_ACCESS_KEY') + container.config.aws_session_token.from_env('AWS_SESSION_TOKEN') + container.init_resources() + + s3_client = container.s3_client() + print(s3_client) + + sqs_client = container.sqs_client() + print(sqs_client) + + service1 = container.service1() + print(service1, service1.s3_client, service1.sqs_client) + assert service1.s3_client is s3_client + assert service1.sqs_client is sqs_client + + service2 = container.service1() + print(service2, service2.s3_client, service2.sqs_client) + assert service2.s3_client is s3_client + assert service2.sqs_client is sqs_client + + +if __name__ == '__main__': + main()