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

Helix API request srcgen metadata serialization #80

Merged
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
55 changes: 31 additions & 24 deletions CatCore/Helpers/JSON/TwitchHelixSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
using System.Text.Json.Serialization;
using CatCore.Models.Twitch.Helix.Responses;
using CatCore.Models.Twitch.Helix.Responses.Badges;
using CatCore.Models.Twitch.Helix.Responses.Bans;
using CatCore.Models.Twitch.Helix.Responses.Bits.Cheermotes;
using CatCore.Models.Twitch.Helix.Responses.Emotes;
using CatCore.Models.Twitch.Helix.Responses.Polls;
using CatCore.Models.Twitch.Helix.Responses.Predictions;
using HelixRequests = CatCore.Models.Twitch.Helix.Requests;
using HelixResponses = CatCore.Models.Twitch.Helix.Responses;

namespace CatCore.Helpers.JSON
{
[JsonSerializable(typeof(ResponseBase<UserData>))]
[JsonSerializable(typeof(ResponseBase<CreateStreamMarkerData>))]
[JsonSerializable(typeof(ResponseBaseWithPagination<ChannelData>))]
[JsonSerializable(typeof(ResponseBase<PollData>))]
[JsonSerializable(typeof(ResponseBaseWithPagination<PollData>))]
[JsonSerializable(typeof(ResponseBase<PredictionData>))]
[JsonSerializable(typeof(ResponseBaseWithPagination<PredictionData>))]
[JsonSerializable(typeof(ResponseBase<CheermoteGroupData>))]
[JsonSerializable(typeof(ResponseBase<BadgeData>))]
[JsonSerializable(typeof(ResponseBaseWithPagination<Stream>))]
[JsonSerializable(typeof(ResponseBaseWithTemplate<GlobalEmote>))]
[JsonSerializable(typeof(ResponseBaseWithTemplate<ChannelEmote>))]
[JsonSerializable(typeof(ResponseBase<ChatSettings>))]
[JsonSerializable(typeof(ResponseBaseWithPagination<BannedUserInfo>))]
[JsonSerializable(typeof(ResponseBase<BanUser>))]
[JsonSerializable(typeof(ResponseBase<UserChatColorData>))]
[JsonSerializable(typeof(ResponseBase<StartRaidData>))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.UserData>))]
[JsonSerializable(typeof(HelixRequests.CreateStreamMarkerRequestDto))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.CreateStreamMarkerData>))]
[JsonSerializable(typeof(HelixResponses.ResponseBaseWithPagination<HelixResponses.ChannelData>))]
[JsonSerializable(typeof(HelixRequests.Polls.PollChoice), TypeInfoPropertyName = "RequestPollChoice")]
[JsonSerializable(typeof(HelixResponses.Polls.PollChoice), TypeInfoPropertyName = "ResponsePollChoice")]
[JsonSerializable(typeof(HelixResponses.ResponseBaseWithPagination<HelixResponses.Polls.PollData>))]
[JsonSerializable(typeof(HelixRequests.Polls.CreatePollRequestDto))]
[JsonSerializable(typeof(HelixRequests.Polls.EndPollRequestDto))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.Polls.PollData>))]
[JsonSerializable(typeof(HelixRequests.Predictions.Outcome), TypeInfoPropertyName = "RequestOutcome")]
[JsonSerializable(typeof(HelixResponses.Predictions.Outcome), TypeInfoPropertyName = "ResponseOutcome")]
[JsonSerializable(typeof(HelixResponses.ResponseBaseWithPagination<HelixResponses.Predictions.PredictionData>))]
[JsonSerializable(typeof(HelixRequests.Predictions.CreatePredictionsRequestDto))]
[JsonSerializable(typeof(HelixRequests.Predictions.EndPredictionRequestDto))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.Predictions.PredictionData>))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.Bits.Cheermotes.CheermoteGroupData>))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.Badges.BadgeData>))]
[JsonSerializable(typeof(HelixResponses.ResponseBaseWithPagination<HelixResponses.Stream>))]
[JsonSerializable(typeof(HelixResponses.ResponseBaseWithTemplate<HelixResponses.Emotes.GlobalEmote>))]
[JsonSerializable(typeof(HelixResponses.ResponseBaseWithTemplate<HelixResponses.Emotes.ChannelEmote>))]
[JsonSerializable(typeof(HelixRequests.ChatSettingsRequestDto))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.ChatSettings>))]
[JsonSerializable(typeof(HelixResponses.ResponseBaseWithPagination<HelixResponses.Bans.BannedUserInfo>))]
[JsonSerializable(typeof(HelixRequests.LegacyRequestDataWrapper<HelixRequests.Bans.BanUserRequestDto>))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.Bans.BanUser>))]
[JsonSerializable(typeof(HelixRequests.SendChatAnnouncementRequestDto))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.UserChatColorData>))]
[JsonSerializable(typeof(HelixResponses.ResponseBase<HelixResponses.StartRaidData>))]
internal partial class TwitchHelixSerializerContext : JsonSerializerContext
{
}
Expand Down
44 changes: 36 additions & 8 deletions CatCore/Services/Twitch/TwitchHelixApiService.Calls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ void CheckCount(ref string[]? array, out bool hasBool)
}

