Skip to content

Commit

Permalink
Merge pull request #116 from itsharppro/dev
Browse files Browse the repository at this point in the history
(#115) udpate webApi read query
  • Loading branch information
SaintAngeLs authored Sep 30, 2024
2 parents 92019ad + c21bbd5 commit 4a45223
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Paralax.HTTP/src/Paralax.HTTP/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static IParalaxBuilder AddHttpClient(this IParalaxBuilder builder, string
}

builder.Services.AddSingleton(options);
builder.Services.AddSingleton<IHttpClientSerializer, SystemTextJsonHttpClientSerializer>();
builder.Services.AddSingleton<IHttpClientSerializer, NetJsonHttpClientSerializer>();

var clientBuilder = builder.Services.AddHttpClient<IHttpClient, ParalaxHttpClient>(clientName);
httpClientBuilder?.Invoke(clientBuilder);
Expand Down
43 changes: 43 additions & 0 deletions src/Paralax.HTTP/src/Paralax.HTTP/NetJsonHttpClientSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.IO;
using System.Threading.Tasks;
using NetJSON;

namespace Paralax.HTTP
{
public class NetJsonHttpClientSerializer : IHttpClientSerializer
{
private readonly NetJSONSettings _settings;

public NetJsonHttpClientSerializer(NetJSONSettings settings = null)
{
_settings = settings ?? new NetJSONSettings
{
UseEnumString = true,
CaseSensitive = false,
SkipDefaultValue = true
};
}

public string Serialize<T>(T value) => NetJSON.NetJSON.Serialize(value, _settings);

public async Task SerializeAsync<T>(Stream stream, T value)
{
using (var writer = new StreamWriter(stream))
{
await writer.WriteAsync(NetJSON.NetJSON.Serialize(value, _settings));
await writer.FlushAsync();
}
}

public async ValueTask<T> DeserializeAsync<T>(Stream stream)
{
using (var reader = new StreamReader(stream))
{
var content = await reader.ReadToEndAsync();
return NetJSON.NetJSON.Deserialize<T>(content, _settings);
}
}

public T Deserialize<T>(string json) => NetJSON.NetJSON.Deserialize<T>(json, _settings);
}
}
1 change: 1 addition & 0 deletions src/Paralax.HTTP/src/Paralax.HTTP/Paralax.HTTP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NetJSON" Version="1.4.4" />
<PackageReference Include="Paralax" Version="*" />
<PackageReference Include="Polly" Version="8.4.1" />
</ItemGroup>
Expand Down
28 changes: 20 additions & 8 deletions src/Paralax.WebApi/src/Paralax.WebApi/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,33 +163,45 @@ public static IParalaxBuilder AddErrorHandler<T>(this IParalaxBuilder builder)
public static T ReadQuery<T>(this HttpContext context) where T : class
{
var request = context.Request;
RouteValueDictionary values = null;

var values = new Dictionary<string, object>();

// Parse route data if it exists
if (HasRouteData(request))
{
values = request.HttpContext.GetRouteData().Values;
var routeValues = request.HttpContext.GetRouteData().Values;
foreach (var (key, value) in routeValues)
{
values[key] = value;
}
}

// Parse query string if it exists
if (HasQueryString(request))
{
var queryString = HttpUtility.ParseQueryString(request.HttpContext.Request.QueryString.Value);
values ??= new RouteValueDictionary();
foreach (var key in queryString.AllKeys)
{
values.TryAdd(key, queryString[key]);
if (!string.IsNullOrEmpty(key))
{
values[key] = queryString[key];
}
}
}

if (values == null)
// If there are no values, return a new instance of the object
if (!values.Any())
{
return NetJSON.NetJSON.Deserialize<T>(EmptyJsonObject);
return null;
}

var serialized = NetJSON.NetJSON.Serialize(values.ToDictionary(k => k.Key, k => k.Value));
// Serialize the dictionary of values into JSON
var serialized = NetJSON.NetJSON.Serialize(values);

// Deserialize the JSON back into the expected object type
return NetJSON.NetJSON.Deserialize<T>(serialized);
}


public static Task Ok(this HttpResponse response, object data = null)
{
response.StatusCode = (int)HttpStatusCode.OK;
Expand Down

0 comments on commit 4a45223

Please sign in to comment.