Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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.

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.

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,16 +490,16 @@

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.

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();

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

switch (true)
{
case bool b when (mediaType.Contains("xml")):

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
XmlSerializer xmlSerializer = new(typeof(T));
StringReader reader = new(contentResponse);

result.Value = (T)xmlSerializer.Deserialize(reader);

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

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
break;
case bool b when (mediaType.Contains("application/json")):
result.Value = JsonConvert.DeserializeObject<T>(contentResponse);
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
2 changes: 1 addition & 1 deletion Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<Company>FrenchyApps42</Company>
<PackageIcon>logo.png</PackageIcon>
<Version>1.4.0</Version>
<Version>1.4.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading