Clean Architectures
with
PyConES 2021
October 3rd, 2021
We quickly and effectively detect and eliminate online piracy for digital assets
(movies, series, books) and counterfeited products offered in marketplaces,
search engines, social media platforms and apps
Smart Protection is a technological company
committed to fighting online piracy
User Portal
● A portal to interact with all
the services for a customer
● Need to be able to
communicate with different
data sources
● A scalable team and product
● Be agile, speed and iteration
are must
User Portal
The Product
User Portal
Presented today by
Alvaro del Castillo
alvaro.delcastillo@smartprotection.com
Clean
Architectures
● Main components (software artifacts) of the system (structure)
● The relationships (structural and behaviour) between the components
Similar to traditional architecture for buildings but software ones must be
designed for change
Clean Architectures
Software Architectures
«A client-server software architecture pattern in which the
user interface (presentation), functional process logic
(domain) and data access and storage are developed and
maintained as independent modules (Wikipedia).» The
dependency goes from top to bottom each layer just
depending on its bottom one.
What’s wrong with these layers? The data layer becomes
the core of the architecture (database model, tech …)
Clean Architectures
Classical Approach: Layered Architecture
Clean Architectures
Clean Architecture
The domain (policies) is in the core and
it does not depend on external layers. It
won’t change if something external
changes. Outer layers depend on Inner
layers.
Outer layers (mechanisms)
communicates with the core using
in/out ports (interfaces). FastAPI is
mechanism for us: it can be changed
without affecting the domain.
● The outer layers should point to the inner
layers
● The Business logic should not know about
the technology implementations of the
outer layers
● The ports are interfaces and the adapter
implementations
Clean Architectures
Hexagonal Architecture
Using in the
Infrastructure of an Hexagonal
Architecture
● ASGI (WSGI with async) based on Starlette
● Data validation (path, query params, body) with Pydantic
● Work with any web server with ASGI support like uvicorn
You define the endpoints of your remote APIs and link to the logic that
implements them. These are the routes. In the routes you have data models to
receive and deliver the data.
FastAPI
Web framework for building remote APIs
Based on the Scarlette ASGI framework, its performance is one of the
fastest Python frameworks available, and it is comparable with NodeJS.
From Scarlette it also inherits WebSocket support, GraphQL support,
in-process background tasks, session and cookie support
(middleware in general),
CORS, HTTP2 and others.
FastAPI
Async nature: Performance
Pydantic enforces type hints (Python) at runtime, and provides user friendly
errors when data is invalid. And it is fast compared to other validation libraries.
class User(BaseModel):
id: int
name = "Lisa Perez"
signup_ts: Optional[datetime] = None
friends: List[int] = []
class Config:
extra = 'forbid'
external_data = {
"id1": 1,
"id": 1,
"signup_ts": "2021-01-10 10:00",
"friends": []
}
user = User(**external_data)
The payload, path params and query params are validated and converted to
Python models automatically.
FastAPI
Pydantic Data Validation
Real time documentation in different
formats, like OpenAPI, using type hints
and default values and ranges!
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int = Path(None,
example="item_id_value"),
q: str = Query("query")):
return {"item_id": item_id, "q": q}
if __name__ == '__main__':
uvicorn.run('basic:app', host='0.0.0.0', reload=True)
FastAPI
Automatic documentation
"Dependency Injection" means, in programming, that there is a way for your
code (in this case, your path operation functions) to declare things that it
requires to work and use: "dependencies".
The dependencies, which can be nested, are resolved before calling the path
operation function. For example, for authorization before calling the endpoint:
router.add_api_route(path='/save/',
name='save',
methods=['POST'],
endpoint=certificates_api.save,
dependencies=[Depends(get_certificates_jwt_authorizer)],
status_code=status.HTTP_200_OK)
FastAPI
Dependency Injection
And ASGI introduces new features like Background Tasks!
Methods executed once the request is sent to the client.
async def save(
self,
request: Request,
background_tasks: BackgroundTask) -> None:
...
background_tasks.add_task(send_notification_use_case.execute,
notification_command)
FastAPI
Background Tasks
The routes call the path methods. All this logic is part of the API framework.
But the path methods calls the use cases, with the data from the requests
converted to the domain models. These use cases are in the application layer,
which works directly with the domain layer (all of them business logic).
The use cases returns data with domain models, which is converted with
Pydantic to a JSON payload which is sent to the client.
FastAPI
Inside the Hexagon Architecture
Showcase: User Portal
Reports
Assets
Content
Showcase: User Portal
Domains detection
Reports
Infrastructure
Application
Domain FastAPI
External
API
SQL
DB
User
Service
Repository
API
Showcase: User Portal
Hexagonal Architecture
● Team and product scalability
● Ready for change
● Technology independent
Clean Architectures
Benefits
The hexagonal architecture born as an evolution of layered architectures with the goal
of improving the isolation between policies (domain) and mechanisms (db). The
policies (business logic) became the core of the architecture and it does not depend in
how it is used (thanks to the dependency inversion).
FastAPI is an easy to use web framework for creating remote APIs, based on the
newest standards and reusing actual frameworks like Starlette and Pydantic. With it
you can create APIs pretty fast and ready to be moved to production.
Integrating FastAPI in our Hexagonal Architectures is pretty natural: it is just the
mechanism to offer remote APIs based on the domain business logic.
Using FastAPI in the Infrastructure of Hexagonal Architecture
Takeaways
Questions?
Thanks for attending!

