diff --git a/CatCore/Helpers/JSON/TwitchHelixSerializerContext.cs b/CatCore/Helpers/JSON/TwitchHelixSerializerContext.cs new file mode 100644 index 0000000..d24e331 --- /dev/null +++ b/CatCore/Helpers/JSON/TwitchHelixSerializerContext.cs @@ -0,0 +1,20 @@ +using System.Text.Json.Serialization; +using CatCore.Models.Twitch.Helix.Responses; +using CatCore.Models.Twitch.Helix.Responses.Bits.Cheermotes; +using CatCore.Models.Twitch.Helix.Responses.Polls; +using CatCore.Models.Twitch.Helix.Responses.Predictions; + +namespace CatCore.Helpers.JSON +{ + [JsonSerializable(typeof(ResponseBase))] + [JsonSerializable(typeof(ResponseBase))] + [JsonSerializable(typeof(ResponseBaseWithPagination))] + [JsonSerializable(typeof(ResponseBase))] + [JsonSerializable(typeof(ResponseBaseWithPagination))] + [JsonSerializable(typeof(ResponseBase))] + [JsonSerializable(typeof(ResponseBaseWithPagination))] + [JsonSerializable(typeof(ResponseBase))] + internal partial class TwitchHelixSerializerContext : JsonSerializerContext + { + } +} \ No newline at end of file diff --git a/CatCore/Services/Twitch/TwitchHelixApiService.Calls.cs b/CatCore/Services/Twitch/TwitchHelixApiService.Calls.cs index b7b6a3c..5c87ad3 100644 --- a/CatCore/Services/Twitch/TwitchHelixApiService.Calls.cs +++ b/CatCore/Services/Twitch/TwitchHelixApiService.Calls.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using CatCore.Helpers.JSON; using CatCore.Models.Twitch.Helix.Requests; using CatCore.Models.Twitch.Helix.Requests.Polls; using CatCore.Models.Twitch.Helix.Requests.Predictions; @@ -62,7 +63,7 @@ void CheckCount(ref string[]? array, out bool hasBool) urlBuilder.Append("login=").Append(string.Join("&login=", loginNames!)); } - return GetAsync>(urlBuilder.ToString(), cancellationToken); + return GetAsync(urlBuilder.ToString(), TwitchHelixSerializerContext.Default.ResponseBaseUserData, cancellationToken); } /// @@ -74,7 +75,7 @@ void CheckCount(ref string[]? array, out bool hasBool) } var body = new CreateStreamMarkerRequestDto(userId, description); - return PostAsync, CreateStreamMarkerRequestDto>($"{TWITCH_HELIX_BASEURL}streams/markers", body, cancellationToken); + return PostAsync($"{TWITCH_HELIX_BASEURL}streams/markers", body, TwitchHelixSerializerContext.Default.ResponseBaseCreateStreamMarkerData, cancellationToken); } /// @@ -112,7 +113,7 @@ void CheckCount(ref string[]? array, out bool hasBool) urlBuilder.Append($"&after={continuationCursor}"); } - return GetAsync>(urlBuilder.ToString(), cancellationToken); + return GetAsync(urlBuilder.ToString(), TwitchHelixSerializerContext.Default.ResponseBaseWithPaginationChannelData, cancellationToken); } /// @@ -159,7 +160,7 @@ void CheckCount(ref string[]? array, out bool hasBool) urlBuilder.Append($"&after={continuationCursor}"); } - return GetAsync>(urlBuilder.ToString(), cancellationToken); + return GetAsync(urlBuilder.ToString(), TwitchHelixSerializerContext.Default.ResponseBaseWithPaginationPollData, cancellationToken); } /// @@ -237,7 +238,7 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote, OptionalParametersValidation(ref channelPointsVotingEnabled, ref channelPointsPerVote, 1000000); var body = new CreatePollRequestDto(userId, title, pollChoices, duration, bitsVotingEnabled, bitsPerVote, channelPointsVotingEnabled, channelPointsPerVote); - return PostAsync, CreatePollRequestDto>($"{TWITCH_HELIX_BASEURL}polls", body, cancellationToken); + return PostAsync($"{TWITCH_HELIX_BASEURL}polls", body, TwitchHelixSerializerContext.Default.ResponseBasePollData, cancellationToken); } /// @@ -262,7 +263,7 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote, } var body = new EndPollRequestDto(userId, pollId, pollStatus); - return PatchAsync, EndPollRequestDto>($"{TWITCH_HELIX_BASEURL}polls", body, cancellationToken); + return PatchAsync($"{TWITCH_HELIX_BASEURL}polls", body, TwitchHelixSerializerContext.Default.ResponseBasePollData, cancellationToken); } /// @@ -310,7 +311,7 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote, urlBuilder.Append($"&after={continuationCursor}"); } - return GetAsync>(urlBuilder.ToString(), cancellationToken); + return GetAsync(urlBuilder.ToString(), TwitchHelixSerializerContext.Default.ResponseBaseWithPaginationPredictionData, cancellationToken); } /// @@ -355,7 +356,7 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote, } var body = new CreatePredictionsRequestDto(userId, title, predictionOutcomes, duration); - return PostAsync, CreatePredictionsRequestDto>($"{TWITCH_HELIX_BASEURL}predictions", body, cancellationToken); + return PostAsync($"{TWITCH_HELIX_BASEURL}predictions", body, TwitchHelixSerializerContext.Default.ResponseBasePredictionData, cancellationToken); } /// @@ -385,7 +386,7 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote, } var body = new EndPredictionRequestDto(userId, predictionId, predictionStatus, winningOutcomeId); - return PatchAsync, EndPredictionRequestDto>($"{TWITCH_HELIX_BASEURL}predictions", body, cancellationToken); + return PatchAsync($"{TWITCH_HELIX_BASEURL}predictions", body, TwitchHelixSerializerContext.Default.ResponseBasePredictionData, cancellationToken); } /// @@ -397,7 +398,7 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote, urlBuilder.Append("?broadcaster_id=").Append(userId); } - return GetAsync>(urlBuilder.ToString(), cancellationToken); + return GetAsync(urlBuilder.ToString(), TwitchHelixSerializerContext.Default.ResponseBaseCheermoteGroupData, cancellationToken); } } } \ No newline at end of file diff --git a/CatCore/Services/Twitch/TwitchHelixApiService.cs b/CatCore/Services/Twitch/TwitchHelixApiService.cs index 277e47c..6aa5c03 100644 --- a/CatCore/Services/Twitch/TwitchHelixApiService.cs +++ b/CatCore/Services/Twitch/TwitchHelixApiService.cs @@ -4,6 +4,7 @@ using System.Net.Http; using System.Net.Http.Json; using System.Runtime.CompilerServices; +using System.Text.Json.Serialization.Metadata; using System.Threading; using System.Threading.Tasks; using CatCore.Helpers; @@ -69,7 +70,7 @@ internal TwitchHelixApiService(ILogger logger, ITwitchAuthService twitchAuthServ _combinedHelixPolicy = Policy.WrapAsync(bulkheadPolicy, enhanceYourCalmPolicy, reAuthPolicy); } - private async Task GetAsync(string url, CancellationToken? cancellationToken = null) where TResponse : struct + private async Task GetAsync(string url, JsonTypeInfo jsonResponseTypeInfo, CancellationToken? cancellationToken = null) where TResponse : struct { #if DEBUG if (string.IsNullOrWhiteSpace(url)) @@ -97,25 +98,25 @@ internal TwitchHelixApiService(ILogger logger, ITwitchAuthService twitchAuthServ return null; } - return await httpResponseMessage.Content.ReadFromJsonAsync(options: null, cancellationToken ?? default).ConfigureAwait(false); + return await httpResponseMessage.Content.ReadFromJsonAsync(jsonResponseTypeInfo, cancellationToken ?? default).ConfigureAwait(false); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private Task PostAsync(string url, TBody body, CancellationToken? cancellationToken = null) where TResponse : struct => - CallEndpointWithBodyExpectBody(HttpMethod.Post, url, body, cancellationToken); + private Task PostAsync(string url, TBody body, JsonTypeInfo jsonResponseTypeInfo, CancellationToken? cancellationToken = null) + where TResponse : struct => CallEndpointWithBodyExpectBody(HttpMethod.Post, url, body, jsonResponseTypeInfo, cancellationToken); [MethodImpl(MethodImplOptions.AggressiveInlining)] private Task PostAsync(string url, TBody body, CancellationToken? cancellationToken = null) => CallEndpointWithBodyNoBody(HttpMethod.Post, url, body, cancellationToken); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private Task PatchAsync(string url, TBody body, CancellationToken? cancellationToken = null) where TResponse : struct => - CallEndpointWithBodyExpectBody(HttpMethodPatch, url, body, cancellationToken); + private Task PatchAsync(string url, TBody body, JsonTypeInfo jsonResponseTypeInfo, CancellationToken? cancellationToken = null) + where TResponse : struct => CallEndpointWithBodyExpectBody(HttpMethodPatch, url, body, jsonResponseTypeInfo, cancellationToken); [MethodImpl(MethodImplOptions.AggressiveInlining)] private Task PatchAsync(string url, TBody body, CancellationToken? cancellationToken = null) => CallEndpointWithBodyNoBody(HttpMethodPatch, url, body, cancellationToken); - private async Task CallEndpointWithBodyExpectBody(HttpMethod httpMethod, string url, TBody body, CancellationToken? cancellationToken = null) - where TResponse : struct + private async Task CallEndpointWithBodyExpectBody(HttpMethod httpMethod, string url, TBody body, JsonTypeInfo jsonResponseTypeInfo, + CancellationToken? cancellationToken = null) where TResponse : struct { #if DEBUG if (string.IsNullOrWhiteSpace(url)) @@ -152,7 +153,7 @@ internal TwitchHelixApiService(ILogger logger, ITwitchAuthService twitchAuthServ return null; } - return await httpResponseMessage.Content.ReadFromJsonAsync(options: null, cancellationToken ?? default).ConfigureAwait(false); + return await httpResponseMessage.Content.ReadFromJsonAsync(jsonResponseTypeInfo, cancellationToken ?? default).ConfigureAwait(false); } private async Task CallEndpointWithBodyNoBody(HttpMethod httpMethod, string url, TBody body, CancellationToken? cancellationToken = null)