From 9acaa0534f9f73be6a53dae1d1f9818acfb4f93c Mon Sep 17 00:00:00 2001 From: Mahsaap Date: Wed, 16 Nov 2022 19:17:34 -0400 Subject: [PATCH 1/5] Add missing whispers class --- TwitchLib.Api.Helix/Helix.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/TwitchLib.Api.Helix/Helix.cs b/TwitchLib.Api.Helix/Helix.cs index dabc1548..413c0397 100644 --- a/TwitchLib.Api.Helix/Helix.cs +++ b/TwitchLib.Api.Helix/Helix.cs @@ -117,13 +117,17 @@ public class Helix /// public Teams Teams { get; } /// + /// User related Helix APIs + /// + public Users Users { get; } + /// /// Video/VOD related Helix APIs /// public Videos Videos { get; } /// - /// User related Helix APIs + /// Whispers related Helix APIs /// - public Users Users { get; } + public Whispers Whispers { get; } /// /// Creates an Instance of the Helix Class. @@ -166,6 +170,7 @@ public Helix(ILoggerFactory loggerFactory = null, IRateLimiter rateLimiter = nul Teams = new Teams(Settings, rateLimiter, http); Users = new Users(Settings, rateLimiter, http); Videos = new Videos(Settings, rateLimiter, http); + Whispers = new Whispers(Settings, rateLimiter, http); } } } From a01106525bbb391eb24fc7358dc72106f327d792 Mon Sep 17 00:00:00 2001 From: Syzuna Date: Thu, 17 Nov 2022 00:19:57 +0100 Subject: [PATCH 2/5] Revert "Add missing whispers class" --- TwitchLib.Api.Helix/Helix.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/TwitchLib.Api.Helix/Helix.cs b/TwitchLib.Api.Helix/Helix.cs index 413c0397..dabc1548 100644 --- a/TwitchLib.Api.Helix/Helix.cs +++ b/TwitchLib.Api.Helix/Helix.cs @@ -117,17 +117,13 @@ public class Helix /// public Teams Teams { get; } /// - /// User related Helix APIs - /// - public Users Users { get; } - /// /// Video/VOD related Helix APIs /// public Videos Videos { get; } /// - /// Whispers related Helix APIs + /// User related Helix APIs /// - public Whispers Whispers { get; } + public Users Users { get; } /// /// Creates an Instance of the Helix Class. @@ -170,7 +166,6 @@ public Helix(ILoggerFactory loggerFactory = null, IRateLimiter rateLimiter = nul Teams = new Teams(Settings, rateLimiter, http); Users = new Users(Settings, rateLimiter, http); Videos = new Videos(Settings, rateLimiter, http); - Whispers = new Whispers(Settings, rateLimiter, http); } } } From ca1d72d5de454bdcbeac28f840c9568c85a42e61 Mon Sep 17 00:00:00 2001 From: "Marvin G. | DotNetExe" Date: Thu, 2 Nov 2023 21:50:03 +0100 Subject: [PATCH 3/5] Error output supplemented by error from Twitch --- .../HttpCallHandlers/TwitchErrorResponse.cs | 16 ++++++++++++ .../HttpCallHandlers/TwitchHttpClient.cs | 26 ++++++++++--------- 2 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 TwitchLib.Api.Core/HttpCallHandlers/TwitchErrorResponse.cs diff --git a/TwitchLib.Api.Core/HttpCallHandlers/TwitchErrorResponse.cs b/TwitchLib.Api.Core/HttpCallHandlers/TwitchErrorResponse.cs new file mode 100644 index 00000000..ef741b74 --- /dev/null +++ b/TwitchLib.Api.Core/HttpCallHandlers/TwitchErrorResponse.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace TwitchLib.Api.Core.HttpCallHandlers +{ + public class TwitchErrorResponse + { + [JsonProperty("error")] + public string Error; + + [JsonProperty("status")] + public int Status; + + [JsonProperty("message")] + public string Message; + } +} \ No newline at end of file diff --git a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs index 0e713e75..a99303b5 100644 --- a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs +++ b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using TwitchLib.Api.Core.Common; using TwitchLib.Api.Core.Enums; using TwitchLib.Api.Core.Exceptions; @@ -122,32 +123,33 @@ public async Task RequestReturnResponseCodeAsync(string url, string method, private void HandleWebException(HttpResponseMessage errorResp) { + var deserializedError = JsonConvert.DeserializeObject(errorResp.Content.ReadAsStringAsync().Result); + switch (errorResp.StatusCode) { case HttpStatusCode.BadRequest: - throw new BadRequestException("Your request failed because either: \n 1. Your ClientID was invalid/not set. \n 2. Your refresh token was invalid. \n 3. You requested a username when the server was expecting a user ID.", errorResp); + throw new BadRequestException($"Your request failed because either: \n 1. Your ClientID was invalid/not set. \n 2. Your refresh token was invalid. \n 3. You requested a username when the server was expecting a user ID. \n {deserializedError.Message}", errorResp); case HttpStatusCode.Unauthorized: var authenticateHeader = errorResp.Headers.WwwAuthenticate; if (authenticateHeader == null || authenticateHeader.Count <= 0) - throw new BadScopeException("Your request was blocked due to bad credentials (Do you have the right scope for your access token?).", errorResp); - throw new TokenExpiredException("Your request was blocked due to an expired Token. Please refresh your token and update your API instance settings.", errorResp); + throw new BadScopeException($"Your request was blocked due to bad credentials (Do you have the right scope for your access token?). \n {deserializedError.Message}", errorResp); + throw new TokenExpiredException($"Your request was blocked due to an expired Token. Please refresh your token and update your API instance settings. \n {deserializedError.Message}", errorResp); case HttpStatusCode.NotFound: - throw new BadResourceException("The resource you tried to access was not valid.", errorResp); + throw new BadResourceException($"The resource you tried to access was not valid. \n {deserializedError.Message}", errorResp); case (HttpStatusCode)429: - errorResp.Headers.TryGetValues("Ratelimit-Reset", out var resetTime); - throw new TooManyRequestsException("You have reached your rate limit. Too many requests were made", resetTime.FirstOrDefault(), errorResp); + errorResp.Headers.TryGetValues($"Ratelimit-Reset", out var resetTime); + throw new TooManyRequestsException($"You have reached your rate limit. Too many requests were made \n {deserializedError.Message}", resetTime.FirstOrDefault(), errorResp); case HttpStatusCode.BadGateway: - throw new BadGatewayException("The API answered with a 502 Bad Gateway. Please retry your request", errorResp); + throw new BadGatewayException($"The API answered with a 502 Bad Gateway. Please retry your request \n {deserializedError.Message}", errorResp); case HttpStatusCode.GatewayTimeout: - throw new GatewayTimeoutException("The API answered with a 504 Gateway Timeout. Please retry your request", errorResp); + throw new GatewayTimeoutException($"The API answered with a 504 Gateway Timeout. Please retry your request \n {deserializedError.Message}", errorResp); case HttpStatusCode.InternalServerError: - throw new InternalServerErrorException("The API answered with a 500 Internal Server Error. Please retry your request", errorResp); + throw new InternalServerErrorException($"The API answered with a 500 Internal Server Error. Please retry your request \n {deserializedError.Message}", errorResp); case HttpStatusCode.Forbidden: - throw new BadTokenException("The token provided in the request did not match the associated user. Make sure the token you're using is from the resource owner (streamer? viewer?)", errorResp); + throw new BadTokenException($"The token provided in the request did not match the associated user. Make sure the token you're using is from the resource owner (streamer? viewer?) \n {deserializedError.Message}", errorResp); default: - throw new HttpRequestException("Something went wrong during the request! Please try again later"); + throw new HttpRequestException($"Something went wrong during the request! Please try again later \n {deserializedError.Message}"); } } - } } \ No newline at end of file From 5e3c8b5a71e12205d49ee5579e515dc7826cd3e0 Mon Sep 17 00:00:00 2001 From: "Marvin G. | DotNetExe" Date: Mon, 6 Nov 2023 19:42:30 +0100 Subject: [PATCH 4/5] Error output supplemented by error from Twitch - made HandleWebException method async - removed outdated message-content from exceptionmessage --- .../HttpCallHandlers/TwitchHttpClient.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs index a99303b5..b47fcce8 100644 --- a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs +++ b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs @@ -121,32 +121,33 @@ public async Task RequestReturnResponseCodeAsync(string url, string method, return (int)response.StatusCode; } - private void HandleWebException(HttpResponseMessage errorResp) + private async void HandleWebException(HttpResponseMessage errorResp) { - var deserializedError = JsonConvert.DeserializeObject(errorResp.Content.ReadAsStringAsync().Result); + var bodyContent = await errorResp.Content.ReadAsStringAsync(); + var deserializedError = JsonConvert.DeserializeObject(bodyContent); switch (errorResp.StatusCode) { case HttpStatusCode.BadRequest: - throw new BadRequestException($"Your request failed because either: \n 1. Your ClientID was invalid/not set. \n 2. Your refresh token was invalid. \n 3. You requested a username when the server was expecting a user ID. \n {deserializedError.Message}", errorResp); + throw new BadRequestException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.Unauthorized: var authenticateHeader = errorResp.Headers.WwwAuthenticate; if (authenticateHeader == null || authenticateHeader.Count <= 0) - throw new BadScopeException($"Your request was blocked due to bad credentials (Do you have the right scope for your access token?). \n {deserializedError.Message}", errorResp); - throw new TokenExpiredException($"Your request was blocked due to an expired Token. Please refresh your token and update your API instance settings. \n {deserializedError.Message}", errorResp); + throw new BadScopeException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); + throw new TokenExpiredException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.NotFound: - throw new BadResourceException($"The resource you tried to access was not valid. \n {deserializedError.Message}", errorResp); + throw new BadResourceException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case (HttpStatusCode)429: errorResp.Headers.TryGetValues($"Ratelimit-Reset", out var resetTime); - throw new TooManyRequestsException($"You have reached your rate limit. Too many requests were made \n {deserializedError.Message}", resetTime.FirstOrDefault(), errorResp); + throw new TooManyRequestsException($"{deserializedError.Error} - {deserializedError.Message}", resetTime.FirstOrDefault(), errorResp); case HttpStatusCode.BadGateway: - throw new BadGatewayException($"The API answered with a 502 Bad Gateway. Please retry your request \n {deserializedError.Message}", errorResp); + throw new BadGatewayException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.GatewayTimeout: - throw new GatewayTimeoutException($"The API answered with a 504 Gateway Timeout. Please retry your request \n {deserializedError.Message}", errorResp); + throw new GatewayTimeoutException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.InternalServerError: - throw new InternalServerErrorException($"The API answered with a 500 Internal Server Error. Please retry your request \n {deserializedError.Message}", errorResp); + throw new InternalServerErrorException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); case HttpStatusCode.Forbidden: - throw new BadTokenException($"The token provided in the request did not match the associated user. Make sure the token you're using is from the resource owner (streamer? viewer?) \n {deserializedError.Message}", errorResp); + throw new BadTokenException($"{deserializedError.Error} - {deserializedError.Message}", errorResp); default: throw new HttpRequestException($"Something went wrong during the request! Please try again later \n {deserializedError.Message}"); } From dc3468027a874ca5a5ce6c9b0653ed5ccc31dc65 Mon Sep 17 00:00:00 2001 From: DotNetExe | Marvin <71598937+DotNetExe@users.noreply.github.com> Date: Tue, 7 Nov 2023 17:28:24 +0100 Subject: [PATCH 5/5] Update TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs Co-authored-by: neon-sunset --- TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs index b47fcce8..8973577d 100644 --- a/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs +++ b/TwitchLib.Api.Core/HttpCallHandlers/TwitchHttpClient.cs @@ -121,7 +121,7 @@ public async Task RequestReturnResponseCodeAsync(string url, string method, return (int)response.StatusCode; } - private async void HandleWebException(HttpResponseMessage errorResp) + private async Task HandleWebException(HttpResponseMessage errorResp) { var bodyContent = await errorResp.Content.ReadAsStringAsync(); var deserializedError = JsonConvert.DeserializeObject(bodyContent);