Skip to content

Commit

Permalink
replace seedwork Application with lato Application
Browse files Browse the repository at this point in the history
  • Loading branch information
pgorecki committed Mar 21, 2024
1 parent 53b7041 commit de8e330
Show file tree
Hide file tree
Showing 54 changed files with 1,527 additions and 671 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.10.0
3.11
2 changes: 1 addition & 1 deletion diary.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_route_controller(request, module):
def post_route_controller(request, module):
result = module.execute_command(MyCommand(
result = module.execute(MyCommand(
foo=request.POST.foo,
bar=request.POST.bar,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_route_controller(request, module):
def post_route_controller(request, module):
result = module.execute_command(MyCommand(
result = module.execute(MyCommand(
foo=request.POST.foo,
bar=request.POST.bar,
))
Expand Down
1,568 changes: 1,183 additions & 385 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ authors = ["Przemysław Górecki <przemyslaw.gorecki@gmail.com>"]

[tool.poetry.dependencies]
python = "^3.10.0"
pytest = "^6.2.4"
pydantic = "^1.8.2"
black = "^21.5b1"
fastapi = "^0.95.2"
uvicorn = "^0.14.0"
starlette-context = "^0.3.3"
SQLAlchemy = "^1.4.22"
Expand All @@ -29,6 +26,9 @@ httpx = "^0.23.1"
requests = "^2.28.1"
bcrypt = "^4.0.1"
mypy = "^1.4.1"
fastapi = "^0.110.0"
lato = "^0.10.0"
pydantic-settings = "^2.2.1"

[tool.poetry.dev-dependencies]
poethepoet = "^0.10.0"
Expand Down
4 changes: 2 additions & 2 deletions src/api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from modules.iam.application.services import IamService
from modules.iam.domain.entities import User
from seedwork.application import Application, TransactionContext
from lato import Application, TransactionContext

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

Expand All @@ -28,5 +28,5 @@ async def get_authenticated_user(
access_token: Annotated[str, Depends(oauth2_scheme)],
ctx: Annotated[TransactionContext, Depends(get_transaction_context)],
) -> User:
current_user = ctx.get_service(IamService).find_user_by_access_token(access_token)
current_user = ctx[IamService].find_user_by_access_token(access_token)
return current_user
19 changes: 10 additions & 9 deletions src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from api.dependencies import oauth2_scheme # noqa
from api.routers import bidding, catalog, diagnostics, iam
from config.api_config import ApiConfig
from config.container import TopLevelContainer
from config.container import create_application, ApplicationContainer
from seedwork.domain.exceptions import DomainException, EntityNotFoundException
from seedwork.infrastructure.database import Base
from seedwork.infrastructure.logging import LoggerFactory, logger
Expand All @@ -15,29 +15,30 @@
LoggerFactory.configure(logger_name="api")

# dependency injection container
container = TopLevelContainer()
container.config.from_pydantic(ApiConfig())
config = ApiConfig()
container = ApplicationContainer(config=config)
db_engine = container.db_engine()
logger.info(f"using db engine {db_engine}, creating tables")
Base.metadata.create_all(db_engine)
logger.info("setup complete")

app = FastAPI(debug=container.config.DEBUG)
app = FastAPI(debug=config.DEBUG)

app.include_router(catalog.router)
app.include_router(bidding.router)
app.include_router(iam.router)
app.include_router(diagnostics.router)
app.container = container

db_engine = container.db_engine()
logger.info(f"using db engine {db_engine}, creating tables")
Base.metadata.create_all(db_engine)
logger.info("setup complete")


try:
import uuid

from modules.iam.application.services import IamService

with app.container.application().transaction_context() as ctx:
iam_service = ctx.get_service(IamService)
iam_service = ctx[IamService]
iam_service.create_user(
user_id=uuid.UUID(int=1),
email="user1@example.com",
Expand Down
9 changes: 9 additions & 0 deletions src/api/models/bidding.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ class BidReadModel(BaseModel):
currency: str
bidder_id: GenericUUID
bidder_username: str

class Config:
arbitrary_types_allowed = True


class BiddingResponse(BaseModel):
listing_id: GenericUUID
auction_status: str = "active" # active, ended
auction_end_date: datetime
bids: list[BidReadModel]

class Config:
arbitrary_types_allowed = True


class PlaceBidRequest(BaseModel):
bidder_id: GenericUUID
amount: float

class Config:
arbitrary_types_allowed = True
24 changes: 11 additions & 13 deletions src/api/routers/bidding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from config.container import inject
from modules.bidding.application.command import PlaceBidCommand, RetractBidCommand
from modules.bidding.application.query.get_bidding_details import GetBiddingDetails
from seedwork.application import Application
from lato import Application

router = APIRouter()

Expand All @@ -25,12 +25,11 @@ async def get_bidding_details_of_listing(
Shows listing details
"""
query = GetBiddingDetails(listing_id=listing_id)
query_result = app.execute_query(query)
payload = query_result.payload
result = app.execute(query)
return BiddingResponse(
listing_id=str(payload.id),
auction_end_date=payload.ends_at,
bids=payload.bids,
listing_id=result.id,
auction_end_date=result.ends_at,
bids=result.bids,
)


Expand All @@ -53,15 +52,14 @@ async def place_bid(
bidder_id=request_body.bidder_id,
amount=request_body.amount,
)
result = app.execute_command(command)
app.execute(command)

query = GetBiddingDetails(listing_id=listing_id)
query_result = app.execute_query(query)
payload = query_result.payload
result = app.execute(query)
return BiddingResponse(
listing_id=str(payload.id),
auction_end_date=payload.ends_at,
bids=payload.bids,
listing_id=result.id,
auction_end_date=result.ends_at,
bids=result.bids,
)


Expand All @@ -81,7 +79,7 @@ async def retract_bid(
listing_id=listing_id,
bidder_id="",
)
app.execute_command(command)
app.execute(command)

query = GetBiddingDetails(listing_id=listing_id)
query_result = app.execute_query(query)
Expand Down
16 changes: 8 additions & 8 deletions src/api/routers/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

@router.get("/catalog", tags=["catalog"], response_model=ListingIndexModel)
@inject
async def get_all_listings(app: Annotated[Application, Depends(get_application)]):
def get_all_listings(app: Annotated[Application, Depends(get_application)]):
"""
Shows all published listings in the catalog
"""
query = GetAllListings()
query_result = app.execute_query(query)
return dict(data=query_result.payload)
result = app.execute(query)
return dict(data=result)


@router.get("/catalog/{listing_id}", tags=["catalog"], response_model=ListingReadModel)
Expand Down Expand Up @@ -64,7 +64,7 @@ async def create_listing(
ask_price=Money(request_body.ask_price_amount, request_body.ask_price_currency),
seller_id=current_user.id,
)
app.execute_command(command)
app.execute(command)

query = GetListingDetails(listing_id=command.listing_id)
query_result = app.execute_query(query)
Expand All @@ -87,7 +87,7 @@ async def delete_listing(
listing_id=listing_id,
seller_id=current_user.id,
)
app.execute_command(command)
app.execute(command)


@router.post(
Expand All @@ -109,8 +109,8 @@ async def publish_listing(
listing_id=listing_id,
seller_id=current_user.id,
)
app.execute_command(command)
app.execute(command)

query = GetListingDetails(listing_id=listing_id)
query_result = app.execute_query(query)
return dict(query_result.payload)
response = app.execute(query)
return response
12 changes: 6 additions & 6 deletions src/api/routers/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from fastapi import APIRouter, Depends

from api.dependencies import (
TransactionContext,
Application,
User,
get_authenticated_user,
get_transaction_context,
get_application,
)

from .iam import UserResponse
Expand All @@ -16,13 +16,13 @@

@router.get("/debug", tags=["diagnostics"])
async def debug(
ctx: Annotated[TransactionContext, Depends(get_transaction_context)],
app: Annotated[Application, Depends(get_application)],
current_user: Annotated[User, Depends(get_authenticated_user)],
):
return dict(
app_id=id(ctx.app),
name=ctx.app.name,
version=ctx.app.version,
app_id=id(app),
name=app.name,
version=app["app_version"],
user=UserResponse(
id=str(current_user.id),
username=current_user.username,
Expand Down
2 changes: 1 addition & 1 deletion src/api/routers/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def login(
form_data: OAuth2PasswordRequestForm = Depends(),
) -> LoginResponse:
try:
iam_service = ctx.get_service(IamService)
iam_service = ctx[IamService]
user = iam_service.authenticate_with_name_and_password(
form_data.username, form_data.password
)
Expand Down
7 changes: 4 additions & 3 deletions src/api/tests/test_bidding.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def setup_app_for_bidding_tests(app, listing_id, seller_id, bidder_id):
logger.info("Adding users")
with app.transaction_context() as ctx:
iam_service = ctx.dependency_provider["iam_service"]
iam_service = ctx["iam_service"]

iam_service.create_user(
user_id=seller_id,
Expand All @@ -27,7 +27,7 @@ def setup_app_for_bidding_tests(app, listing_id, seller_id, bidder_id):
)

logger.info("Adding listing")
app.execute_command(
app.execute(
CreateListingDraftCommand(
listing_id=listing_id,
title="Foo",
Expand All @@ -36,7 +36,7 @@ def setup_app_for_bidding_tests(app, listing_id, seller_id, bidder_id):
seller_id=seller_id,
)
)
app.execute_command(
app.execute(
PublishListingDraftCommand(listing_id=listing_id, seller_id=seller_id)
)
logger.info("test setup complete")
Expand All @@ -52,4 +52,5 @@ def test_place_bid(app, api_client):
url = f"/bidding/{listing_id}/place_bid"

response = api_client.post(url, json={"bidder_id": str(bidder_id), "amount": 11})
json = response.json()
assert response.status_code == 200
14 changes: 7 additions & 7 deletions src/api/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_empty_catalog_list(api_client):
@pytest.mark.integration
def test_catalog_list_with_one_item(app, api_client):
# arrange
app.execute_command(
app.execute(
CreateListingDraftCommand(
listing_id=GenericUUID(int=1),
title="Foo",
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_catalog_list_with_one_item(app, api_client):
@pytest.mark.integration
def test_catalog_list_with_two_items(app, api_client):
# arrange
app.execute_command(
app.execute(
CreateListingDraftCommand(
listing_id=GenericUUID(int=1),
title="Foo #1",
Expand All @@ -59,7 +59,7 @@ def test_catalog_list_with_two_items(app, api_client):
seller_id=GenericUUID(int=2),
)
)
app.execute_command(
app.execute(
CreateListingDraftCommand(
listing_id=GenericUUID(int=2),
title="Foo #2",
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_catalog_create_draft_fails_due_to_incomplete_data(
@pytest.mark.integration
def test_catalog_delete_draft(app, authenticated_api_client):
current_user = authenticated_api_client.current_user
app.execute_command(
app.execute(
CreateListingDraftCommand(
listing_id=GenericUUID(int=1),
title="Listing to be deleted",
Expand All @@ -115,7 +115,7 @@ def test_catalog_publish_listing_draft(app, authenticated_api_client):
# arrange
current_user = authenticated_api_client.current_user
listing_id = GenericUUID(int=1)
app.execute_command(
app.execute(
CreateListingDraftCommand(
listing_id=listing_id,
title="Listing to be published",
Expand All @@ -136,7 +136,7 @@ def test_published_listing_appears_in_biddings(app, authenticated_api_client):
# arrange
listing_id = GenericUUID(int=1)
current_user = authenticated_api_client.current_user
app.execute_command(
app.execute(
CreateListingDraftCommand(
listing_id=listing_id,
title="Listing to be published",
Expand All @@ -145,7 +145,7 @@ def test_published_listing_appears_in_biddings(app, authenticated_api_client):
seller_id=current_user.id,
)
)
app.execute_command(
app.execute(
PublishListingDraftCommand(
listing_id=listing_id,
seller_id=current_user.id,
Expand Down
6 changes: 3 additions & 3 deletions src/api/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def test_login_with_api_token(app, api_client):
# arrange
with app.transaction_context() as ctx:
iam_service = ctx.get_service(IamService)
iam_service = ctx[IamService]
iam_service.create_user(
user_id=GenericUUID(int=1),
email="admin@example.com",
Expand All @@ -31,7 +31,7 @@ def test_login_with_api_token(app, api_client):
def test_login_with_invalid_username_returns_400(app, api_client):
# arrange
with app.transaction_context() as ctx:
iam_service = ctx.get_service(IamService)
iam_service = ctx[IamService]
iam_service.create_user(
user_id=GenericUUID(int=1),
email="admin@example.com",
Expand All @@ -53,7 +53,7 @@ def test_login_with_invalid_username_returns_400(app, api_client):
def test_login_with_invalid_password_returns_400(app, api_client):
# arrange
with app.transaction_context() as ctx:
iam_service = ctx.get_service(IamService)
iam_service = ctx[IamService]
iam_service.create_user(
user_id=GenericUUID(int=1),
email="admin@example.com",
Expand Down
Loading

0 comments on commit de8e330

Please sign in to comment.