Replay an HttpCall From Flurl

815 Views Asked by At

I'm writing an error handler to handle token refreshes. When I get an expired_token error I'm refreshing the token and I'd like to replay the request but I'm not sure of how

public async Task HandleErrorAsync(HttpCall call)
{
      var exception = call.Exception;
      if (exception is FlurlHttpException)
      {
         FlurlHttpException ex = (exception as FlurlHttpException);
         var errorResponse = await ex.GetResponseJsonAsync<ErrorResponse>();

         if(errorResponse.Errors.Any(x => x.Id == EXPIRED_TOKEN))
         {
             await this.RefreshOAuthToken();
             //How can I Replay the request
             //call.Response = call.Request.Replay(); 
             call.ExceptionHandled = true;
         }
     }
}

After I refresh the token I have access to the HttpCall Object which just threw the expired token error. I'd like to replay the request and replace the response but I 'm not sure of how to do that.

How can I replay a request from an HttpCall in Flurl?

2

There are 2 best solutions below

0
On BEST ANSWER

I found an overload to generically send my request, I made an extension method

public static async Task<HttpCall> Replay(this HttpCall call)
{
    call.Response = await call.FlurlRequest.SendAsync(call.Request.Method, call.Request.Content);
    return call;
}
1
On

Maybe you want to take a look at the c# library named polly. I think it solves most of the retry problems developer are facing. https://github.com/App-vNext/Polly

You could use the retry policy from polly or another one that fits your needs

        // Retry multiple times, calling an action on each retry 
// with the current exception and retry count
Policy
    .Handle<SomeExceptionType>()
    .Retry(3, (exception, retryCount) =>
    {
        // do something 
    });