diff --git a/src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs b/src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs index 45a719a..7639bfc 100644 --- a/src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs +++ b/src/TwitchStreamingTools/ViewModels/Pages/AccountViewModel.cs @@ -13,6 +13,7 @@ using Newtonsoft.Json; +using Nullinside.Api.Common; using Nullinside.Api.Common.Extensions; using Nullinside.Api.Common.Twitch; @@ -264,36 +265,38 @@ private void ClearCredentials() { /// Downloads the user's profile image and adds it to the cache. /// /// The path to the saved file. - private async Task DownloadUserImage() { - // The user object from the API will tell us the download link on twitch for the image. - var api = new TwitchApiWrapper(); - if (string.IsNullOrWhiteSpace(api.OAuth?.AccessToken)) { - return null; - } + private async Task DownloadUserImage(CancellationToken token = new()) { + return await Retry.Execute(async () => { + // The user object from the API will tell us the download link on twitch for the image. + var api = new TwitchApiWrapper(); + if (string.IsNullOrWhiteSpace(api.OAuth?.AccessToken)) { + return null; + } - User? user = await api.GetUser().ConfigureAwait(false); - if (string.IsNullOrWhiteSpace(user?.ProfileImageUrl)) { - return null; - } + User? user = await api.GetUser(token).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(user?.ProfileImageUrl)) { + return null; + } - // Download the image via http. - using var http = new HttpClient(); - byte[] imageBytes = await http.GetByteArrayAsync(user.ProfileImageUrl).ConfigureAwait(false); + // Download the image via http. + using var http = new HttpClient(); + byte[] imageBytes = await http.GetByteArrayAsync(user.ProfileImageUrl, token).ConfigureAwait(false); - // If the directory doesn't exist, create it. - if (!Directory.Exists(PROFILE_IMAGE_FOLDER)) { - Directory.CreateDirectory(PROFILE_IMAGE_FOLDER); - } + // If the directory doesn't exist, create it. + if (!Directory.Exists(PROFILE_IMAGE_FOLDER)) { + Directory.CreateDirectory(PROFILE_IMAGE_FOLDER); + } - // I don't think twitch usernames can have non-filepath friendly characters but might as well sanitize it anyway. - string filename = SanitizeFilename(string.Format(PROFILE_IMAGE_FILENAME, user.Login)); - string imagePath = Path.Combine(PROFILE_IMAGE_FOLDER, filename); + // I don't think twitch usernames can have non-filepath friendly characters but might as well sanitize it anyway. + string filename = SanitizeFilename(string.Format(PROFILE_IMAGE_FILENAME, user.Login)); + string imagePath = Path.Combine(PROFILE_IMAGE_FOLDER, filename); - // Save to disk - await File.WriteAllBytesAsync(imagePath, imageBytes).ConfigureAwait(false); + // Save to disk + await File.WriteAllBytesAsync(imagePath, imageBytes, token).ConfigureAwait(false); - // Return path to file, even though everyone already knows it. - return imagePath; + // Return path to file, even though everyone already knows it. + return imagePath; + }, 10, token, TimeSpan.FromSeconds(1)).ConfigureAwait(false); } ///