Skip to content

Commit 9788a18

Browse files
committed
Add boto3 example
1 parent 3cf14c1 commit 9788a18

File tree

8 files changed

+111
-0
lines changed

8 files changed

+111
-0
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Choose one of the following:
155155
- `Application example (single container) <https://python-dependency-injector.ets-labs.org/examples/application-single-container.html>`_
156156
- `Application example (multiple containers) <https://python-dependency-injector.ets-labs.org/examples/application-multiple-containers.html>`_
157157
- `Decoupled packages example (multiple containers) <https://python-dependency-injector.ets-labs.org/examples/decoupled-packages.html>`_
158+
- `Boto3 example <https://python-dependency-injector.ets-labs.org/examples/boto3.html>`_
158159
- `Django example <https://python-dependency-injector.ets-labs.org/examples/django.html>`_
159160
- `Flask example <https://python-dependency-injector.ets-labs.org/examples/flask.html>`_
160161
- `Aiohttp example <https://python-dependency-injector.ets-labs.org/examples/aiohttp.html>`_

docs/examples/boto3.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.. _boto3-example:
2+
3+
Boto3 example
4+
=============
5+
6+
.. meta::
7+
:keywords: Python,Dependency Injection,Boto3,AWS,Amazon Web Services,S3,SQS,Rout53,EC2,Lambda,Example
8+
:description: This example demonstrates a usage of Boto3 AWS client and Dependency Injector.
9+
10+
11+
This example shows how to use ``Dependency Injector`` with `Boto3 <https://www.djangoproject.com/>`_.
12+
13+
The source code is available on the `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/boto3-session>`_.
14+
15+
Listing of ``boto3_session_example.py``:
16+
17+
.. literalinclude:: ../../examples/miniapps/boto3-session/boto3_session_example.py
18+
:language: python
19+
20+
.. disqus::

docs/examples/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Explore the examples to see the ``Dependency Injector`` in action.
1313
application-single-container
1414
application-multiple-containers
1515
decoupled-packages
16+
boto3
1617
django
1718
flask
1819
flask-blueprints

docs/introduction/di_in_python.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ Choose one of the following as a next step:
281281
- :ref:`application-single-container`
282282
- :ref:`application-multiple-containers`
283283
- :ref:`decoupled-packages`
284+
- :ref:`boto3`
284285
- :ref:`django-example`
285286
- :ref:`flask-example`
286287
- :ref:`flask-blueprints-example`

docs/main/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ follows `Semantic versioning`_
99

1010
Development version
1111
-------------------
12+
- Add ``boto3`` example.
1213
- Add tests for ``.as_float()`` modifier usage with wiring.
1314
- Make refactoring of wiring module and tests.
1415
See PR # `#406 <https://github.com/ets-labs/python-dependency-injector/issues/406>`_.

docs/wiring.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ Take a look at other application examples:
405405
- :ref:`application-single-container`
406406
- :ref:`application-multiple-containers`
407407
- :ref:`decoupled-packages`
408+
- :ref:`boto3`
408409
- :ref:`django-example`
409410
- :ref:`flask-example`
410411
- :ref:`flask-blueprints-example`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Boto3 Session Example
2+
=====================
3+
4+
This is a `Boto3 <https://boto3.amazonaws.com/v1/documentation/api/latest/index.html>`_ session +
5+
`Dependency Injector <https://python-dependency-injector.ets-labs.org/>`_ example.
6+
7+
Run
8+
---
9+
10+
To run the application do:
11+
12+
.. code-block:: bash
13+
14+
python boto3_session_example.py
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""Boto3 session example."""
2+
3+
import boto3.session
4+
from dependency_injector import containers, providers
5+
6+
7+
class Service:
8+
def __init__(self, s3_client, sqs_client):
9+
self.s3_client = s3_client
10+
self.sqs_client = sqs_client
11+
12+
13+
class Container(containers.DeclarativeContainer):
14+
15+
config = providers.Configuration()
16+
17+
session = providers.Resource(
18+
boto3.session.Session,
19+
aws_access_key_id=config.aws_access_key_id,
20+
aws_secret_access_key=config.aws_secret_access_key,
21+
aws_session_token=config.aws_session_token,
22+
)
23+
24+
s3_client = providers.Resource(
25+
session.provided.client.call(),
26+
service_name='s3',
27+
)
28+
29+
sqs_client = providers.Resource(
30+
providers.MethodCaller(session.provided.client), # Alternative syntax
31+
service_name='sqs',
32+
)
33+
34+
service1 = providers.Factory(
35+
Service,
36+
s3_client=s3_client,
37+
sqs_client=sqs_client,
38+
)
39+
40+
service2 = providers.Factory(
41+
Service,
42+
s3_client=session.provided.client.call(service_name='s3'), # Alternative inline syntax
43+
sqs_client=session.provided.client.call(service_name='sqs'), # Alternative inline syntax
44+
)
45+
46+
47+
def main():
48+
container = Container()
49+
container.config.aws_access_key_id.from_env('AWS_ACCESS_KEY_ID')
50+
container.config.aws_secret_access_key.from_env('AWS_SECRET_ACCESS_KEY')
51+
container.config.aws_session_token.from_env('AWS_SESSION_TOKEN')
52+
container.init_resources()
53+
54+
s3_client = container.s3_client()
55+
print(s3_client)
56+
57+
sqs_client = container.sqs_client()
58+
print(sqs_client)
59+
60+
service1 = container.service1()
61+
print(service1, service1.s3_client, service1.sqs_client)
62+
assert service1.s3_client is s3_client
63+
assert service1.sqs_client is sqs_client
64+
65+
service2 = container.service1()
66+
print(service2, service2.s3_client, service2.sqs_client)
67+
assert service2.s3_client is s3_client
68+
assert service2.sqs_client is sqs_client
69+
70+
71+
if __name__ == '__main__':
72+
main()

0 commit comments

Comments
 (0)