|
13 | 13 |
|
14 | 14 | using Newtonsoft.Json; |
15 | 15 |
|
| 16 | +using Nullinside.Api.Common; |
16 | 17 | using Nullinside.Api.Common.Extensions; |
17 | 18 | using Nullinside.Api.Common.Twitch; |
18 | 19 |
|
@@ -264,36 +265,38 @@ private void ClearCredentials() { |
264 | 265 | /// Downloads the user's profile image and adds it to the cache. |
265 | 266 | /// </summary> |
266 | 267 | /// <returns>The path to the saved file.</returns> |
267 | | - private async Task<string?> DownloadUserImage() { |
268 | | - // The user object from the API will tell us the download link on twitch for the image. |
269 | | - var api = new TwitchApiWrapper(); |
270 | | - if (string.IsNullOrWhiteSpace(api.OAuth?.AccessToken)) { |
271 | | - return null; |
272 | | - } |
| 268 | + private async Task<string?> DownloadUserImage(CancellationToken token = new()) { |
| 269 | + return await Retry.Execute(async () => { |
| 270 | + // The user object from the API will tell us the download link on twitch for the image. |
| 271 | + var api = new TwitchApiWrapper(); |
| 272 | + if (string.IsNullOrWhiteSpace(api.OAuth?.AccessToken)) { |
| 273 | + return null; |
| 274 | + } |
273 | 275 |
|
274 | | - User? user = await api.GetUser().ConfigureAwait(false); |
275 | | - if (string.IsNullOrWhiteSpace(user?.ProfileImageUrl)) { |
276 | | - return null; |
277 | | - } |
| 276 | + User? user = await api.GetUser(token).ConfigureAwait(false); |
| 277 | + if (string.IsNullOrWhiteSpace(user?.ProfileImageUrl)) { |
| 278 | + return null; |
| 279 | + } |
278 | 280 |
|
279 | | - // Download the image via http. |
280 | | - using var http = new HttpClient(); |
281 | | - byte[] imageBytes = await http.GetByteArrayAsync(user.ProfileImageUrl).ConfigureAwait(false); |
| 281 | + // Download the image via http. |
| 282 | + using var http = new HttpClient(); |
| 283 | + byte[] imageBytes = await http.GetByteArrayAsync(user.ProfileImageUrl, token).ConfigureAwait(false); |
282 | 284 |
|
283 | | - // If the directory doesn't exist, create it. |
284 | | - if (!Directory.Exists(PROFILE_IMAGE_FOLDER)) { |
285 | | - Directory.CreateDirectory(PROFILE_IMAGE_FOLDER); |
286 | | - } |
| 285 | + // If the directory doesn't exist, create it. |
| 286 | + if (!Directory.Exists(PROFILE_IMAGE_FOLDER)) { |
| 287 | + Directory.CreateDirectory(PROFILE_IMAGE_FOLDER); |
| 288 | + } |
287 | 289 |
|
288 | | - // I don't think twitch usernames can have non-filepath friendly characters but might as well sanitize it anyway. |
289 | | - string filename = SanitizeFilename(string.Format(PROFILE_IMAGE_FILENAME, user.Login)); |
290 | | - string imagePath = Path.Combine(PROFILE_IMAGE_FOLDER, filename); |
| 290 | + // I don't think twitch usernames can have non-filepath friendly characters but might as well sanitize it anyway. |
| 291 | + string filename = SanitizeFilename(string.Format(PROFILE_IMAGE_FILENAME, user.Login)); |
| 292 | + string imagePath = Path.Combine(PROFILE_IMAGE_FOLDER, filename); |
291 | 293 |
|
292 | | - // Save to disk |
293 | | - await File.WriteAllBytesAsync(imagePath, imageBytes).ConfigureAwait(false); |
| 294 | + // Save to disk |
| 295 | + await File.WriteAllBytesAsync(imagePath, imageBytes, token).ConfigureAwait(false); |
294 | 296 |
|
295 | | - // Return path to file, even though everyone already knows it. |
296 | | - return imagePath; |
| 297 | + // Return path to file, even though everyone already knows it. |
| 298 | + return imagePath; |
| 299 | + }, 10, token, TimeSpan.FromSeconds(1)).ConfigureAwait(false); |
297 | 300 | } |
298 | 301 |
|
299 | 302 | /// <summary> |
|
0 commit comments