I'm using Refit, with Polly and Polly.Extensions.Http in an ASP.NET Core Web API to call some external APIs.
I'm trying to add a policy handler using Polly which can retry depending on the body content as the external API I am working with, doesn't use HTTP Status codes to return operation results, but instead puts operation results in a common returned body object. So it will always return 200 OK but then it could indicate it failed with a body that has Success
= false
and some errors in the Errors
property. Here is the response object properties that are always returned:
public abstract class ApiResultResponse
{
public string RequestId { get; set; }
public List<string> Warnings { get; set; }
public List<Error> Errors { get; set; }
public bool Success { get; set; }
}
Trying to add an asynchronous retry policy here using the AddPolicyHandler
method call.
But I am not sure how to properly create the retryPolicy object with Polly that will create the desired functionality.
services.AddRefitClient<ISomeApi>(refitSettings)
.ConfigureHttpClient(c => {
c.BaseAddress = someApiBaseUrl;
})
.AddPolicyHandler(retryPolicy)
I want to add this functionality to a bunch of APIs as one common situation is that calling these APIs may result in rate limiting failures, but I need to evaluate the ApiResultResponse
object Errors
property to determine if the cause was rate limiting for the failure. I want to handle these at a global level and automatically retry, and not handle it downstream in services.