Retry failed NexusAPI calls.

This commit is contained in:
Timothy Baldridge 2020-01-03 16:02:48 -07:00
parent d20de58f50
commit 412d854160
2 changed files with 22 additions and 7 deletions

View File

@ -95,5 +95,6 @@ namespace Wabbajack.Common
public static string WabbajackCacheHostname = "build.wabbajack.org";
public static int WabbajackCachePort = 80;
public static int MaxHTTPRetries = 4;
}
}

View File

@ -222,14 +222,28 @@ namespace Wabbajack.Lib.NexusApi
public async Task<T> Get<T>(string url)
{
var response = await HttpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
UpdateRemaining(response);
if (!response.IsSuccessStatusCode)
throw new HttpRequestException($"{response.StatusCode} - {response.ReasonPhrase}");
using (var stream = await response.Content.ReadAsStreamAsync())
int retries = 0;
TOP:
try
{
return stream.FromJSON<T>();
var response = await HttpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
UpdateRemaining(response);
if (!response.IsSuccessStatusCode)
throw new HttpRequestException($"{response.StatusCode} - {response.ReasonPhrase}");
using (var stream = await response.Content.ReadAsStreamAsync())
{
return stream.FromJSON<T>();
}
}
catch (TimeoutException)
{
if (retries == Consts.MaxHTTPRetries)
throw;
Utils.Log($"Nexus call to {url} failed, retrying {retries} of {Consts.MaxHTTPRetries}");
retries++;
goto TOP;
}
}