Clean architectures with fast api pycones

  • 1.
  • 2.
    We quickly andeffectively detect and eliminate online piracy for digital assets (movies, series, books) and counterfeited products offered in marketplaces, search engines, social media platforms and apps Smart Protection is a technological company committed to fighting online piracy
  • 3.
  • 4.
    ● A portalto interact with all the services for a customer ● Need to be able to communicate with different data sources ● A scalable team and product ● Be agile, speed and iteration are must User Portal The Product
  • 5.
    User Portal Presented todayby Alvaro del Castillo alvaro.delcastillo@smartprotection.com
  • 6.
  • 7.
    ● Main components(software artifacts) of the system (structure) ● The relationships (structural and behaviour) between the components Similar to traditional architecture for buildings but software ones must be designed for change Clean Architectures Software Architectures
  • 8.
    «A client-server softwarearchitecture pattern in which the user interface (presentation), functional process logic (domain) and data access and storage are developed and maintained as independent modules (Wikipedia).» The dependency goes from top to bottom each layer just depending on its bottom one. What’s wrong with these layers? The data layer becomes the core of the architecture (database model, tech …) Clean Architectures Classical Approach: Layered Architecture
  • 9.
    Clean Architectures Clean Architecture Thedomain (policies) is in the core and it does not depend on external layers. It won’t change if something external changes. Outer layers depend on Inner layers. Outer layers (mechanisms) communicates with the core using in/out ports (interfaces). FastAPI is mechanism for us: it can be changed without affecting the domain.
  • 10.
    ● The outerlayers should point to the inner layers ● The Business logic should not know about the technology implementations of the outer layers ● The ports are interfaces and the adapter implementations Clean Architectures Hexagonal Architecture
  • 11.
    Using in the Infrastructureof an Hexagonal Architecture
  • 12.
    ● ASGI (WSGIwith async) based on Starlette ● Data validation (path, query params, body) with Pydantic ● Work with any web server with ASGI support like uvicorn You define the endpoints of your remote APIs and link to the logic that implements them. These are the routes. In the routes you have data models to receive and deliver the data. FastAPI Web framework for building remote APIs
  • 13.
    Based on theScarlette ASGI framework, its performance is one of the fastest Python frameworks available, and it is comparable with NodeJS. From Scarlette it also inherits WebSocket support, GraphQL support, in-process background tasks, session and cookie support (middleware in general), CORS, HTTP2 and others. FastAPI Async nature: Performance
  • 14.
    Pydantic enforces typehints (Python) at runtime, and provides user friendly errors when data is invalid. And it is fast compared to other validation libraries. class User(BaseModel): id: int name = "Lisa Perez" signup_ts: Optional[datetime] = None friends: List[int] = [] class Config: extra = 'forbid' external_data = { "id1": 1, "id": 1, "signup_ts": "2021-01-10 10:00", "friends": [] } user = User(**external_data) The payload, path params and query params are validated and converted to Python models automatically. FastAPI Pydantic Data Validation
  • 15.
    Real time documentationin different formats, like OpenAPI, using type hints and default values and ranges! app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int = Path(None, example="item_id_value"), q: str = Query("query")): return {"item_id": item_id, "q": q} if __name__ == '__main__': uvicorn.run('basic:app', host='0.0.0.0', reload=True) FastAPI Automatic documentation
  • 16.
    "Dependency Injection" means,in programming, that there is a way for your code (in this case, your path operation functions) to declare things that it requires to work and use: "dependencies". The dependencies, which can be nested, are resolved before calling the path operation function. For example, for authorization before calling the endpoint: router.add_api_route(path='/save/', name='save', methods=['POST'], endpoint=certificates_api.save, dependencies=[Depends(get_certificates_jwt_authorizer)], status_code=status.HTTP_200_OK) FastAPI Dependency Injection
  • 17.
    And ASGI introducesnew features like Background Tasks! Methods executed once the request is sent to the client. async def save( self, request: Request, background_tasks: BackgroundTask) -> None: ... background_tasks.add_task(send_notification_use_case.execute, notification_command) FastAPI Background Tasks
  • 18.
    The routes callthe path methods. All this logic is part of the API framework. But the path methods calls the use cases, with the data from the requests converted to the domain models. These use cases are in the application layer, which works directly with the domain layer (all of them business logic). The use cases returns data with domain models, which is converted with Pydantic to a JSON payload which is sent to the client. FastAPI Inside the Hexagon Architecture
  • 19.
  • 20.
  • 21.
  • 22.
    ● Team andproduct scalability ● Ready for change ● Technology independent Clean Architectures Benefits
  • 23.
    The hexagonal architectureborn as an evolution of layered architectures with the goal of improving the isolation between policies (domain) and mechanisms (db). The policies (business logic) became the core of the architecture and it does not depend in how it is used (thanks to the dependency inversion). FastAPI is an easy to use web framework for creating remote APIs, based on the newest standards and reusing actual frameworks like Starlette and Pydantic. With it you can create APIs pretty fast and ready to be moved to production. Integrating FastAPI in our Hexagonal Architectures is pretty natural: it is just the mechanism to offer remote APIs based on the domain business logic. Using FastAPI in the Infrastructure of Hexagonal Architecture Takeaways
  • 24.