Update services miniapp

This commit is contained in:
Roman Mogilatov 2016-05-18 23:18:29 +03:00
parent 3247e79af4
commit 075e1bcf4f
8 changed files with 178 additions and 170 deletions

View File

@ -55,20 +55,20 @@ Installation
Example
-------
Brief example below demonstrates usage of *Dependency Injector* catalogs and
providers for definition of several IoC containers for some microservice
system that consists from several business and platform services:
.. code-block:: python
"""Dependency Injector example."""
"""Example of several Dependency Injector catalogs."""
import sys
import sqlite3
from boto.s3.connection import S3Connection
import boto.s3.connection
import example.services
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
from example import services
class Platform(catalogs.DeclarativeCatalog):
@ -76,7 +76,7 @@ Example
database = providers.Singleton(sqlite3.connect, ':memory:')
s3 = providers.Singleton(S3Connection,
s3 = providers.Singleton(boto.s3.connection.S3Connection,
aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
@ -84,32 +84,69 @@ Example
class Services(catalogs.DeclarativeCatalog):
"""Catalog of business service providers."""
users = providers.Factory(services.Users,
users = providers.Factory(example.services.Users,
db=Platform.database)
photos = providers.Factory(services.Photos,
photos = providers.Factory(example.services.Photos,
db=Platform.database,
s3=Platform.s3)
auth = providers.Factory(services.Auth,
auth = providers.Factory(example.services.Auth,
db=Platform.database,
token_ttl=3600)
Next example demonstrates usage of these IoC containers with the help of
``@inject`` decorator:
@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
@injections.inject(photos_service=Services.photos)
def main(argv, users_service, auth_service, photos_service):
.. code-block:: python
"""Dependency Injector example."""
from dependency_injector.injections import inject
from catalogs import Services
@inject(users_service=Services.users)
@inject(auth_service=Services.auth)
@inject(photos_service=Services.photos)
def main(login, password, photo, users_service, auth_service, photos_service):
"""Main function."""
login, password, photo_path = argv[1:]
user = users_service.get_user(login)
auth_service.authenticate(user, password)
photos_service.upload_photo(user['id'], photo_path)
photos_service.upload_photo(user['id'], photo)
if __name__ == '__main__':
main(sys.argv)
main(login='user', password='secret', photo='photo.jpg')
Also some alternative definition styles could be used. Like this one:
.. code-block:: python
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect) \
.args(':memory:')
s3 = providers.Singleton(boto.s3.connection.S3Connection) \
.kwargs(aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
or like this one:
.. code-block:: python
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect)
database.args(':memory:')
s3 = providers.Singleton(boto.s3.connection.S3Connection)
s3.kwargs(aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
You can get more *Dependency Injector* examples in ``/examples`` directory on
GitHub:

View File

@ -5,4 +5,10 @@ Instructions for running:
.. code-block:: bash
python main.py dependency-injector secret myself.jpg
python main.py
.. note::
``catalogs_alt_style_1.py`` and ``catalogs_alt_style_2.py`` contain
examples of atlernative catalogs definition styles.

View File

@ -0,0 +1,33 @@
"""Example of several Dependency Injector catalogs."""
import sqlite3
import boto.s3.connection
import example.services
from dependency_injector import catalogs
from dependency_injector import providers
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect, ':memory:')
s3 = providers.Singleton(boto.s3.connection.S3Connection,
aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
class Services(catalogs.DeclarativeCatalog):
"""Catalog of business service providers."""
users = providers.Factory(example.services.Users,
db=Platform.database)
photos = providers.Factory(example.services.Photos,
db=Platform.database,
s3=Platform.s3)
auth = providers.Factory(example.services.Auth,
db=Platform.database,
token_ttl=3600)

View File

@ -0,0 +1,37 @@
"""Example of several Dependency Injector catalogs.
Alternative definition style #1.
"""
import sqlite3
import boto.s3.connection
import example.services
from dependency_injector import catalogs
from dependency_injector import providers
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect) \
.args(':memory:')
s3 = providers.Singleton(boto.s3.connection.S3Connection) \
.kwargs(aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
class Services(catalogs.DeclarativeCatalog):
"""Catalog of business service providers."""
users = providers.Factory(example.services.Users) \
.kwargs(db=Platform.database)
photos = providers.Factory(example.services.Photos) \
.kwargs(db=Platform.database,
s3=Platform.s3)
auth = providers.Factory(example.services.Auth) \
.kwargs(db=Platform.database,
token_ttl=3600)

View File

@ -0,0 +1,37 @@
"""Example of several Dependency Injector catalogs.
Alternative definition style #2.
"""
import sqlite3
import boto.s3.connection
import example.services
from dependency_injector import catalogs
from dependency_injector import providers
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect)
database.args(':memory:')
s3 = providers.Singleton(boto.s3.connection.S3Connection)
s3.kwargs(aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
class Services(catalogs.DeclarativeCatalog):
"""Catalog of business service providers."""
users = providers.Factory(example.services.Users)
users.kwargs(db=Platform.database)
photos = providers.Factory(example.services.Photos)
photos.kwargs(db=Platform.database,
s3=Platform.s3)
auth = providers.Factory(example.services.Auth)
auth.kwargs(db=Platform.database,
token_ttl=3600)

View File

@ -1,53 +1,19 @@
"""Dependency Injector example."""
import sys
import sqlite3
from dependency_injector.injections import inject
from boto.s3.connection import S3Connection
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
from example import services
from catalogs import Services
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect, ':memory:')
s3 = providers.Singleton(S3Connection,
aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
class Services(catalogs.DeclarativeCatalog):
"""Catalog of business service providers."""
users = providers.Factory(services.Users,
db=Platform.database)
photos = providers.Factory(services.Photos,
db=Platform.database,
s3=Platform.s3)
auth = providers.Factory(services.Auth,
db=Platform.database,
token_ttl=3600)
@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
@injections.inject(photos_service=Services.photos)
def main(argv, users_service, auth_service, photos_service):
@inject(users_service=Services.users)
@inject(auth_service=Services.auth)
@inject(photos_service=Services.photos)
def main(login, password, photo, users_service, auth_service, photos_service):
"""Main function."""
login, password, photo_path = argv[1:]
user = users_service.get_user(login)
auth_service.authenticate(user, password)
photos_service.upload_photo(user['id'], photo_path)
photos_service.upload_photo(user['id'], photo)
if __name__ == '__main__':
main(sys.argv)
main(login='user', password='secret', photo='photo.jpg')

View File

@ -1,54 +0,0 @@
"""Dependency Injector example."""
import sys
import sqlite3
from boto.s3.connection import S3Connection
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
from example import services
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect)
database.args(':memory:')
s3 = providers.Singleton(S3Connection)
s3.kwargs(aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
class Services(catalogs.DeclarativeCatalog):
"""Catalog of business service providers."""
users = providers.Factory(services.Users)
users.kwargs(db=Platform.database)
photos = providers.Factory(services.Photos)
photos.kwargs(db=Platform.database,
s3=Platform.s3)
auth = providers.Factory(services.Auth)
auth.kwargs(db=Platform.database,
token_ttl=3600)
@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
@injections.inject(photos_service=Services.photos)
def main(argv, users_service, auth_service, photos_service):
"""Main function."""
login, password, photo_path = argv[1:]
user = users_service.get_user(login)
auth_service.authenticate(user, password)
photos_service.upload_photo(user['id'], photo_path)
if __name__ == '__main__':
main(sys.argv)

View File

@ -1,54 +0,0 @@
"""Dependency Injector example."""
import sys
import sqlite3
from boto.s3.connection import S3Connection
from dependency_injector import catalogs
from dependency_injector import providers
from dependency_injector import injections
from example import services
class Platform(catalogs.DeclarativeCatalog):
"""Catalog of platform service providers."""
database = providers.Singleton(sqlite3.connect) \
.args(':memory:')
s3 = providers.Singleton(S3Connection) \
.kwargs(aws_access_key_id='KEY',
aws_secret_access_key='SECRET')
class Services(catalogs.DeclarativeCatalog):
"""Catalog of business service providers."""
users = providers.Factory(services.Users) \
.kwargs(db=Platform.database)
photos = providers.Factory(services.Photos) \
.kwargs(db=Platform.database,
s3=Platform.s3)
auth = providers.Factory(services.Auth) \
.kwargs(db=Platform.database,
token_ttl=3600)
@injections.inject(users_service=Services.users)
@injections.inject(auth_service=Services.auth)
@injections.inject(photos_service=Services.photos)
def main(argv, users_service, auth_service, photos_service):
"""Main function."""
login, password, photo_path = argv[1:]
user = users_service.get_user(login)
auth_service.authenticate(user, password)
photos_service.upload_photo(user['id'], photo_path)
if __name__ == '__main__':
main(sys.argv)