0

I would like to create a custom asynchronous Httpclient class while implementing the above code in a fastapi api which needs to type 2 or 3 other api.

As mentioned and explained in the answer in the link below, I'm going to integrate it into a fastapi app and use lifespan to manage the httpclient lifecycle.

what-is-the-proper-way-to-make-downstream-https-requests-inside-of-uvicorn-fastapi

The advantage of the class is that I can create a python package to share with my colleagues, preconfigure some default parameters and contain custom methods such as (get_users , get_tenants etc...).

Simple example:

from httpx import AsyncClient, Response
from typing import Any

class CustomAsyncClient():
    def __init__(
        self,
        base_url: str = "https://my-custom-api.example.com",
        client_kwargs: dict[str, Any] | None = None
    ):
        self.client: AsyncClient = AsyncClient(base_url=base_url, **client_kwargs)

    async def close_client(self) -> bool:
        await self.client.aclose()
        return self.client.is_closed

    async def getTenants(self) -> Response:
        pass

    async def getUsers(self) -> Response:
        pass

How to implement this correctly and cleanly ?

Thanks in advance

expected --> A clean custom asynchronous Httpclient class with custom methods and parameters

Walid
  • 1
  • 1

0 Answers0