-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApiBase.cs
More file actions
75 lines (66 loc) · 3.01 KB
/
ApiBase.cs
File metadata and controls
75 lines (66 loc) · 3.01 KB
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
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Tiltify
{
public class ApiBase
{
internal const string BaseTiltifyAPI = "https://tiltify.com/api";
private readonly ApiSettings settings;
private readonly IRateLimiter rateLimiter;
private readonly IHttpCallHandler http;
private readonly JsonSerializerSettings jsonDeserializer = new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
public ApiBase(ApiSettings settings, IRateLimiter rateLimiter, IHttpCallHandler http)
{
this.settings = settings;
this.rateLimiter = rateLimiter;
this.http = http;
}
protected async Task<string> TiltifyGetAsync(string resource, ApiVersion api, List<KeyValuePair<string, string>> getParams = null, string customBase = null)
{
var url = ConstructResourceUrl(resource, getParams, api, customBase);
var accessToken = settings.OAuthToken;
return await rateLimiter.Perform(async () => (await http.GeneralRequestAsync(url, "GET", null, api, accessToken).ConfigureAwait(false)).Value).ConfigureAwait(false);
}
protected async Task<T> TiltifyGetGenericAsync<T>(string resource, ApiVersion api, List<KeyValuePair<string, string>> getParams = null, string customBase = null)
{
var url = ConstructResourceUrl(resource, getParams, api, customBase);
var accessToken = settings.OAuthToken;
return await rateLimiter.Perform(async () => JsonConvert.DeserializeObject<T>((await http.GeneralRequestAsync(url, "GET", null, api, accessToken).ConfigureAwait(false)).Value, jsonDeserializer)).ConfigureAwait(false);
}
private string ConstructResourceUrl(string resource = null, List<KeyValuePair<string, string>> getParams = null, ApiVersion api = ApiVersion.V3, string overrideUrl = null)
{
var url = "";
if (overrideUrl == null)
{
if (resource == null)
throw new Exception("Cannot pass null resource with null override url");
switch (api)
{
case ApiVersion.V3:
url = $"{BaseTiltifyAPI}/v3{resource}";
break;
}
}
else
{
url = resource == null ? overrideUrl : $"{overrideUrl}{resource}";
}
if (getParams != null)
{
for (var i = 0; i < getParams.Count; i++)
{
if (i == 0)
url += $"?{getParams[i].Key}={Uri.EscapeDataString(getParams[i].Value)}";
else
url += $"&{getParams[i].Key}={Uri.EscapeDataString(getParams[i].Value)}";
}
}
return url;
}
}
}