Skip to content

Support Retries in the Generated Client #118

@bowenwr

Description

@bowenwr

Is your feature request related to a problem? Please describe.
It would be nice if the generated client could allow for retry hooks out of the box. For instance, currently when using requests we are able to hook into urllib3's retries to automatically retry certain failing status codes like temporary connection issues. Something like:

retries = Retry(
        total=5,
        method_whitelist=False,  # the default whitelist excludes PATCH, False retries on all HTTP methods
        backoff_factor=0.5,
        status_forcelist=[429,502,503,504]
    )

adapter = HTTPAdapter(max_retries=retries)
session = requests.Session()
session.mount("http://", adapter)
session.mount("https://", adapter)

response = session.request("get", "http://example.com/")

Describe the solution you'd like
It would be great if we could somewhat similarly hook into the underlying retry mechanisms of httpx.

I would be OK exposing the direct hooks to HTTPX's retries, or introducing an abstraction in case the underlying HTTP library changed.

I imagine generated method signatures might look like:

def get_endpoint(*, client: Client, retries: Optional[Retry] = None)

Alternatively, as a superset of retry functionality we could perhaps expose a factory to create the httpx session itself since that's how it performs retries and might allow for further configuration.

def get_endpoint(*, client: Client, session_factory: Optional[Callable[[], Session]] = None)

It could also make sense to expose this at the Client level instead since I imagine most uses cases would be satisfied with a common configuration.

Describe alternatives you've considered
Without hooks into the direct HTTP client itself, the best approximation is to catch ApiResponseErrors and evaluate the response to attempt a manual retry.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions