-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchStatePlatform.cs
67 lines (57 loc) · 2.87 KB
/
MatchStatePlatform.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace GeniusSports.AblyMatchStateSubscriberExample
{
class MatchStatePlatform
{
private readonly Options options;
private readonly UrlProvider urlProvider;
private readonly MatchStatePlatformAuth auth;
public MatchStatePlatform(Options options, UrlProvider urlProvider, MatchStatePlatformAuth auth)
{
this.options = options;
this.urlProvider = urlProvider;
this.auth = auth;
}
/// <summary>
/// Gets scheduled fixtures between <paramref name="from"/> and <paramref name="to"/> for a given
/// <paramref name="sourceId"/> and <paramref name="sportId"/>.
/// </summary>
public async Task<IList<string>> GetScheduledFixtureIds(string sourceId, int sportId, DateTime from, DateTime to)
{
var baseUrl = urlProvider.MatchStatePlatformBaseUrl;
var url = $"{baseUrl}/sources/{sourceId}/sports/{sportId}/schedule?from={from:s}&to={to:s}";
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", await auth.GetAccessToken());
httpClient.DefaultRequestHeaders.Add("x-api-key", options.ApiKey);
var response = await httpClient.GetAsync(url);
var responseBody = await response.Content.ReadAsStringAsync();
var schedule = JsonConvert.DeserializeObject<JArray>(responseBody);
return schedule != null
? schedule.Select(x => x["fixtureId"].ToString()).ToList()
: new List<string>();
}
/// <summary>
/// Gets Ably channel name and access token for given <paramref name="sourceId"/>, <paramref name="sportId"/>
/// and <paramref name="fixtureId"/>.
/// Channel name and access token are needed for subscribing for Ably match state feed.
/// </summary>
public async Task<(string channelName, string accessToken)> GetLiveAccess(string sourceId, int sportId, int fixtureId)
{
var baseUrl = urlProvider.MatchStatePlatformBaseUrl;
var url = $"{baseUrl}/sources/{sourceId}/sports/{sportId}/fixtures/{fixtureId}/liveaccess";
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", await auth.GetAccessToken());
httpClient.DefaultRequestHeaders.Add("x-api-key", options.ApiKey);
var response = await httpClient.GetAsync(url);
var responseBody = await response.Content.ReadAsStringAsync();
var liveAccess = JsonConvert.DeserializeObject<JObject>(responseBody);
return (liveAccess["channelName"].ToString(), liveAccess["accessToken"].ToString());
}
}
}