python-dependency-injector/examples/miniapps/fastapi/giphynavigator/endpoints.py

45 lines
1006 B
Python
Raw Permalink Normal View History

2020-11-13 01:48:07 +03:00
"""Endpoints module."""
2020-11-18 07:46:30 +03:00
from typing import Optional, List
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from dependency_injector.wiring import inject, Provide
2020-11-13 01:48:07 +03:00
2020-11-18 07:46:30 +03:00
from .services import SearchService
2020-11-13 01:48:07 +03:00
from .containers import Container
2020-11-18 07:46:30 +03:00
class Gif(BaseModel):
url: str
class Response(BaseModel):
query: str
limit: int
gifs: List[Gif]
router = APIRouter()
2021-09-30 23:55:50 +03:00
@router.get("/", response_model=Response)
@inject
2020-11-13 01:48:07 +03:00
async def index(
2020-11-18 07:46:30 +03:00
query: Optional[str] = None,
limit: Optional[str] = None,
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]),
2020-11-13 01:48:07 +03:00
):
2020-11-18 07:46:30 +03:00
query = query or default_query
limit = limit or default_limit
2020-11-13 01:48:07 +03:00
gifs = await search_service.search(query, limit)
2020-11-18 07:46:30 +03:00
2020-11-13 01:48:07 +03:00
return {
2021-09-30 23:55:50 +03:00
"query": query,
"limit": limit,
"gifs": gifs,
2020-11-13 01:48:07 +03:00
}