Skip to content

Commit

Permalink
Ensure success status code after every HttpClient.SendAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Apr 3, 2023
1 parent 479ac7a commit 2f41ab3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions TwitchDownloaderCore/TwitchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static async Task<GqlVideoResponse> GetVideoInfo(int videoId)
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<GqlVideoResponse>();
}

Expand All @@ -49,6 +50,7 @@ public static async Task<GqlVideoTokenResponse> GetVideoToken(int videoId, strin
if (authToken != null && authToken != "")
request.Headers.Add("Authorization", "OAuth " + authToken);
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<GqlVideoTokenResponse>();
}

Expand All @@ -74,6 +76,7 @@ public static async Task<GqlClipResponse> GetClipInfo(object clipId)
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<GqlClipResponse>();
}

Expand All @@ -87,6 +90,7 @@ public static async Task<List<GqlClipTokenResponse>> GetClipLinks(string clipId)
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<List<GqlClipTokenResponse>>();
}

Expand All @@ -100,6 +104,7 @@ public static async Task<GqlVideoSearchResponse> GetGqlVideos(string channelName
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<GqlVideoSearchResponse>();
}

Expand All @@ -113,6 +118,7 @@ public static async Task<GqlClipSearchResponse> GetGqlClips(string channelName,
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<GqlClipSearchResponse>();
}

Expand Down Expand Up @@ -148,6 +154,7 @@ private static async Task GetBttvEmoteData(int streamerId, List<EmoteResponseIte
{
var globalEmoteRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("https://api.betterttv.net/3/cached/emotes/global", UriKind.Absolute));
using var globalEmoteResponse = await httpClient.SendAsync(globalEmoteRequest, HttpCompletionOption.ResponseHeadersRead);
globalEmoteResponse.EnsureSuccessStatusCode();
var BTTV = await globalEmoteResponse.Content.ReadFromJsonAsync<List<BTTVEmote>>();

//Channel might not have BTTV emotes
Expand Down Expand Up @@ -177,6 +184,7 @@ private static async Task GetFfzEmoteData(int streamerId, List<EmoteResponseItem
{
var globalEmoteRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("https://api.betterttv.net/3/cached/frankerfacez/emotes/global", UriKind.Absolute));
using var globalEmoteResponse = await httpClient.SendAsync(globalEmoteRequest, HttpCompletionOption.ResponseHeadersRead);
globalEmoteResponse.EnsureSuccessStatusCode();
var FFZ = await globalEmoteResponse.Content.ReadFromJsonAsync<List<FFZEmote>>();

//Channel might not have FFZ emotes
Expand Down Expand Up @@ -205,6 +213,7 @@ private static async Task GetStvEmoteData(int streamerId, List<EmoteResponseItem
{
var globalEmoteRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("https://7tv.io/v3/emote-sets/global", UriKind.Absolute));
using var globalEmoteResponse = await httpClient.SendAsync(globalEmoteRequest, HttpCompletionOption.ResponseHeadersRead);
globalEmoteResponse.EnsureSuccessStatusCode();
var globalEmoteObject = await globalEmoteResponse.Content.ReadFromJsonAsync<STVGlobalEmoteResponse>();
var stvEmotes = globalEmoteObject.emotes;

Expand Down Expand Up @@ -437,10 +446,12 @@ public static async Task<List<TwitchEmote>> GetEmotes(List<Comment> comments, st
// TODO: would want to make this configurable as we do for emotes though...
var globalBadgeRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("https://badges.twitch.tv/v1/badges/global/display", UriKind.Absolute));
using var globalBadgeResponse = await httpClient.SendAsync(globalBadgeRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
globalBadgeResponse.EnsureSuccessStatusCode();
var globalBadges = await globalBadgeResponse.Content.ReadFromJsonAsync<TwitchBadgeResponse>(cancellationToken: cancellationToken);

var subBadgeRequest = new HttpRequestMessage(HttpMethod.Get, new Uri($"https://badges.twitch.tv/v1/badges/channels/{streamerId}/display", UriKind.Absolute));
using var subBadgeResponse = await httpClient.SendAsync(subBadgeRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
subBadgeResponse.EnsureSuccessStatusCode();
var subBadges = await subBadgeResponse.Content.ReadFromJsonAsync<TwitchBadgeResponse>(cancellationToken: cancellationToken);

List<EmbedChatBadge> badges = new List<EmbedChatBadge>();
Expand Down Expand Up @@ -643,6 +654,7 @@ public static async Task<List<CheerEmote>> GetBits(List<Comment> comments, strin
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var cheerResponseMessage = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
cheerResponseMessage.EnsureSuccessStatusCode();
var cheerResponse = await cheerResponseMessage.Content.ReadFromJsonAsync<GqlCheerResponse>(cancellationToken: cancellationToken);

string bitFolder = Path.Combine(cacheFolder, "bits");
Expand Down Expand Up @@ -814,6 +826,7 @@ public static async Task<GqlUserInfoResponse> GetUserInfo(List<string> idList)
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<GqlUserInfoResponse>();
}

Expand Down Expand Up @@ -890,6 +903,7 @@ public static async Task<GqlVideoChapterResponse> GetVideoChapters(int videoId)
};
request.Headers.Add("Client-ID", "kimne78kx3ncx6brgo4mv6wki5h1ko");
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<GqlVideoChapterResponse>();
}
}
Expand Down

0 comments on commit 2f41ab3

Please sign in to comment.