Skip to content

6 Retry Policies

TimCodes.NET edited this page Dec 1, 2023 · 1 revision

Sometimes APIs fail, or can be rate-limited etc. You can specify how your API client should retry in different response scenarios by implementing an IRetryPolicy:

    bool ShouldRetry(IApiResponse response, int attempt);

    Task OnBeforeRetryAsync(IApiRequest request, IApiResponse response, string apiIdentifier);

This allows you to inspect an IApiResponse and the number of attempts already tried, and return whether or not the request should be retried.

Just before it is retried, your OnBeforeRetryAsync method will be called, allowing you to manipulate the request or response. In the case of the OpenIdRetryPolicy, it clears the access token from the cache when an Unauthorized response is received.

You could potentially use this feature to implement an exponential backoff retry policy.

To assign a retry policy, either use the ApiClient's DefaultRetryPolicies or the IApiRequests RetryPolicies.

Clone this wiki locally