Skip to content

Commit

Permalink
Fix helix api requiring both client id and token
Browse files Browse the repository at this point in the history
  • Loading branch information
laurencee committed May 6, 2020
1 parent d9b6999 commit 369896c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,9 @@ public void SetAccessToken(string accessToken)
private Task<T> ExecuteRequest<T>(string request, CancellationToken cancellationToken = default)
{
HttpClient httpClient = HttpClientExtensions.CreateCompressionHttpClient();
httpClient.DefaultRequestHeaders.Add(RequestConstants.ClientIdHeaderKey, RequestConstants.ClientIdHeaderValue);
if (_accessToken != null)
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
else
httpClient.DefaultRequestHeaders.Add(RequestConstants.ClientIdHeaderKey, RequestConstants.ClientIdHeaderValue);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);

return httpClient.ExecuteRequest<T>(request, cancellationToken);
}
Expand Down
4 changes: 2 additions & 2 deletions GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("2.12.6.0")]
[assembly: AssemblyFileVersion("2.12.6.0")]
[assembly: AssemblyVersion("2.12.7.0")]
[assembly: AssemblyFileVersion("2.12.7.0")]
18 changes: 9 additions & 9 deletions Livestream.Monitor/Model/ApiClients/TwitchApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class TwitchApiClient : IApiClient
private readonly ITwitchTvV5ReadonlyClient twitchTvV5ReadonlyClient;
private readonly ITwitchTvHelixReadonlyClient twitchTvHelixClient;
private readonly ISettingsHandler settingsHandler;
private readonly HashSet<ChannelIdentifier> moniteredChannels = new HashSet<ChannelIdentifier>();
private readonly HashSet<ChannelIdentifier> monitoredChannels = new HashSet<ChannelIdentifier>();
private readonly Dictionary<string, string> gameNameToIdMap = new Dictionary<string, string>();
private readonly Dictionary<string, string> gameIdToNameMap = new Dictionary<string, string>();
private readonly Dictionary<string, User> channelIdToUserMap = new Dictionary<string, User>();
Expand Down Expand Up @@ -193,28 +193,28 @@ public async Task<List<LivestreamQueryResult>> AddChannel(ChannelIdentifier newC

if (queryResults.All(x => x.IsSuccess))
{
moniteredChannels.Add(newChannel);
monitoredChannels.Add(newChannel);
}
return queryResults;
}

public void AddChannelWithoutQuerying(ChannelIdentifier newChannel)
{
if (newChannel == null) throw new ArgumentNullException(nameof(newChannel));
moniteredChannels.Add(newChannel);
monitoredChannels.Add(newChannel);
}

public Task RemoveChannel(ChannelIdentifier channelIdentifier)
{
channelIdToUserMap.Remove(channelIdentifier.ChannelId);
moniteredChannels.Remove(channelIdentifier);
monitoredChannels.Remove(channelIdentifier);
return Task.CompletedTask;
}

public async Task<List<LivestreamQueryResult>> QueryChannels(CancellationToken cancellationToken)
{
var queryResults = new List<LivestreamQueryResult>();
if (moniteredChannels.Count == 0) return queryResults;
if (monitoredChannels.Count == 0) return queryResults;

// Twitch "get streams" call only returns online streams so to determine if the stream actually exists/is still valid, we must specifically ask for channel details.
List<Stream> onlineStreams = new List<Stream>();
Expand All @@ -226,7 +226,7 @@ public async Task<List<LivestreamQueryResult>> QueryChannels(CancellationToken c
try
{
var query = new GetStreamsQuery();
query.UserIds.AddRange(moniteredChannels.Select(x => x.ChannelId));
query.UserIds.AddRange(monitoredChannels.Select(x => x.ChannelId));
onlineStreams = await twitchTvHelixClient.GetStreams(query, cancellationToken);
success = true;
}
Expand All @@ -241,7 +241,7 @@ public async Task<List<LivestreamQueryResult>> QueryChannels(CancellationToken c
{
if (cancellationToken.IsCancellationRequested) return queryResults;

var channelIdentifier = moniteredChannels.First(x => x.ChannelId.IsEqualTo(onlineStream.UserId));
var channelIdentifier = monitoredChannels.First(x => x.ChannelId.IsEqualTo(onlineStream.UserId));
var gameName = await GetGameNameById(onlineStream.GameId);

var livestream = new LivestreamModel(onlineStream.UserId, channelIdentifier);
Expand All @@ -253,7 +253,7 @@ public async Task<List<LivestreamQueryResult>> QueryChannels(CancellationToken c
});
}

var offlineStreams = moniteredChannels.Where(x => onlineStreams.All(y => y.UserId != x.ChannelId)).ToList();
var offlineStreams = monitoredChannels.Where(x => onlineStreams.All(y => y.UserId != x.ChannelId)).ToList();
foreach (var offlineStream in offlineStreams)
{
var queryResult = new LivestreamQueryResult(offlineStream)
Expand Down Expand Up @@ -421,7 +421,7 @@ public async Task Initialize(CancellationToken cancellationToken = default)
{
// initialize user cache
var usersQuery = new GetUsersQuery();
usersQuery.UserIds.AddRange(moniteredChannels.Select(x => x.ChannelId));
usersQuery.UserIds.AddRange(monitoredChannels.Select(x => x.ChannelId));
var users = await twitchTvHelixClient.GetUsers(usersQuery, cancellationToken);
foreach (var user in users)
{
Expand Down

0 comments on commit 369896c

Please sign in to comment.