Skip to content

Commit

Permalink
(#119) cqrs web api: update http clinet serialization [pack-all-force]
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Oct 3, 2024
1 parent 6707703 commit 5a78d3f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ public NetJsonHttpClientSerializer(NetJSONSettings settings = null)
{
_settings = settings ?? new NetJSONSettings
{
UseEnumString = true,
CaseSensitive = false,
SkipDefaultValue = true
UseEnumString = true,
CaseSensitive = false,
SkipDefaultValue = false,
DateFormat = NetJSONDateFormat.ISO,
TimeZoneFormat = NetJSONTimeZoneFormat.Utc
};
}

Expand Down
10 changes: 2 additions & 8 deletions src/Paralax.HTTP/src/Paralax.HTTP/ParalaxHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ private async Task<HttpResult<T>> CreateHttpResult<T>(HttpResponseMessage respon
var stream = await response.Content.ReadAsStreamAsync();
serializer ??= _serializer;
var result = await serializer.DeserializeAsync<T>(stream);
Console.WriteLine($"Deserialized Result: {result}");

return new HttpResult<T>(result, response);
}
Expand All @@ -198,7 +199,6 @@ private StringContent GetJsonPayload(object data, IHttpClientSerializer serializ
return content;
}

// Retry policy using Polly
private async Task<T> RetryPolicy<T>(Func<Task<T>> action)
{
return await Policy
Expand All @@ -209,22 +209,18 @@ private async Task<T> RetryPolicy<T>(Func<Task<T>> action)

protected virtual async Task<HttpResult<T>> SendResultAsync<T>(string uri, Method method, HttpContent content = null, IHttpClientSerializer serializer = null)
{
// Sending the request using the method and content
var response = await SendAsync(uri, method, content);

// If the response status code is not successful, return a default result
if (!response.IsSuccessStatusCode)
{
return new HttpResult<T>(default, response);
}

// Read the response content stream
var stream = await response.Content.ReadAsStreamAsync();

// Deserialize the stream using the provided serializer (or default to _serializer)
var result = await DeserializeJsonFromStream<T>(stream, serializer);
Console.WriteLine($"Deserialized Result: {result}");

// Return the deserialized result along with the response
return new HttpResult<T>(result, response);
}

Expand All @@ -235,7 +231,6 @@ private async Task<T> DeserializeJsonFromStream<T>(Stream stream, IHttpClientSer
return default;
}

// Use the provided serializer or default to _serializer
serializer ??= _serializer;

return await serializer.DeserializeAsync<T>(stream);
Expand All @@ -253,7 +248,6 @@ public enum Method
}
}

// Extension method for converting Method enum to HttpMethod
internal static class MethodExtensions
{
public static HttpMethod ToHttpMethod(this ParalaxHttpClient.Method method)
Expand Down

0 comments on commit 5a78d3f

Please sign in to comment.