Skip to content
22 changes: 22 additions & 0 deletions Web/API/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
/// </summary>
private readonly HttpClient _httpClient = new();

/// <summary>
/// Gets or sets the timeout applied to every request sent by this instance.
/// Defaults to 100 seconds (HttpClient default).
/// Set to <see cref="System.Threading.Timeout.InfiniteTimeSpan"/> to disable.
/// </summary>
public TimeSpan Timeout
{
get => _httpClient.Timeout;
set => _httpClient.Timeout = value;
}

/// <summary>
/// Settings for JSON serialization.
/// </summary>
Expand Down Expand Up @@ -57,12 +68,12 @@
/// <summary>
/// Gets the JSON body content of the request.
/// </summary>
public object Body { get; private set; } = null;

Check warning on line 71 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 71 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 71 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

/// <summary>
/// Gets the binary document content of the request.
/// </summary>
public byte[] DocumentBody { get; private set; } = null;

Check warning on line 76 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 76 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 76 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

/// <summary>
/// Gets the name of the binary document file.
Expand All @@ -72,7 +83,7 @@
/// <summary>
/// Gets the content type of the request.
/// </summary>
public string ContentType { get; private set; } = null;

Check warning on line 86 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 86 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

#endregion

Expand Down Expand Up @@ -479,7 +490,7 @@

if (response.IsSuccessStatusCode)
{
string? mediaType = response.Content?.Headers?.ContentType?.MediaType.ToLower();

Check warning on line 493 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 493 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
string contentResponse = await response.Content?.ReadAsStringAsync();

switch (true)
Expand All @@ -503,6 +514,17 @@
result.Error = await response.Content.ReadAsStringAsync();
}
}
catch (TaskCanceledException ex) when (!ex.CancellationToken.IsCancellationRequested)
{
// HttpClient.Timeout dépassé — HttpClient lève TaskCanceledException dans ce cas
result.StatusCode = 408; // Request Timeout
result.Error = $"La requête a expiré ({_httpClient.Timeout.TotalSeconds}s).";
}
catch (TaskCanceledException ex)
{
result.StatusCode = 499;
result.Error = $"La requête a été annulée : {ex.Message}";
}
catch (Exception ex)
{
result.StatusCode = 500;
Expand Down
Loading