openapi-python-client
openapi-python-client copied to clipboard
Support Retries in the Generated Client
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 ApiResponseError
s and evaluate the response to attempt a manual retry.
So the httpx team seems to have been going back and forth on a retry API for some time. I think we should wait on adding our own retry functionality until they iron it out so we can take advantage of it. Latest issue tracking it is https://github.com/encode/httpx/issues/1141 though this keeps changing.
It sure seems like they'll have it in place for 1.0 which they're moving pretty quickly toward.
Thanks for the follow-up, I understand if httpx
is a moving target on retry functionality it's worth waiting on.
Looks like this might be possible now via https://github.com/encode/httpcore/pull/221 , but I don't see any clear documentation on how to do it. Anyone is free to give it a shot if they want. Also, it'll be easier to do manually (without inbuilt support here) once #202 is done.
I found in the documentation that retries (only connection retries) can be used via Custom Transports: https://www.python-httpx.org/advanced/#custom-transports
>>> import httpx
>>> transport = httpx.HTTPTransport(retries=1)
>>> client = httpx.Client(transport=transport)
Looking at the changelog, the minimum version to support this is httpx >= 0.17.1