var body = new CreateStreamMarkerRequestDto(userId, description);
return await PostAsync(TWITCH_HELIX_BASEURL + "streams/markers", body, TwitchHelixSerializerContext.Default.ResponseBaseCreateStreamMarkerData, cancellationToken).ConfigureAwait(false);
return await PostAsync(TWITCH_HELIX_BASEURL + "streams/markers", body,
TwitchHelixSerializerContext.Default.CreateStreamMarkerRequestDto,
TwitchHelixSerializerContext.Default.ResponseBaseCreateStreamMarkerData,
cancellationToken)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down Expand Up @@ -187,7 +191,11 @@ void CheckCount(ref string[]? array, out bool hasBool)
var body = new ChatSettingsRequestDto(emoteMode, followerMode, followerModeDurationMinutes, nonModeratorChatDelay, nonModeratorChatDelayDurationSeconds, slowMode,
slowModeWaitTimeSeconds, subscriberMode, uniqueChatMode);

return await PatchAsync(urlBuilder, body, TwitchHelixSerializerContext.Default.ResponseBaseChatSettings, cancellationToken).ConfigureAwait(false);
return await PatchAsync(urlBuilder, body,
TwitchHelixSerializerContext.Default.ChatSettingsRequestDto,
TwitchHelixSerializerContext.Default.ResponseBaseChatSettings,
cancellationToken)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down Expand Up @@ -258,7 +266,11 @@ void CheckCount(ref string[]? array, out bool hasBool)

var body = new BanUserRequestDto(userId, durationSeconds, reason);
var bodyWrapper = new LegacyRequestDataWrapper<BanUserRequestDto>(body);
return await PostAsync(urlBuilder, bodyWrapper, TwitchHelixSerializerContext.Default.ResponseBaseBanUser, cancellationToken).ConfigureAwait(false);
return await PostAsync(urlBuilder, bodyWrapper,
TwitchHelixSerializerContext.Default.LegacyRequestDataWrapperBanUserRequestDto,
TwitchHelixSerializerContext.Default.ResponseBaseBanUser,
cancellationToken)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand All @@ -279,7 +291,7 @@ public async Task<bool> SendChatAnnouncement(string broadcasterId, string messag
var urlBuilder = TWITCH_HELIX_BASEURL + "chat/announcements?broadcaster_id=" + broadcasterId + "&moderator_id=" + loggedInUser.UserId;

var body = new SendChatAnnouncementRequestDto(message, color);
return await PostAsync(urlBuilder, body, cancellationToken).ConfigureAwait(false);
return await PostAsync(urlBuilder, body, TwitchHelixSerializerContext.Default.SendChatAnnouncementRequestDto, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down Expand Up @@ -484,7 +496,11 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote,
OptionalParametersValidation(ref channelPointsVotingEnabled, ref channelPointsPerVote, 1000000);

var body = new CreatePollRequestDto(loggedInUser.UserId, title, pollChoices, duration, channelPointsVotingEnabled, channelPointsPerVote);
return await PostAsync(TWITCH_HELIX_BASEURL + "polls", body, TwitchHelixSerializerContext.Default.ResponseBasePollData, cancellationToken).ConfigureAwait(false);
return await PostAsync(TWITCH_HELIX_BASEURL + "polls", body,
TwitchHelixSerializerContext.Default.CreatePollRequestDto,
TwitchHelixSerializerContext.Default.ResponseBasePollData,
cancellationToken)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand All @@ -503,7 +519,11 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote,
}

var body = new EndPollRequestDto(loggedInUser.UserId, pollId, pollStatus);
return await PatchAsync(TWITCH_HELIX_BASEURL + "polls", body, TwitchHelixSerializerContext.Default.ResponseBasePollData, cancellationToken).ConfigureAwait(false);
return await PatchAsync(TWITCH_HELIX_BASEURL + "polls", body,
TwitchHelixSerializerContext.Default.EndPollRequestDto,
TwitchHelixSerializerContext.Default.ResponseBasePollData,
cancellationToken)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down Expand Up @@ -586,7 +606,11 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote,
}

