Skip to content

Commit

Permalink
fix: httpx client & handler & router
Browse files Browse the repository at this point in the history
  • Loading branch information
ijaric committed Sep 30, 2023
1 parent 8f2065e commit 9888f0e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/fastapi_app/lib/api/v1/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .health import *

__all__ = [
"health_router",
"basic_router",
]
4 changes: 2 additions & 2 deletions src/fastapi_app/lib/api/v1/handlers/health/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .liveness_probe import router as health_router
from .liveness_probe import basic_router

__all__ = [
"health_router",
"basic_router",
]
4 changes: 2 additions & 2 deletions src/fastapi_app/lib/api/v1/handlers/health/liveness_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import lib.api.v1.schemas as api_shemas

router = fastapi.APIRouter()
basic_router = fastapi.APIRouter()


@router.get(
@basic_router.get(
"/",
response_model=api_shemas.HealthResponseModel,
summary="Статус работоспособности",
Expand Down
7 changes: 4 additions & 3 deletions src/fastapi_app/lib/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def from_settings(cls, settings: app_settings.Settings) -> typing.Self:

logger.info("Initializing global clients")
postgres_client = clients.AsyncPostgresClient(settings=settings)
http_client = clients.get_async_http_session()

disposable_resources.append(
DisposableResource(
Expand Down Expand Up @@ -72,10 +73,10 @@ def from_settings(cls, settings: app_settings.Settings) -> typing.Self:
# Handlers

logger.info("Initializing handlers")
# liveness_probe_handler = health_handlers.LivenessProbeHandler()
liveness_probe_handler = api_v1_handlers.basic_router


logger.info("Creating application")
# aio_app = aiohttp_web.Application()

fastapi_app = fastapi.FastAPI(
title=settings.app.title,
Expand All @@ -86,7 +87,7 @@ def from_settings(cls, settings: app_settings.Settings) -> typing.Self:
)

# Routes
fastapi_app.include_router(api_v1_handlers.health_router, prefix="/api/v1/health", tags=["health"])
fastapi_app.include_router(liveness_probe_handler, prefix="/api/v1/health", tags=["health"])

application = Application(
settings=settings,
Expand Down
3 changes: 2 additions & 1 deletion src/fastapi_app/lib/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .httpx import get_async_http_session
from .postgres import AsyncPostgresClient

__all__ = ["AsyncPostgresClient"]
__all__ = ["AsyncPostgresClient", "get_async_http_session"]
10 changes: 7 additions & 3 deletions src/fastapi_app/lib/clients/httpx.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Purpose: Provide an example of an async http client for the application
import contextlib
import typing

import httpx


@contextlib.asynccontextmanager
async def get_http_client() -> typing.AsyncGenerator[httpx.AsyncClient, None]:
client = httpx.AsyncClient() # Insert your own settings here
async def get_async_http_session(
settings: dict[str, typing.Any] | None = None
) -> typing.AsyncGenerator[httpx.AsyncClient, None]:
"""Async http client."""
if settings is None:
settings = {}
client = httpx.AsyncClient(**settings) # Insert your own settings here
async with client as ac:
yield ac

0 comments on commit 9888f0e

Please sign in to comment.