Skip to content

Commit

Permalink
feat: added support of xml deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sikelio committed Oct 4, 2024
1 parent 776c0a6 commit 2f77cac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This repos contains tool classes in C# for multiple types of usage.
* WakeOnLan: Power on a device with it mac address.

* `FrApp42.Web` - [Link](https://www.nuget.org/packages/FrApp42.Web)
* Request: Make API request for any of your C# projects. ⚠️ *SOAP requests are not supported.* ⚠️
* Request: Make API request for any of your C# projects.

## Authors
* [AnthoDingo](https://github.com)
Expand Down
21 changes: 19 additions & 2 deletions Web/API/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Text;
using System.Xml.Serialization;

namespace FrApp42.Web.API
{
Expand Down Expand Up @@ -392,8 +393,24 @@ private async Task<Result<T>> Process<T>(HttpRequestMessage request)

if (response.IsSuccessStatusCode)
{
Stream ContentResponse = await response.Content?.ReadAsStreamAsync();
result.Value = await System.Text.Json.JsonSerializer.DeserializeAsync<T>(ContentResponse);
string MediaType = response.Content?.Headers?.ContentType?.MediaType.ToLower();

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 396 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.
string ContentResponse = await response.Content?.ReadAsStringAsync();

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 401 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 405 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 405 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);
break;
default:
result.Value = default;
break;
}
}
else
{
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.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 2f77cac

Please sign in to comment.