var body = new CreatePredictionsRequestDto(loggedInUser.UserId, title, predictionOutcomes, duration);
return await PostAsync(TWITCH_HELIX_BASEURL + "predictions", body, TwitchHelixSerializerContext.Default.ResponseBasePredictionData, cancellationToken).ConfigureAwait(false);
return await PostAsync(TWITCH_HELIX_BASEURL + "predictions", body,
TwitchHelixSerializerContext.Default.CreatePredictionsRequestDto,
TwitchHelixSerializerContext.Default.ResponseBasePredictionData,
cancellationToken)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand All @@ -610,7 +634,11 @@ void OptionalParametersValidation(ref bool? voteEnabled, ref uint? costPerVote,
}

var body = new EndPredictionRequestDto(loggedInUser.UserId, predictionId, predictionStatus, winningOutcomeId);
return await PatchAsync(TWITCH_HELIX_BASEURL + "predictions", body, TwitchHelixSerializerContext.Default.ResponseBasePredictionData, cancellationToken).ConfigureAwait(false);
return await PatchAsync(TWITCH_HELIX_BASEURL + "predictions", body,
TwitchHelixSerializerContext.Default.EndPredictionRequestDto,
TwitchHelixSerializerContext.Default.ResponseBasePredictionData,
cancellationToken)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down
25 changes: 14 additions & 11 deletions CatCore/Services/Twitch/TwitchHelixApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,27 @@ private async Task<ValidationResponse> CheckUserLoggedIn()
=> CallEndpointNoBodyExpectBody(HttpMethod.Get, url, jsonResponseTypeInfo, cancellationToken);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Task<TResponse?> PostAsync<TResponse, TBody>(string url, TBody body, JsonTypeInfo<TResponse> jsonResponseTypeInfo, CancellationToken cancellationToken = default)
where TResponse : struct => CallEndpointWithBodyExpectBody(HttpMethod.Post, url, body, jsonResponseTypeInfo, cancellationToken);
private Task<TResponse?> PostAsync<TResponse, TBody>(string url, TBody body, JsonTypeInfo<TBody> jsonRequestTypeInfo, JsonTypeInfo<TResponse> jsonResponseTypeInfo, CancellationToken cancellationToken = default)
where TResponse : struct => CallEndpointWithBodyExpectBody(HttpMethod.Post, url, body, jsonRequestTypeInfo, jsonResponseTypeInfo, cancellationToken);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Task<TResponse?> PostAsync<TResponse>(string url, JsonTypeInfo<TResponse> jsonResponseTypeInfo, CancellationToken cancellationToken = default)
where TResponse : struct => CallEndpointNoBodyExpectBody(HttpMethod.Post, url, jsonResponseTypeInfo, cancellationToken);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Task<bool> PostAsync<TBody>(string url, TBody body, CancellationToken cancellationToken = default) => CallEndpointWithBodyNoBody(HttpMethod.Post, url, body, cancellationToken);
private Task<bool> PostAsync<TBody>(string url, TBody body, JsonTypeInfo<TBody> jsonRequestTypeInfo, CancellationToken cancellationToken = default)
=> CallEndpointWithBodyNoBody(HttpMethod.Post, url, body, jsonRequestTypeInfo, cancellationToken);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Task<bool> PutAsync(string url, CancellationToken cancellationToken = default) => CallEndpointNoBodyNoBody(HttpMethod.Put, url, cancellationToken);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Task<TResponse?> PatchAsync<TResponse, TBody>(string url, TBody body, JsonTypeInfo<TResponse> jsonResponseTypeInfo, CancellationToken cancellationToken = default)
where TResponse : struct => CallEndpointWithBodyExpectBody(HttpMethodPatch, url, body, jsonResponseTypeInfo, cancellationToken);
private Task<TResponse?> PatchAsync<TResponse, TBody>(string url, TBody body, JsonTypeInfo<TBody> jsonRequestTypeInfo, JsonTypeInfo<TResponse> jsonResponseTypeInfo, CancellationToken cancellationToken = default)
where TResponse : struct => CallEndpointWithBodyExpectBody(HttpMethodPatch, url, body, jsonRequestTypeInfo, jsonResponseTypeInfo, cancellationToken);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Task<bool> PatchAsync<TBody>(string url, TBody body, CancellationToken cancellationToken = default) => CallEndpointWithBodyNoBody(HttpMethodPatch, url, body, cancellationToken);
private Task<bool> PatchAsync<TBody>(string url, TBody body, JsonTypeInfo<TBody> jsonRequestTypeInfo, CancellationToken cancellationToken = default)
=> CallEndpointWithBodyNoBody(HttpMethodPatch, url, body, jsonRequestTypeInfo, cancellationToken);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Task<bool> DeleteAsync(string url, CancellationToken cancellationToken = default) => CallEndpointNoBodyNoBody(HttpMethod.Delete, url, cancellationToken);
Expand Down Expand Up @@ -221,8 +223,9 @@ private async Task<bool> CallEndpointNoBodyNoBody(HttpMethod httpMethod, string
}
}

