Skip to content

Commit

Permalink
Retry uploading playtime if needed
Browse files Browse the repository at this point in the history
It shouldn't happen, but who knows
  • Loading branch information
hawkeye116477 committed Oct 23, 2024
1 parent 5d3de9c commit b4b3f0b
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Services/EpicAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private OauthResponse LoadTokens()
return null;
}

public void UploadPlaytime(DateTime startTime, DateTime endTime, Game game)
public void UploadPlaytime(DateTime startTime, DateTime endTime, Game game, int attempts = 3)
{
GlobalProgressOptions globalProgressOptions = new GlobalProgressOptions(ResourceProvider.GetString(LOC.LegendaryUploadingPlaytime).Format(game.Name), false);
api.Dialogs.ActivateGlobalProgress(async (a) =>
Expand All @@ -337,11 +337,25 @@ public void UploadPlaytime(DateTime startTime, DateTime endTime, Game game)
playtimePayload.startTime = startTime;
var playtimeJson = Serialization.ToJson(playtimePayload);
var content = new StringContent(playtimeJson, Encoding.UTF8, "application/json");
var result = await httpClient.PutAsync(uri, content);
if (!result.IsSuccessStatusCode)
try
{
api.Dialogs.ShowErrorMessage(api.Resources.GetString(LOC.LegendaryUploadPlaytimeError).Format(game.Name));
logger.Error($"An error occured during uploading playtime to the cloud. Status code: {result.StatusCode}.");
var response = await httpClient.PutAsync(uri, content);
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException exception)
{
if (attempts > 1)
{
attempts -= 1;
logger.Debug($"Retrying playtime upload for {game.Name}. Attempts left: {attempts}");
await Task.Delay(2000);
UploadPlaytime(startTime, endTime, game, attempts);
}
else
{
api.Dialogs.ShowErrorMessage(api.Resources.GetString(LOC.LegendaryUploadPlaytimeError).Format(game.Name));
logger.Error($"An error occured during uploading playtime to the cloud: {exception}.");
}
}
}
}
Expand Down

0 comments on commit b4b3f0b

Please sign in to comment.