Skip to content

Commit 0ac0b3e

Browse files
authored
Merge pull request #3 from scottoffen/add-xml-support
Add XML Deserialization Support
2 parents 00aa62f + c546540 commit 0ac0b3e

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

src/FluentHttpClient/Cookies.cs

-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public void Add(string key, object value)
2626
base.Add(ValidateName(key), ValidateValue(value.ToString()));
2727
}
2828

29-
#if !NETSTANDARD2_0
30-
3129
public new bool TryAdd(string key, string value)
3230
{
3331
return base.TryAdd(ValidateName(key), ValidateValue(value));
@@ -38,8 +36,6 @@ public bool TryAdd(string key, object value)
3836
return base.TryAdd(ValidateName(key), ValidateValue(value.ToString()));
3937
}
4038

41-
#endif
42-
4339
public override string ToString()
4440
{
4541
return Count <= 0

src/FluentHttpClient/FluentHttpClient.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net7.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.1;net5.0;net6.0;net7.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<LangVersion>10.0</LangVersion>
77
<PackageId>FluentHttpClient</PackageId>
@@ -14,7 +14,7 @@
1414
<PackageTags>fluent httpclient rest http api web client</PackageTags>
1515
<AssemblyVersion>$(Version)</AssemblyVersion>
1616
<FileVersion>$(Version)</FileVersion>
17-
<Version>2.0.0</Version>
17+
<Version>2.1.0</Version>
1818
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1919
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2020
<RepositoryUrl>https://github.com/scottoffen/fluenthttpclient</RepositoryUrl>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Xml.Linq;
2+
using System.Xml.Serialization;
3+
4+
namespace FluentHttpClient;
5+
6+
public static class FluentXmlDeserialization
7+
{
8+
public static async Task<XElement> DeserializeXmlAsync(this Task<HttpResponseMessage> taskResponse)
9+
{
10+
return await taskResponse.DeserializeXmlAsync(LoadOptions.None, CancellationToken.None);
11+
}
12+
13+
public static async Task<XElement> DeserializeXmlAsync(this Task<HttpResponseMessage> taskResponse, LoadOptions options)
14+
{
15+
return await taskResponse.DeserializeXmlAsync(options, CancellationToken.None);
16+
}
17+
18+
public static async Task<XElement> DeserializeXmlAsync(this Task<HttpResponseMessage> taskResponse, LoadOptions options, CancellationToken? token)
19+
{
20+
token = token ?? CancellationToken.None;
21+
var response = await taskResponse;
22+
23+
return await XElement.LoadAsync(await response.GetResponseStreamAsync(), options, token.Value);
24+
}
25+
26+
public static async Task<T> DeserializeXmlAsync<T>(this Task<HttpResponseMessage> taskResponse, Func<HttpResponseMessage, Task<T>> defaultAction)
27+
{
28+
var response = await taskResponse;
29+
return (response.IsSuccessStatusCode)
30+
? await response.DeserializeXmlAsync<T>()
31+
: await defaultAction(response);
32+
}
33+
34+
public static async Task<T> DeserializeXmlAsync<T>(this Task<HttpResponseMessage> taskResponse)
35+
{
36+
var response = await taskResponse;
37+
return await response.DeserializeXmlAsync<T>();
38+
}
39+
40+
public static async Task<T> DeserializeXmlAsync<T>(this HttpResponseMessage response)
41+
{
42+
using (var reader = new StreamReader(await response.GetResponseStreamAsync()))
43+
{
44+
var serializer = new XmlSerializer(typeof(T));
45+
return (T) serializer.Deserialize(reader);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)