I want to use Polly not to check for overt "failures" but rather for other conditions. Specifically, I want to make a single (async) call, for example httpClient.GetAsync(...)
which for the purposes of this question I know will succeed - that is, after the execution of:
var response = await _myHttpRequestPolicy.ExecuteAsync(() => httpClient.GetAsync(uri));
response.IsSuccessStatusCode
will be true
.
Let's assume then I do the standard:
var content = await response.Content.ReadAsStringAsync();
and
content == { "Name":"Tom", "Age", 30", "ErrorCode":"12345" }
I want my policy logic to execute based on the contents (or absence or presence of) the ErrorCode
portion of the response. So it's just a single call I'm making.
How can I do this with Polly?
You're configuring a policy to guard the HttpClient GetAsync method, which returns
Task<HttpResponseMessage>
. You want to configure aPolicy<HttpResponseMessage>
to work with this method, using async handlers.Policy<T>.HandleResult(Func<T, bool> filter)
allows you to look at the HttpResponseMessage and determine whether you want to handle that result.A couple of options. One, you could figure out deserializing/reading the HttpResponseMessage's json payload within the HandleResult method. You only get a
Func<HttpResponseMessage, bool>
to work with. This would need to happen synchronously, as adding async/await changes the return type to Task.Second, you could apply the policy at a higher level. Get the response as you are httpclient.GetAsync(uri), then deserialize the content. Maybe have one
Policy<HttpResponseMessage>
wrap the httpclient call, and onePolicy<MyAbstractApiResponse>
to look for the custom error code after deserializing?As a note, an API error should really be picked up by the IsSuccessStatusCode property on the HttpResponseMessage. Your REST api (is it yours? that's an assumption) should be setting status codes appropriate to the error, not solely 200's and custom response properties.
Related further reading: Check string content of response before retrying with Polly
Update:
I didn't look at real method names or syntax in putting that together so it might not compile. Hopefully you get the idea I'm trying to convey, one policy is called from within another.