Skip to content

Commit 6990088

Browse files
author
Andrii Bondarchuk
committed
Previously ZwiftRacing returned empty response when rider is not exists. Now it throws 500 with json object: {"error": "API responded with status 404"}
1 parent 129f430 commit 6990088

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/FitSyncHub.Zwift/HttpClients/ZwiftRacingHttpClient.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22
using FitSyncHub.Zwift.HttpClients.Models.Responses.ZwiftRacing;
33
using FitSyncHub.Zwift.JsonSerializerContexts;
44
using Microsoft.AspNetCore.WebUtilities;
5+
using Microsoft.Extensions.Logging;
56
using Microsoft.Extensions.Primitives;
67

78
namespace FitSyncHub.Zwift.HttpClients;
89

910
public sealed class ZwiftRacingHttpClient
1011
{
1112
private readonly HttpClient _httpClient;
13+
private readonly ILogger<ZwiftRacingHttpClient> _logger;
1214

13-
public ZwiftRacingHttpClient(HttpClient httpClient)
15+
public ZwiftRacingHttpClient(
16+
HttpClient httpClient,
17+
ILogger<ZwiftRacingHttpClient> logger)
1418
{
1519
_httpClient = httpClient;
20+
_logger = logger;
1621
}
1722

1823
public async Task<IReadOnlyCollection<ZwiftRacingEventResponse>> GetEvent(
@@ -51,11 +56,28 @@ public async Task<IReadOnlyCollection<ZwiftRacingEventResponse>> GetEvent(
5156
var url = QueryHelpers.AddQueryString($"api/riders/{riderId}/history", queryParams);
5257

5358
var response = await _httpClient.GetAsync(url, cancellationToken);
54-
response.EnsureSuccessStatusCode();
55-
5659
var content = await response.Content.ReadAsStringAsync(cancellationToken);
5760

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;
6082
}
6183
}

0 commit comments

Comments
 (0)