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

CLIENTS: Add custom serialization and deserialization func WASM version and awaitable func. #155

Merged
Show file tree
Hide file tree
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
63 changes: 47 additions & 16 deletions RESTFulSense.WebAssembly/Clients/IRESTFulApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// See License.txt in the project root for license information.
// ---------------------------------------------------------------

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -12,81 +13,111 @@ namespace RESTFulSense.WebAssembly.Clients
{
public interface IRESTFulApiClient
{
ValueTask<T> GetContentAsync<T>(string relativeUrl);
ValueTask<T> GetContentAsync<T>(string relativeUrl, CancellationToken cancellationToken);
ValueTask<T> GetContentAsync<T>(string relativeUrl, Func<string, ValueTask<T>> deserializationFunction = null);
ValueTask<T> GetContentAsync<T>(
string relativeUrl,
CancellationToken cancellationToken,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<string> GetContentStringAsync(string relativeUrl);
ValueTask<Stream> GetContentStreamAsync(string relativeUrl);

ValueTask PostContentWithNoResponseAsync<T>(
string relativeUrl,
T content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null);

ValueTask PostContentWithNoResponseAsync<T>(
string relativeUrl,
T content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null);

ValueTask<T> PostContentAsync<T>(
string relativeUrl,
T content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<T> PostContentAsync<T>(
string relativeUrl,
T content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<Stream> PostContentWithStreamResponseAsync<T>(
string relativeUrl,
T content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null);

ValueTask<TResult> PostContentAsync<TContent, TResult>(
string relativeUrl,
TContent content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<TContent, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<TResult>> deserializationFunction = null);

ValueTask<TResult> PostContentAsync<TContent, TResult>(
string relativeUrl,
TContent content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<TContent, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<TResult>> deserializationFunction = null);

ValueTask<T> PutContentAsync<T>(
string relativeUrl,
T content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<TResult> PutContentAsync<TContent, TResult>(
string relativeUrl,
TContent content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<TContent, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<TResult>> deserializationFunction = null);

ValueTask<TResult> PutContentAsync<TContent, TResult>(
string relativeUrl,
TContent content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<TContent, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<TResult>> deserializationFunction = null);

ValueTask<T> PutContentAsync<T>(string relativeUrl, Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<T> PutContentAsync<T>(
string relativeUrl,
CancellationToken cancellationToken, Func<string,
ValueTask<T>> deserializationFunction = null);

ValueTask<T> PutContentAsync<T>(string relativeUrl);
ValueTask<T> PutContentAsync<T>(string relativeUrl, CancellationToken cancellationToken);
ValueTask DeleteContentAsync(string relativeUrl);
ValueTask DeleteContentAsync(string relativeUrl, CancellationToken cancellationToken);
ValueTask<T> DeleteContentAsync<T>(string relativeUrl);
ValueTask<T> DeleteContentAsync<T>(string relativeUrl, CancellationToken cancellationToken);
ValueTask<T> DeleteContentAsync<T>(string relativeUrl, Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<T> DeleteContentAsync<T>(
string relativeUrl,
CancellationToken cancellationToken,
Func<string, ValueTask<T>> deserializationFunction = null);
}
}
62 changes: 46 additions & 16 deletions RESTFulSense.WebAssembly/Clients/IRESTFulApiFactoryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// See License.txt in the project root for license information.
// ---------------------------------------------------------------

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -12,81 +13,110 @@ namespace RESTFulSense.WebAssembly.Clients
{
public interface IRESTFulApiFactoryClient
{
ValueTask<T> GetContentAsync<T>(string relativeUrl);
ValueTask<T> GetContentAsync<T>(string relativeUrl, CancellationToken cancellationToken);
ValueTask<T> GetContentAsync<T>(string relativeUrl, Func<string, ValueTask<T>> deserializationFunction = null);
ValueTask<T> GetContentAsync<T>(
string relativeUrl,
CancellationToken cancellationToken,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<string> GetContentStringAsync(string relativeUrl);
ValueTask<Stream> GetContentStreamAsync(string relativeUrl);

ValueTask PostContentWithNoResponseAsync<T>(
string relativeUrl,
T content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null);

ValueTask PostContentWithNoResponseAsync<T>(
string relativeUrl,
T content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null);

ValueTask<T> PostContentAsync<T>(
string relativeUrl,
T content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<T> PostContentAsync<T>(
string relativeUrl,
T content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<Stream> PostContentWithStreamResponseAsync<T>(
string relativeUrl,
T content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null);

ValueTask<TResult> PostContentAsync<TContent, TResult>(
string relativeUrl,
TContent content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<TContent, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<TResult>> deserializationFunction = null);

ValueTask<T> PutContentAsync<T>(
string relativeUrl,
T content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<T> PutContentAsync<T>(
string relativeUrl,
T content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<T, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<TResult> PutContentAsync<TContent, TResult>(
string relativeUrl,
TContent content,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<TContent, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<TResult>> deserializationFunction = null);

ValueTask<TResult> PutContentAsync<TContent, TResult>(
string relativeUrl,
TContent content,
CancellationToken cancellationToken,
string mediaType = "text/json",
bool ignoreDefaultValues = false);
bool ignoreDefaultValues = false,
Func<TContent, ValueTask<string>> serializationFunction = null,
Func<string, ValueTask<TResult>> deserializationFunction = null);

ValueTask<T> PutContentAsync<T>(string relativeUrl, Func<string, ValueTask<T>> deserializationFunction = null);
ValueTask<T> PutContentAsync<T>(
string relativeUrl,
CancellationToken cancellationToken,
Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<T> PutContentAsync<T>(string relativeUrl);
ValueTask<T> PutContentAsync<T>(string relativeUrl, CancellationToken cancellationToken);
ValueTask DeleteContentAsync(string relativeUrl);
ValueTask DeleteContentAsync(string relativeUrl, CancellationToken cancellationToken);
ValueTask<T> DeleteContentAsync<T>(string relativeUrl);
ValueTask<T> DeleteContentAsync<T>(string relativeUrl, CancellationToken cancellationToken);
ValueTask<T> DeleteContentAsync<T>(string relativeUrl, Func<string, ValueTask<T>> deserializationFunction = null);

ValueTask<T> DeleteContentAsync<T>(
string relativeUrl,
CancellationToken cancellationToken,
Func<string, ValueTask<T>> deserializationFunction = null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ---------------------------------------------------------------
// Copyright (c) Hassan Habib
// Licensed under the MIT License.
// See License.txt in the project root for license information.
// ---------------------------------------------------------------

using Newtonsoft.Json;

namespace RESTFulSense.WebAssembly.Clients
{
public partial class RESTFulApiClient
{
private static JsonSerializerSettings CreateJsonSerializerSettings(bool ignoreDefaultValues)
{
var defaultValueHandling = ignoreDefaultValues ? DefaultValueHandling.Ignore : DefaultValueHandling.Include;
var jsonSerializerSettings = new JsonSerializerSettings { DefaultValueHandling = defaultValueHandling };
return jsonSerializerSettings;
}

private static string ConvertToJsonNewtonSoftStringContent<T>(T content, bool ignoreDefaultValues)
{
JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings(ignoreDefaultValues);

return JsonConvert.SerializeObject(
content,
formatting: Formatting.None,
settings: jsonSerializerSettings);
}
}
}
38 changes: 18 additions & 20 deletions RESTFulSense.WebAssembly/Clients/RESTFulApiClient.Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
// See License.txt in the project root for license information.
// ---------------------------------------------------------------

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace RESTFulSense.WebAssembly.Clients
{
public partial class RESTFulApiClient
{
private static HttpContent ConvertToHttpContent<T>(T content, string mediaType, bool ignoreDefaultValues)
private async static ValueTask<HttpContent> ConvertToHttpContent<T>(
T content,
string mediaType,
bool ignoreDefaultValues,
Func<T, ValueTask<string>> serializationFunction = null)
{
return mediaType switch
{
"text/json" => ConvertToJsonStringContent(content, mediaType, ignoreDefaultValues),
"application/json" => ConvertToJsonStringContent(content, mediaType, ignoreDefaultValues),
"text/json" => await ConvertToJsonStringContent(content, mediaType, ignoreDefaultValues, serializationFunction),
"application/json" => await ConvertToJsonStringContent(content, mediaType, ignoreDefaultValues, serializationFunction),
"text/plain" => ConvertToStringContent(content, mediaType),
"application/octet-stream" => ConvertToStreamContent(content as Stream, mediaType),
_ => ConvertToStringContent(content, mediaType)
Expand All @@ -34,14 +39,15 @@ private static StringContent ConvertToStringContent<T>(T content, string mediaTy
mediaType);
}

private static StringContent ConvertToJsonStringContent<T>(T content, string mediaType, bool ignoreDefaultValues)
private async static ValueTask<StringContent> ConvertToJsonStringContent<T>(
T content,
string mediaType,
bool ignoreDefaultValues,
Func<T, ValueTask<string>> serializationFunction = null)
{
JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings(ignoreDefaultValues);

string serializedRestrictionRequest = JsonConvert.SerializeObject(
content,
formatting: Formatting.None,
settings: jsonSerializerSettings);
string serializedRestrictionRequest = serializationFunction == null
? ConvertToJsonNewtonSoftStringContent<T>(content, ignoreDefaultValues)
: await serializationFunction(content);

var contentString =
new StringContent(
Expand All @@ -50,7 +56,7 @@ private static StringContent ConvertToJsonStringContent<T>(T content, string med
mediaType);

return contentString;
}
}

private static StreamContent ConvertToStreamContent<T>(T content, string mediaType)
where T : Stream
Expand All @@ -60,13 +66,5 @@ private static StreamContent ConvertToStreamContent<T>(T content, string mediaTy

return contentStream;
}

private static JsonSerializerSettings CreateJsonSerializerSettings(bool ignoreDefaultValues)
{
DefaultValueHandling defaultValueHandling = ignoreDefaultValues ? DefaultValueHandling.Ignore : DefaultValueHandling.Include;
var jsonSerializerSettings = new JsonSerializerSettings { DefaultValueHandling = defaultValueHandling };

return jsonSerializerSettings;
}
}
}
Loading