From 387b6fa3078b2d694faed42bc408e6d26f82ce3a Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Tue, 16 Jan 2024 13:04:38 +0300 Subject: [PATCH] Allow passing media type when making JSON content --- src/Tingle.Extensions.Http/AbstractHttpApiClient.cs | 10 +++++++--- .../Apple/ApnsNotifier.cs | 3 +-- .../FcmLegacy/FcmLegacyNotifier.cs | 4 ++-- .../Firebase/FirebaseNotifier.cs | 4 ++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Tingle.Extensions.Http/AbstractHttpApiClient.cs b/src/Tingle.Extensions.Http/AbstractHttpApiClient.cs index 5f7d62b..2c62ed2 100644 --- a/src/Tingle.Extensions.Http/AbstractHttpApiClient.cs +++ b/src/Tingle.Extensions.Http/AbstractHttpApiClient.cs @@ -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; @@ -244,25 +245,28 @@ protected virtual async Task> SendAsyncCreate with JSON content from the provided . /// The type of the value to serialize. /// The object to to write + /// The media type to use for the content. /// A instance. [RequiresUnreferencedCode(MessageStrings.SerializationUnreferencedCodeMessage)] [RequiresDynamicCode(MessageStrings.SerializationRequiresDynamicCodeMessage)] - protected virtual HttpContent MakeJsonContent(TValue value) => JsonContent.Create(value, options: Options.SerializerOptions); + protected virtual HttpContent MakeJsonContent(TValue value, MediaTypeHeaderValue? mediaType = null) => JsonContent.Create(value, mediaType, options: Options.SerializerOptions); /// Create with JSON content from the provided . /// The object to to write /// The type of the value to serialize. + /// The media type to use for the content. /// A instance. [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); /// Create with JSON content from the provided . /// The type of the value to serialize. /// The object to to write /// Metadata about the type to convert. + /// The media type to use for the content. /// A instance. - protected virtual HttpContent MakeJsonContent(TValue value, JsonTypeInfo jsonTypeInfo) => JsonContent.Create(value, jsonTypeInfo); + protected virtual HttpContent MakeJsonContent(TValue value, JsonTypeInfo jsonTypeInfo, MediaTypeHeaderValue? mediaType = null) => JsonContent.Create(value, jsonTypeInfo, mediaType); /// Reads the UTF-8 encoded text representing a single JSON value into a . /// The type to deserialize the JSON value into. diff --git a/src/Tingle.Extensions.PushNotifications/Apple/ApnsNotifier.cs b/src/Tingle.Extensions.PushNotifications/Apple/ApnsNotifier.cs index c8664af..530ba81 100644 --- a/src/Tingle.Extensions.PushNotifications/Apple/ApnsNotifier.cs +++ b/src/Tingle.Extensions.PushNotifications/Apple/ApnsNotifier.cs @@ -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; @@ -66,7 +65,7 @@ public virtual Task> 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); } diff --git a/src/Tingle.Extensions.PushNotifications/FcmLegacy/FcmLegacyNotifier.cs b/src/Tingle.Extensions.PushNotifications/FcmLegacy/FcmLegacyNotifier.cs index e4b0234..e6b5dbb 100644 --- a/src/Tingle.Extensions.PushNotifications/FcmLegacy/FcmLegacyNotifier.cs +++ b/src/Tingle.Extensions.PushNotifications/FcmLegacy/FcmLegacyNotifier.cs @@ -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; @@ -54,7 +53,8 @@ protected virtual async Task> SendAsync