|
2 | 2 | using FitSyncHub.Zwift.HttpClients.Models.Responses.ZwiftRacing; |
3 | 3 | using FitSyncHub.Zwift.JsonSerializerContexts; |
4 | 4 | using Microsoft.AspNetCore.WebUtilities; |
| 5 | +using Microsoft.Extensions.Logging; |
5 | 6 | using Microsoft.Extensions.Primitives; |
6 | 7 |
|
7 | 8 | namespace FitSyncHub.Zwift.HttpClients; |
8 | 9 |
|
9 | 10 | public sealed class ZwiftRacingHttpClient |
10 | 11 | { |
11 | 12 | private readonly HttpClient _httpClient; |
| 13 | + private readonly ILogger<ZwiftRacingHttpClient> _logger; |
12 | 14 |
|
13 | | - public ZwiftRacingHttpClient(HttpClient httpClient) |
| 15 | + public ZwiftRacingHttpClient( |
| 16 | + HttpClient httpClient, |
| 17 | + ILogger<ZwiftRacingHttpClient> logger) |
14 | 18 | { |
15 | 19 | _httpClient = httpClient; |
| 20 | + _logger = logger; |
16 | 21 | } |
17 | 22 |
|
18 | 23 | public async Task<IReadOnlyCollection<ZwiftRacingEventResponse>> GetEvent( |
@@ -51,11 +56,28 @@ public async Task<IReadOnlyCollection<ZwiftRacingEventResponse>> GetEvent( |
51 | 56 | var url = QueryHelpers.AddQueryString($"api/riders/{riderId}/history", queryParams); |
52 | 57 |
|
53 | 58 | var response = await _httpClient.GetAsync(url, cancellationToken); |
54 | | - response.EnsureSuccessStatusCode(); |
55 | | - |
56 | 59 | var content = await response.Content.ReadAsStringAsync(cancellationToken); |
57 | 60 |
|
58 | | - return JsonSerializer.Deserialize(content, |
59 | | - ZwiftRacingGenerationContext.Default.ZwiftRacingRiderResponse); |
| 61 | + if (response.IsSuccessStatusCode) |
| 62 | + { |
| 63 | + return JsonSerializer.Deserialize(content, |
| 64 | + ZwiftRacingGenerationContext.Default.ZwiftRacingRiderResponse); |
| 65 | + } |
| 66 | + |
| 67 | + var jsonDocument = JsonDocument.Parse(content); |
| 68 | + if (jsonDocument.RootElement.TryGetProperty("error", out var errorJsonValue)) |
| 69 | + { |
| 70 | + var errorText = errorJsonValue.ToString(); |
| 71 | + if (errorText == "API responded with status 404") |
| 72 | + { |
| 73 | + return default; |
| 74 | + } |
| 75 | + |
| 76 | + _logger.LogWarning("Error from zwiftracing.app: {Error}", errorText); |
| 77 | + } |
| 78 | + |
| 79 | + response.EnsureSuccessStatusCode(); |
| 80 | + // it will not go here, but just to supress compiler error |
| 81 | + return default; |
60 | 82 | } |
61 | 83 | } |
0 commit comments