-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add module-level depencency containers, refactor logger
- Loading branch information
Showing
20 changed files
with
824 additions
and
196 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
from os import umask | ||
|
||
|
||
import uuid | ||
from starlette_context.plugins import Plugin | ||
|
||
|
||
class DependencyInjecionPlugin(Plugin): | ||
key = "container" | ||
class RequestContextPlugin(Plugin): | ||
key = "request_context" | ||
|
||
def __init__(self, container) -> None: | ||
super().__init__() | ||
self.container = container | ||
|
||
async def process_request(self, request): | ||
return self.container | ||
"""When processing a request, we set up a new request context""" | ||
context = self.container.request_context() | ||
context._correlation_id.set(uuid.uuid4()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pathlib import Path | ||
import sys | ||
|
||
# add base project path to PYTHONPATH | ||
BASE_DIR = Path(__file__).resolve().parent.parent | ||
sys.path.append(str(BASE_DIR)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from uuid import uuid4 | ||
from modules.catalog import catalog_container | ||
from modules.catalog.domain.repositories import SellerRepository | ||
from modules.catalog.application.queries import ( | ||
GetAllListingsQuery, | ||
GetListingsOfSellerQuery, | ||
) | ||
from seedwork.infrastructure.request_context import request_context | ||
from seedwork.infrastructure.logging import logger, LoggerFactory | ||
|
||
|
||
# a sample command line script to print all listings | ||
# run with "cd src && python -m cli" | ||
|
||
# configure logger prior to first usage | ||
LoggerFactory.configure(logger_name="cli") | ||
|
||
# configure catalog module | ||
catalog_container.config.from_dict(dict()) | ||
|
||
# instantiate catalog module | ||
catalog_module = catalog_container.module() | ||
|
||
logger.info("Application configured") | ||
|
||
# interact with a catalog module by issuing queries and commands | ||
# use request context if you want to logically separate queries/commands | ||
# from each other in the logs | ||
with request_context: | ||
query_result = catalog_module.execute_query(GetAllListingsQuery()) | ||
logger.info("All listings: %s", query_result.data) | ||
|
||
with request_context: | ||
seller_id = SellerRepository.next_id() | ||
query_result = catalog_module.execute_query( | ||
GetListingsOfSellerQuery(seller_id=seller_id) | ||
) | ||
logger.info(f"Listings of seller {seller_id} %s", query_result.data) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import uuid | ||
import logging | ||
from sqlalchemy import create_engine | ||
from dependency_injector import containers, providers | ||
|
||
from modules.catalog.infrastructure.persistence import MongoListingRepository | ||
from seedwork.infrastructure.request_context import RequestContext | ||
|
||
|
||
class DummyService: | ||
def __init__(self, config) -> None: | ||
self.config = config | ||
|
||
def serve(self): | ||
return f"serving with config {self.config}" | ||
|
||
|
||
class Container(containers.DeclarativeContainer): | ||
"""Dependency Injection Container | ||
see https://github.com/ets-labs/python-dependency-injector for more details | ||
""" | ||
|
||
config = providers.Configuration() | ||
engine = providers.Singleton(create_engine, config.DATABASE_URL, echo=config.DEBUG) | ||
dummy_service = providers.Factory(DummyService, config) | ||
dummy_singleton = providers.Singleton(DummyService, config) | ||
|
||
request_context = providers.Singleton(RequestContext) | ||
|
||
correlation_id = providers.Factory( | ||
lambda request_context: request_context.correlation_id.get(), request_context | ||
) | ||
listing_repository = providers.Factory(MongoListingRepository) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from modules.catalog.container import CatalogModuleContainer | ||
|
||
""" | ||
catalog_container is a composition root for a catalog module | ||
Use `catalog_container.module()` to create an instance of CatalogModule | ||
""" | ||
catalog_container = CatalogModuleContainer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
from seedwork.domain.value_objects import UUID | ||
from seedwork.application.queries import Query | ||
|
||
|
||
class GetAllListingsQuery(Query): | ||
... | ||
|
||
|
||
class GetListingsOfSellerQuery(Query): | ||
seller_id: UUID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
from seedwork.application.queries import Query | ||
from seedwork.application.query_handlers import QueryResult | ||
from seedwork.application.query_handlers import QueryHandler, QueryResult | ||
from seedwork.application.decorators import query_handler | ||
|
||
from ..application.queries import GetAllListingsQuery | ||
from ..application.queries import GetAllListingsQuery, GetListingsOfSellerQuery | ||
from ..domain.repositories import ListingRepository | ||
|
||
|
||
@query_handler | ||
def get_all_listings(query: GetAllListingsQuery, sql_connection) -> QueryResult: | ||
def get_all_listings(query: GetAllListingsQuery) -> QueryResult: | ||
sql = "SELECT * FROM listings" | ||
data = sql_connection.execute(sql) | ||
data = ["foo", "bar"] | ||
return QueryResult.ok(data) | ||
|
||
|
||
@query_handler | ||
def get_listings_of_seller(query: GetListingsOfSellerQuery) -> QueryHandler: | ||
data = ["foo"] | ||
return QueryResult.ok(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
Each module has it's own DI composition root | ||
""" | ||
|
||
from dependency_injector import containers, providers | ||
from sqlalchemy import create_engine | ||
from modules.catalog.module import CatalogModule | ||
from modules.catalog.infrastructure.persistence import MongoListingRepository | ||
|
||
|
||
class CatalogModuleContainer(containers.DeclarativeContainer): | ||
"""Dependency Injection Container | ||
see https://github.com/ets-labs/python-dependency-injector for more details | ||
""" | ||
|
||
config = providers.Configuration() | ||
engine = providers.Singleton(create_engine, config.DATABASE_URL, echo=config.DEBUG) | ||
listing_repository = providers.Factory(MongoListingRepository) | ||
module = providers.Singleton(CatalogModule) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from seedwork.domain.aggregates import Aggregate | ||
from seedwork.domain.value_objects import UUID | ||
|
||
from modules.catalog.domain.repositories import ListingRepository | ||
|
||
|
||
class MongoListingRepository(ListingRepository): | ||
def get_by_id(self, id: UUID) -> Aggregate: | ||
... | ||
|
||
def get_by_id(self, id: UUID) -> Aggregate: | ||
... | ||
|
||
def insert(self, entity: Aggregate): | ||
... | ||
|
||
def update(self, entity: Aggregate): | ||
... | ||
|
||
def delete(self, entity_id: UUID): | ||
... |
Oops, something went wrong.