private async Task<TResponse?> CallEndpointWithBodyExpectBody<TResponse, TBody>(HttpMethod httpMethod, string url, TBody body, JsonTypeInfo<TResponse> jsonResponseTypeInfo,
CancellationToken cancellationToken = default) where TResponse : struct
private async Task<TResponse?> CallEndpointWithBodyExpectBody<TResponse, TBody>(HttpMethod httpMethod, string url, TBody body, JsonTypeInfo<TBody> jsonRequestTypeInfo,
JsonTypeInfo<TResponse> jsonResponseTypeInfo, CancellationToken cancellationToken = default)
where TResponse : struct
{
#if DEBUG
if (string.IsNullOrWhiteSpace(url))
Expand Down Expand Up @@ -252,7 +255,7 @@ private async Task<bool> CallEndpointNoBodyNoBody(HttpMethod httpMethod, string
{
using var httpResponseMessage = await _combinedHelixPolicy.ExecuteAsync(async ct =>
{
using var jsonContent = JsonContent.Create(body);
using var jsonContent = JsonContent.Create(body, options: jsonRequestTypeInfo.Options);
using var httpRequestMessage = new HttpRequestMessage(httpMethod, url) { Content = jsonContent };
return await _helixClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -283,7 +286,7 @@ private async Task<bool> CallEndpointNoBodyNoBody(HttpMethod httpMethod, string
}
}

private async Task<bool> CallEndpointWithBodyNoBody<TBody>(HttpMethod httpMethod, string url, TBody body, CancellationToken cancellationToken = default)
private async Task<bool> CallEndpointWithBodyNoBody<TBody>(HttpMethod httpMethod, string url, TBody body, JsonTypeInfo<TBody> jsonRequestTypeInfo, CancellationToken cancellationToken = default)
{
#if DEBUG
if (string.IsNullOrWhiteSpace(url))
Expand Down Expand Up @@ -313,7 +316,7 @@ private async Task<bool> CallEndpointWithBodyNoBody<TBody>(HttpMethod httpMethod
{
using var httpResponseMessage = await _combinedHelixPolicy.ExecuteAsync(async ct =>
{
using var jsonContent = JsonContent.Create(body);
using var jsonContent = JsonContent.Create(body, options: jsonRequestTypeInfo.Options);
using var httpRequestMessage = new HttpRequestMessage(httpMethod, url) { Content = jsonContent };
return await _helixClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false);
Expand Down