Update FastAPI example

This commit is contained in:
Roman Mogylatov 2020-11-17 23:46:30 -05:00
parent d37ae8e7db
commit 950d5a5e6e
4 changed files with 32 additions and 6 deletions

View File

@ -10,6 +10,7 @@ follows `Semantic versioning`_
Develop Develop
------- -------
- Improve ``FastAPI`` integration: handle ``Depends(Provide[...])``. - Improve ``FastAPI`` integration: handle ``Depends(Provide[...])``.
- Update ``FastAPI`` example.
4.4.0 4.4.0
----- -----

View File

@ -113,9 +113,9 @@ The output should be something like:
giphynavigator/__init__.py 0 0 100% giphynavigator/__init__.py 0 0 100%
giphynavigator/application.py 13 0 100% giphynavigator/application.py 13 0 100%
giphynavigator/containers.py 6 0 100% giphynavigator/containers.py 6 0 100%
giphynavigator/endpoints.py 6 0 100% giphynavigator/endpoints.py 20 0 100%
giphynavigator/giphy.py 14 9 36% giphynavigator/giphy.py 14 9 36%
giphynavigator/services.py 9 1 89% giphynavigator/services.py 9 1 89%
giphynavigator/tests.py 38 0 100% giphynavigator/tests.py 38 0 100%
--------------------------------------------------- ---------------------------------------------------
TOTAL 86 10 88% TOTAL 100 10 90%

View File

@ -14,7 +14,7 @@ def create_app() -> FastAPI:
app = FastAPI() app = FastAPI()
app.container = container app.container = container
app.add_api_route('/', endpoints.index) app.include_router(endpoints.router)
return app return app

View File

@ -1,17 +1,42 @@
"""Endpoints module.""" """Endpoints module."""
from typing import Optional, List
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from dependency_injector.wiring import inject, Provide from dependency_injector.wiring import inject, Provide
from .services import SearchService
from .containers import Container from .containers import Container
class Gif(BaseModel):
url: str
class Response(BaseModel):
query: str
limit: int
gifs: List[Gif]
router = APIRouter()
@router.get('/', response_model=Response)
@inject @inject
async def index( async def index(
query: str = Provide[Container.config.default.query], query: Optional[str] = None,
limit: int = Provide[Container.config.default.limit.as_int()], limit: Optional[str] = None,
search_service=Provide[Container.search_service], default_query: str = Depends(Provide[Container.config.default.query]),
default_limit: int = Depends(Provide[Container.config.default.limit.as_int()]),
search_service: SearchService = Depends(Provide[Container.search_service]),
): ):
query = query or default_query
limit = limit or default_limit
gifs = await search_service.search(query, limit) gifs = await search_service.search(query, limit)
return { return {
'query': query, 'query': query,
'limit': limit, 'limit': limit,