Skip to content

Commit

Permalink
Allow passing media type when making JSON content
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Jan 16, 2024
1 parent 8567b1b commit 387b6fa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/Tingle.Extensions.Http/AbstractHttpApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json.Serialization.Metadata;
using SC = Tingle.Extensions.Http.HttpJsonSerializerContext;
Expand Down Expand Up @@ -244,25 +245,28 @@ protected virtual async Task<ResourceResponse<TResource, TProblem>> SendAsync<TR
/// <summary>Create <see cref="HttpContent"/> with JSON content from the provided <paramref name="value"/>.</summary>
/// <typeparam name="TValue">The type of the value to serialize.</typeparam>
/// <param name="value">The object to to write</param>
/// <param name="mediaType">The media type to use for the content.</param>
/// <returns>A <see cref="JsonContent"/> instance.</returns>
[RequiresUnreferencedCode(MessageStrings.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(MessageStrings.SerializationRequiresDynamicCodeMessage)]
protected virtual HttpContent MakeJsonContent<TValue>(TValue value) => JsonContent.Create(value, options: Options.SerializerOptions);
protected virtual HttpContent MakeJsonContent<TValue>(TValue value, MediaTypeHeaderValue? mediaType = null) => JsonContent.Create(value, mediaType, options: Options.SerializerOptions);

/// <summary>Create <see cref="HttpContent"/> with JSON content from the provided <paramref name="value"/>.</summary>
/// <param name="value">The object to to write</param>
/// <param name="valueType">The type of the value to serialize.</param>
/// <param name="mediaType">The media type to use for the content.</param>
/// <returns>A <see cref="JsonContent"/> instance.</returns>
[RequiresUnreferencedCode(MessageStrings.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(MessageStrings.SerializationRequiresDynamicCodeMessage)]
protected virtual HttpContent MakeJsonContent(object value, Type valueType) => JsonContent.Create(value, valueType, options: Options.SerializerOptions);
protected virtual HttpContent MakeJsonContent(object value, Type valueType, MediaTypeHeaderValue? mediaType = null) => JsonContent.Create(value, valueType, mediaType, options: Options.SerializerOptions);

/// <summary>Create <see cref="HttpContent"/> with JSON content from the provided <paramref name="value"/>.</summary>
/// <typeparam name="TValue">The type of the value to serialize.</typeparam>
/// <param name="value">The object to to write</param>
/// <param name="jsonTypeInfo">Metadata about the type to convert.</param>
/// <param name="mediaType">The media type to use for the content.</param>
/// <returns>A <see cref="JsonContent"/> instance.</returns>
protected virtual HttpContent MakeJsonContent<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo) => JsonContent.Create(value, jsonTypeInfo);
protected virtual HttpContent MakeJsonContent<TValue>(TValue value, JsonTypeInfo<TValue> jsonTypeInfo, MediaTypeHeaderValue? mediaType = null) => JsonContent.Create(value, jsonTypeInfo, mediaType);

/// <summary>Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.</summary>
/// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json.Serialization.Metadata;
using Tingle.Extensions.Http;
using Tingle.Extensions.PushNotifications.Apple.Models;
Expand Down Expand Up @@ -66,7 +65,7 @@ public virtual Task<ResourceResponse<ApnsMessageResponse, ApnsResponseError>> Se
if (data == null) throw new ArgumentNullException(nameof(data));
if (data.Aps == null) throw new ArgumentException($"{nameof(data.Aps)} cannot be null", nameof(data));

var content = JsonContent.Create(data, jsonTypeInfo);
var content = MakeJsonContent(data, jsonTypeInfo);
return SendAsync(header, content, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Net.Http.Json;
using System.Text.Json.Serialization.Metadata;
using Tingle.Extensions.Http;
using Tingle.Extensions.PushNotifications.FcmLegacy.Models;
Expand Down Expand Up @@ -54,7 +53,8 @@ protected virtual async Task<ResourceResponse<FcmLegacyResponse>> SendAsync<TMes
CancellationToken cancellationToken = default)
where TMessage : FcmLegacyRequest
{
var request = new HttpRequestMessage(HttpMethod.Post, BaseUrl) { Content = JsonContent.Create(message, jsonTypeInfo), };
var content = MakeJsonContent(message, jsonTypeInfo);
var request = new HttpRequestMessage(HttpMethod.Post, BaseUrl) { Content = content, };
return await SendAsync(request, SC.Default.FcmLegacyResponse, cancellationToken).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Net.Http.Json;
using Tingle.Extensions.Http;
using Tingle.Extensions.PushNotifications.FcmLegacy;
using Tingle.Extensions.PushNotifications.Firebase.Models;
Expand All @@ -26,7 +25,8 @@ public virtual async Task<ResourceResponse<FirebaseResponse, FirebaseResponsePro
CancellationToken cancellationToken = default)
{
var url = $"https://fcm.googleapis.com/v1/projects/{Options.ProjectId}/messages:send";
var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = JsonContent.Create(message, SC.Default.FirebaseRequest), };
var content = MakeJsonContent(message, SC.Default.FirebaseRequest);
var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = content, };
return await SendAsync(request, SC.Default.FirebaseResponse, SC.Default.FirebaseResponseProblem, cancellationToken).ConfigureAwait(false);
}
}

0 comments on commit 387b6fa

Please sign in to comment.