Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to override the execution order of certain class-level and method-level response handlers #120

Open
prkumar opened this issue Nov 23, 2018 · 1 comment

Comments

@prkumar
Copy link
Owner

prkumar commented Nov 23, 2018

Is your feature request related to a problem? Please describe.
Here's the original question raised by @liiight on gitter:

so i have a class response handler to handle request errors, which basically just does raise_for_status()
I have another response handler that I want to use in order to retry 404 status code via a retry lib I use
I set the 2nd response handler directly on the relevant method but it seems that the 1st one is the one that actually catches the exception
is there a way to decide on the order of those?

Describe the solution you'd like
There should be a way to specify that a particular method-level response handler should run before any or certain class-level handlers.

Additional context
Here's my response to the original question:

currently, class-level decorators run before method-level decorators, as you noticed in your usage. #72 (v0.4.1) details some of the rationale for this. Currently, Uplink doesn't give you a way to decide on the order between class-level and method-level decorators. From what I can tell, there are two existing workarounds, but both have drawbacks. First, you could make the retry response handler a class-level decorator. If you don't want all methods to be retried, the other workaround is to apply the raise_for_status decorator on each method, but this makes things more verbose.

@prkumar prkumar changed the title Override the execution order of certain class-level and method-level response handlers Allow users to override the execution order of certain class-level and method-level response handlers Nov 23, 2018
@prkumar prkumar changed the title Allow users to override the execution order of certain class-level and method-level response handlers Allow a way to override the execution order of certain class-level and method-level response handlers Nov 23, 2018
@prkumar prkumar added this to the v0.7.0 milestone Nov 23, 2018
@prkumar prkumar changed the title Allow a way to override the execution order of certain class-level and method-level response handlers Add a way to override the execution order of certain class-level and method-level response handlers Nov 23, 2018
@prkumar prkumar modified the milestones: v0.7.0, v0.8.0 Dec 7, 2018
@prkumar prkumar pinned this issue Jan 28, 2019
@prkumar prkumar modified the milestones: v0.8.0, v0.9.0 Feb 21, 2019
@prkumar prkumar modified the milestones: v0.9.0, v0.10.0 Apr 12, 2019
@cognifloyd
Copy link
Contributor

cognifloyd commented Jul 6, 2019

Here's my use-case for this feature:
I have a class level annotation that raises errors when status >= 400. But, I just discovered that one of the APIs also returns 404 when it should be returning an empty list, so I want to check for 404 in a method-level annotation before the class-level 400+ check happens.
This is the ugly handler that I wanted to apply to only one method (while still allowing it to be covered by the class level handler). Instead, I had to move the class level handler down and decorate every method with it since class level runs before method level.

@response_handler
def returns_empty_list_on_404(response):
    # ugly hack to handle stupid api
    if response.status_code == 404:
        response.status_code = 200
        response._content = '[]'
    return response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants