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

Call an authenticate method before retrying #122

Closed
reisenberger opened this issue Jun 10, 2016 · 7 comments
Closed

Call an authenticate method before retrying #122

reisenberger opened this issue Jun 10, 2016 · 7 comments
Labels

Comments

@reisenberger
Copy link
Member

reisenberger commented Jun 10, 2016

@robertbaker asked under a separate issue (moving here as worth calling out as own issue!)

I have a scenario for web requests.

Say I make a httpClient call that throws a 403. On a 403, I want to
call my authenticate method first then retry the method.

I don't think this is possible currently with polly. If it is please give me an example!

@reisenberger
Copy link
Member Author

reisenberger commented Jun 10, 2016

Hey @robertbaker. This came up on StackOverflow recently, but I think the q has been deleted. It's very possible. Configure a policy with an onRetry delegate. Polly calls the onRetry delegate before the next try. In the onRetry delegate, call your authentication method.

The code at the top of #107 gives an example. [#107 uncovered an obscure compiler gotcha that led to an async sequencing issue, now fixed, but the code example matches your need I think!]

Let us know if you need any further assistance!

EDIT: The example in #107 is async, but you can also do this with sync policies.

@reisenberger
Copy link
Member Author

reisenberger commented Jun 10, 2016

@robertbaker If an additional issue is that HttpClient returns 403 as a status code rather than throws an exception, HttpResponseMessage.EnsureSuccessStatusCode() can be used to make that an exception, as described here. This provides a way of getting Polly to handle outcomes from HttpClient calls that might be either exceptions (eg timeouts) or status codes. There's a sketch of an async example using continuations here. EDIT: Or of course you can throw a specific exception only on 403 yourself.

We are looking at allowing Policies to handle return values as well as exceptions natively - see #14, and wider discussion in the Roadmap. Any feedback you have towards this feature would be welcome! Thanks.

UPDATE: The feature #14 is now delivered. The main Polly readme describes much more elegant ways of handling exceptions and HttpStatusCodes combied.

@ghost
Copy link

ghost commented Jun 13, 2016

Thanks, I'll try to implement this soon.

@reisenberger
Copy link
Member Author

reisenberger commented Jun 13, 2016

@robertbaker No problem. @PhilipAnthonyMurray was essentially working on sthg similar I think. We are doing the same thing in-house and achieved this pattern (refreshing authentication on retry) against both straight https authentication and SAML authentication: but the pattern isn't any more complicated than outlined above.

@reisenberger
Copy link
Member Author

@adamhathcock You raised OAuth and Polly on #90, see this thread #122. One pattern is to use the onRetry delegate (which gets executed between tries), so, in its simplest schematic form, something like:

var authorisationEnsuringPolicy = Policy
  .Handle<AuthorizationException>()
  .Retry(1, onRetry: (e, i) => RefreshAuthorization());

authorisationEnsuringPolicy.Execute(() => DoSomethingThatRequiresAuthorization());

@adamhathcock
Copy link

Thanks @reisenberger I was sure there was some way to do it without having to bring in some kind of new concept.

@reisenberger
Copy link
Member Author

Closing historic issue. Fully documented on wiki

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

No branches or pull requests

2 participants