This is how I ended up doing it, after I found out that Polly actually catches exceptions, and therefore is compatible with HttpUtils:
Define the retry policy, in this example we only catch 429, then wait for a relatively long time (a bit randomized) before retrying.
var delay = Backoff
.DecorrelatedJitterBackoffV2(
medianFirstRetryDelay: TimeSpan.FromSeconds(10),
retryCount
);
policyS = Policy
.Handle<HttpRequestException>(ex => ex.Message.Contains("429"))
.WaitAndRetry(
delay,
(result, timespan, retryNo, context) => {
Console.WriteLine($"{context.OperationKey}: Retry number {retryNo} within " +
$"{timespan.TotalMilliseconds}ms. Original status code: 429");
}
);
The actual API call is here:
var response = policyS.Execute(ctx => {
var response2 = lookupUrl.GetJsonFromUrl(
requestFilter: x => {
x.AddHeader("x-authorization", $"Bearer {ccToken}");
});
return response2.FromJson<LookupDTO>();
}, pollyContext);