Skip to content

Commit

Permalink
Retry failed NexusAPI calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jan 3, 2020
1 parent d20de58 commit 412d854
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions Wabbajack.Common/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ public static string UserAgent

public static string WabbajackCacheHostname = "build.wabbajack.org";
public static int WabbajackCachePort = 80;
public static int MaxHTTPRetries = 4;
}
}
26 changes: 20 additions & 6 deletions Wabbajack.Lib/NexusApi/NexusApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,28 @@ public static async Task<NexusApiClient> Get(string apiKey = null)

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}");
int retries = 0;
TOP:
try
{
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())
using (var stream = await response.Content.ReadAsStreamAsync())
{
return stream.FromJSON<T>();
}
}
catch (TimeoutException)
{
return stream.FromJSON<T>();
if (retries == Consts.MaxHTTPRetries)
throw;
Utils.Log($"Nexus call to {url} failed, retrying {retries} of {Consts.MaxHTTPRetries}");
retries++;
goto TOP;
}
}

Expand Down

0 comments on commit 412d854

Please sign in to comment.