aiohttp_retry
aiohttp_retry copied to clipboard
Feature request: Custom callback to evaluate response
I like this library, however I'm missing a feature to evaluate the response in custom callback. There are APIs, that return by default 200 OK with errors description in response payload, e.g. graphql. So to be able to retry graphql client errors, I would like to have an option to specify custom callback to check the response.
Proposal would be:
- if custom callback is specified, run it first, before checking status code
- the callback should return
boolto indicate the response is ok/not ok and needs to retried.
Thoughts? I could also help with the implementation. Thanks
@adamko147 Hi!
I think this feature is usefull, but it, definetely, should be implemented in aiohttp package. If I got you right, it's not about retries, it about handling request. I think it should look like this. You specify callback for aiohttp, if response is wrong, you raise an exception. aiohttp-retry handle this exception and do retry.
I'm not sure if you can do callback in aiohttp. If not, it is definitely a good proposal for them.
Hi @inyutin, thanks for the reply. I think this is somewhere in the middle between aiohttp and retry lib. The reason I proposed this here is because I think it's a bit closer to the retry lib part. It's about possibility to specify what would be reason for retry. Could be http status code, exception or a custom callback provided, which I think could be very useful in in dealing with situation where none of the first two works well. I've tried to play around aiohttp trace configs to raise an exception, but since it depends on response content, it's more complicated - you need to use response_chunk callbacks and those are anyway called after the request is done, so raising an exception there won't trigger the retry library. Therefore I think the easier and simple would be implementation in retry-lib.
My idea was sth like this
async def check_response(response):
body = await response.json()
if 'errors' in body:
return False
return True
retry_options = ExponentialRetry(callback=check_reponse)
async with RetryClient(retry_options=retry_options) as client:
async with client.get('/url') as res:
body = await res.json()
pretty simple and straightforward ;) Thanks!
@adamko147 it's added! Look at this commit https://github.com/inyutin/aiohttp_retry/commit/5f51f6aa248b4a0d86e80b7a8ecf147d6f6e70b3 There you can find an example