python-dependency-injector/examples/miniapps/boto3-session/boto3_session_example.py

73 lines
2.0 KiB
Python
Raw Normal View History

2021-02-25 18:51:40 +03:00
"""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(),
2021-09-30 22:57:34 +03:00
service_name="s3",
2021-02-25 18:51:40 +03:00
)
sqs_client = providers.Resource(
providers.MethodCaller(session.provided.client), # Alternative syntax
2021-09-30 22:57:34 +03:00
service_name="sqs",
2021-02-25 18:51:40 +03:00
)
service1 = providers.Factory(
Service,
s3_client=s3_client,
sqs_client=sqs_client,
)
service2 = providers.Factory(
Service,
2021-09-30 22:57:34 +03:00
s3_client=session.provided.client.call(service_name="s3"), # Alternative inline syntax
sqs_client=session.provided.client.call(service_name="sqs"), # Alternative inline syntax
2021-02-25 18:51:40 +03:00
)
def main():
container = Container()
2021-09-30 22:57:34 +03:00
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")
2021-02-25 18:51:40 +03:00
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
2021-09-30 23:02:49 +03:00
service2 = container.service2()
2021-02-25 18:51:40 +03:00
print(service2, service2.s3_client, service2.sqs_client)
2021-09-30 23:02:49 +03:00
assert service2.s3_client.__class__.__name__ == "S3"
assert service2.sqs_client.__class__.__name__ == "SQS"
2021-02-25 18:51:40 +03:00
2021-09-30 22:57:34 +03:00
if __name__ == "__main__":
2021-02-25 18:51:40 +03:00
main()