I have the following code to call the bank api, and convert the response into the object.
public static async Task<ExchangeModel> LoadExchange(string currency, string date)
{
string url = "here is the bank's api url";
var streamTask = await _clientPolicy.LinearHttpRetry.ExecuteAsync(
() => _client.GetStreamAsync(url));
var deserialized = await JsonSerializer.DeserializeAsync<Root>(await streamTask);
var rate = deserialized.ExchangeModel.FirstOrDefault(r => r.Currency == currency);
return rate;
}
I wanted to add polly in order to retry api call if it failed. To do so, I created a policy client, but I don't know how to check if GetStreamAsync
is succeed as it is a System.IO.Stream
and it has no succeed status or something.
public AsyncRetryPolicy<System.IO.Stream> LinearHttpRetry { get; }
public ClientPolicy()
{
LinearHttpRetry = Policy.HandleResult<System.IO.Stream>(
res => !res.IsSuccessStatusCode).
WaitAndRetryAsync(5, retryAttempts => TimeSpan.FromSeconds(3));
}
In this example I kept it !res.IsSuccessStatusCode
but there is no such parameter in my case.