Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handled responses with null content #22

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
12 changes: 11 additions & 1 deletion Web/API/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@
/// <summary>
/// Gets the JSON body content of the request.
/// </summary>
public object Body { get; private set; } = null;

Check warning on line 54 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 54 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 59 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 59 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.
/// </summary>
public string DocumentFileName { get; private set; } = null;

Check warning on line 64 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 64 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 content type of the request.
/// </summary>
public string ContentType { get; private set; } = null;

Check warning on line 69 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 69 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
#endregion

#region Constructors
Expand Down Expand Up @@ -391,18 +391,28 @@
response = await _httpClient.SendAsync(request);
result.StatusCode = (int)response.StatusCode;

if (
response.Content == null ||
string.IsNullOrEmpty(await response.Content.ReadAsStringAsync())
)
{
result.Value = default;

return result;
}

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

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 406 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 407 in Web/API/Request.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 407 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("application/xml")):

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 411 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 415 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.

Check warning on line 415 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 Down
Loading