-
Notifications
You must be signed in to change notification settings - Fork 4
/
API.cs
148 lines (133 loc) · 6.28 KB
/
API.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using CODBO4.Enums;
using CODBO4.Models;
using CODBO4.Models.Match;
using CODBO4.Models.Profile;
using Newtonsoft.Json;
using Blackout = CODBO4.Models.Profile.Blackout;
using Multiplayer = CODBO4.Models.Profile.Multiplayer;
namespace CODBO4
{
public static class Api
{
/// <summary>
/// Get user data from http://bo4tracker.com, will return successful if stats are available
/// </summary>
/// <param name="username">Username of the user</param>
/// <param name="platform">Platform the user is on</param>
/// <returns>User data</returns>
public static async Task<Validate> ValidateUserAsync(string username, Platform platform)
{
var url = $"{Utilities.ValidateUrl}/{username}/{platform.GetDescription()}";
return await DownloadDataAsync<Validate>(url).ConfigureAwait(false);
}
/// <summary>
/// Get user's profile stats for the specified mode
/// </summary>
/// <param name="username">Username of the user</param>
/// <param name="platform">Platform the user is on</param>
/// <param name="mode">Type of stats to fetch</param>
/// <returns>Profile data</returns>
public static async Task<Multiplayer> GetProfileAsync(string username, Platform platform, Mode mode)
{
var url = $"{Utilities.StatsUrl}/{username}/{platform.GetDescription()}?type={mode}";
return (mode == Mode.Multiplayer)
? await DownloadDataAsync<Multiplayer>(url).ConfigureAwait(false)
: await DownloadDataAsync<Blackout>(url).ConfigureAwait(false);
}
/// <summary>
/// Get user's profile stats for the specified mode
/// </summary>
/// <param name="username">Username of the user</param>
/// <param name="userid">Id the user</param>
/// <param name="platform">Platform the user is on</param>
/// <param name="mode">Type of stats to fetch</param>
/// <returns>Profile data</returns>
public static async Task<Multiplayer> GetProfileAsync(string username, long userid, Platform platform, Mode mode)
{
var url = $"{Utilities.StatsUrl}/{username}/{platform.GetDescription()}?type={mode}&u={userid}";
return mode == Mode.Multiplayer
? await DownloadDataAsync<Multiplayer>(url).ConfigureAwait(false)
: await DownloadDataAsync<Blackout>(url).ConfigureAwait(false);
}
/// <summary>
/// Get user's last 20 matches for the specified mode
/// </summary>
/// <param name="username"></param>
/// <param name="platform"></param>
/// <param name="mode"></param>
/// <returns>Recent matches data</returns>
public static async Task<Multiplayer> GetUserMatchesAsync(string username, Platform platform, Mode mode)
{
var url = $"{Utilities.UserMatchesUrl}/{username}/{platform.GetDescription()}?type={mode}";
return mode == Mode.Multiplayer
? await DownloadDataAsync<Multiplayer>(url).ConfigureAwait(false)
: await DownloadDataAsync<Blackout>(url).ConfigureAwait(false);
}
/// <summary>
/// Get up to the last 100 recent matches played
/// </summary>
/// <param name="rows">Number of rows to fetch</param>
/// <returns>Recent matches data</returns>
public static async Task<RecentMatches> GetRecentMatchesAsync(string rows)
{
var url = $"{Utilities.RecentMatchesUrl}?rows={rows}";
return await DownloadDataAsync<RecentMatches>(url).ConfigureAwait(false);
}
/// <summary>
/// Get matches using match id
/// </summary>
/// <param name="matchId">Id of the match to fetch</param>
/// <returns>Match data</returns>
public static async Task<Matches> GetMatchAsync(string matchId)
{
var url = $"{Utilities.MatchesUrl}?id={matchId}";
return await DownloadDataAsync<Matches>(url).ConfigureAwait(false);
}
/// <summary>
/// Get leader board data based on scope
/// </summary>
/// <param name="platform">Platform to get leader boards for</param>
/// <param name="scope">Type of scope to search for</param>
/// <param name="rows">Number of rows to fetch (100 Max)</param>
/// <returns>LeaderBoard data</returns>
public static async Task<Leaderboards> GetLeaderBoardAsync(Platform platform, Scope scope, int rows)
{
var url = $"{Utilities.LeaderBoardsUrl}/{platform.GetDescription()}/{scope.GetDescription()}?rows={rows}";
return await DownloadDataAsync<Leaderboards>(url).ConfigureAwait(false);
}
/// <summary>
/// Get username from their user id
/// </summary>
/// <param name="userid">User id's to fetch (500 Max)</param>
/// <returns>Username data</returns>
public static async Task<User> GetUserByIdAsync(IEnumerable<long> userid)
{
var userIds = userid.Aggregate("", (current, id) => current + $"?id={id}&");
var url = $"{Utilities.UseridToUsernameUrl}?{userIds}";
return await DownloadDataAsync<User>(url).ConfigureAwait(false);
}
private static async Task<T> DownloadDataAsync<T>(string url)
{
var uri = Uri.EscapeUriString(url);
using (var client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(uri).ConfigureAwait(false))
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception($"Bad response {response.StatusCode}");
using (HttpContent content = response.Content)
{
var responseData = await content.ReadAsStringAsync().ConfigureAwait(false);
if (Utilities.IsValidResponse(responseData))
return JsonConvert.DeserializeObject<T>(responseData);
throw new Exception(JsonConvert.DeserializeObject(responseData).ToString());
}
}
}
}
}