mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 01:47:36 +03:00 
			
		
		
		
	Add `boto3` example
				
					
				
			This commit is contained in:
		
							parent
							
								
									3cf14c139f
								
							
						
					
					
						commit
						9788a1888f
					
				| 
						 | 
				
			
			@ -155,6 +155,7 @@ Choose one of the following:
 | 
			
		|||
- `Application example (single container) <https://python-dependency-injector.ets-labs.org/examples/application-single-container.html>`_
 | 
			
		||||
- `Application example (multiple containers) <https://python-dependency-injector.ets-labs.org/examples/application-multiple-containers.html>`_
 | 
			
		||||
- `Decoupled packages example (multiple containers) <https://python-dependency-injector.ets-labs.org/examples/decoupled-packages.html>`_
 | 
			
		||||
- `Boto3 example <https://python-dependency-injector.ets-labs.org/examples/boto3.html>`_
 | 
			
		||||
- `Django example <https://python-dependency-injector.ets-labs.org/examples/django.html>`_
 | 
			
		||||
- `Flask example <https://python-dependency-injector.ets-labs.org/examples/flask.html>`_
 | 
			
		||||
- `Aiohttp example <https://python-dependency-injector.ets-labs.org/examples/aiohttp.html>`_
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										20
									
								
								docs/examples/boto3.rst
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								docs/examples/boto3.rst
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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 <https://www.djangoproject.com/>`_.
 | 
			
		||||
 | 
			
		||||
The source code is available on the `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/boto3-session>`_.
 | 
			
		||||
 | 
			
		||||
Listing of ``boto3_session_example.py``:
 | 
			
		||||
 | 
			
		||||
.. literalinclude:: ../../examples/miniapps/boto3-session/boto3_session_example.py
 | 
			
		||||
   :language: python
 | 
			
		||||
 | 
			
		||||
.. disqus::
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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`
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 <https://github.com/ets-labs/python-dependency-injector/issues/406>`_.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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`
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										14
									
								
								examples/miniapps/boto3-session/README.rst
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								examples/miniapps/boto3-session/README.rst
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
Boto3 Session Example
 | 
			
		||||
=====================
 | 
			
		||||
 | 
			
		||||
This is a `Boto3 <https://boto3.amazonaws.com/v1/documentation/api/latest/index.html>`_ session +
 | 
			
		||||
`Dependency Injector <https://python-dependency-injector.ets-labs.org/>`_ example.
 | 
			
		||||
 | 
			
		||||
Run
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
To run the application do:
 | 
			
		||||
 | 
			
		||||
.. code-block:: bash
 | 
			
		||||
 | 
			
		||||
    python boto3_session_example.py
 | 
			
		||||
							
								
								
									
										72
									
								
								examples/miniapps/boto3-session/boto3_session_example.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								examples/miniapps/boto3-session/boto3_session_example.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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()
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user