Files

248 lines
8.9 KiB
C#
Raw Permalink Normal View History

2020-04-14 11:16:51 +02:00
using System.Collections.Generic;
2020-06-06 15:54:23 +02:00
using System.Linq;
2020-02-15 16:00:42 +03:00
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
2020-05-23 11:43:14 +02:00
using WotBlitzStatisticsPro.Common;
using WotBlitzStatisticsPro.Common.Model;
2020-02-15 16:00:42 +03:00
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.WgApiClient
{
2020-05-29 23:03:32 +02:00
public class WargamingApiClient : WagramingApiClientBase,
IWargamingApiClient,
2020-09-18 16:07:25 +02:00
IWargamingDictionariesApiClient,
2021-10-18 17:47:06 +03:00
IWargamingTanksApiClient,
IWargamingAuthenticationClient
2020-05-29 23:03:32 +02:00
{
2020-02-15 16:00:42 +03:00
public WargamingApiClient(
HttpClient httpClient,
IWargamingApiSettings wargamingApiSettings,
ILogger<WagramingApiClientBase> logger) : base(httpClient, wargamingApiSettings, logger)
2020-02-15 16:00:42 +03:00
{
}
2020-05-29 23:03:32 +02:00
#region Search methods
2020-12-27 12:39:25 +01:00
public async Task<List<WotAccountListResponse>?> FindAccounts(string nickName,
2020-02-15 16:00:42 +03:00
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
2020-04-14 11:16:51 +02:00
return await GetFromBlitzApi<List<WotAccountListResponse>>(
realmType,
language,
"account/list/",
$"search={nickName}").ConfigureAwait(false);
}
2020-12-27 12:39:25 +01:00
public async Task<List<WotClanListResponse>?> FindClans(string searchString,
2020-05-19 14:22:47 +02:00
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
return await GetFromBlitzApi<List<WotClanListResponse>>(
realmType,
language,
"clans/list/",
$"search={searchString}").ConfigureAwait(false);
}
2020-05-29 23:03:32 +02:00
#endregion
2020-02-15 16:00:42 +03:00
2020-05-29 23:03:32 +02:00
#region Encyclopedia methods
public async Task<(
2020-12-27 12:39:25 +01:00
WotEncyclopediaInfoResponse?,
WotClanMembersDictionaryResponse?)> GetStaticDictionariesAsync(
2020-05-29 23:03:32 +02:00
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
var encyclopedia = await GetFromBlitzApi<WotEncyclopediaInfoResponse>(
realmType,
language,
"encyclopedia/info/"
).ConfigureAwait(false);
var clanGlossaryResponse = await GetFromBlitzApi<WotClanMembersDictionaryResponse>(
realmType,
language,
"clans/glossary/");
return (encyclopedia, clanGlossaryResponse);
}
2020-12-27 12:39:25 +01:00
public async Task<List<WotEncyclopediaAchievementsResponse>?> GetAchievementsDictionary(RealmType realmType = RealmType.Ru, RequestLanguage language = RequestLanguage.En)
2020-06-06 15:54:23 +02:00
{
var response = await GetFromBlitzApi<Dictionary<string, WotEncyclopediaAchievementsResponse>>(
realmType,
language,
"encyclopedia/achievements/").ConfigureAwait(false);
2020-12-27 12:39:25 +01:00
return response?.Values.ToList();
2020-06-06 15:54:23 +02:00
}
2020-05-29 23:03:32 +02:00
2020-12-27 12:39:25 +01:00
public async Task<List<WotEncyclopediaVehiclesResponse>?> GetVehicles(RealmType realmType = RealmType.Ru, RequestLanguage language = RequestLanguage.En)
2020-06-07 14:39:03 +02:00
{
var response = await GetFromBlitzApi<Dictionary<string, WotEncyclopediaVehiclesResponse>>(
realmType,
language,
"encyclopedia/vehicles/").ConfigureAwait(false);
2020-12-27 12:39:25 +01:00
return response?.Values.ToList();
2020-06-07 14:39:03 +02:00
}
2020-05-29 23:03:32 +02:00
#endregion
public async Task<WotAccountInfo?> GetPlayerAccountInfo(
long accountId,
2020-02-15 16:00:42 +03:00
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En,
string? authenticationToken = null)
2020-02-15 16:00:42 +03:00
{
var account = authenticationToken == null
? await GetFromBlitzApi<Dictionary<string, WotAccountInfo>>(
realmType,
language,
"account/info/",
$"account_id={accountId}").ConfigureAwait(false)
: await GetFromBlitzApi<Dictionary<string, WotAccountInfo>>(
realmType,
language,
"account/info/",
$"account_id={accountId}",
$"access_token={authenticationToken}").ConfigureAwait(false);
2020-12-27 12:39:25 +01:00
return account?[accountId.ToString()];
2020-02-15 16:00:42 +03:00
}
2020-02-25 00:46:02 +03:00
2021-05-12 16:30:23 +02:00
public async Task<List<WotAccountInfo>?> GetShortPlayerAccountsInfo(long[] accountIds, RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En, string? authenticationToken = null)
{
var accounts = await GetFromBlitzApi<Dictionary<string, WotAccountInfo>>(
realmType,
language,
"account/info/",
$"account_id={string.Join(',', accountIds)}&fields=account_id,nickname,last_battle_time,statistics.all.battles,statistics.all.wins").ConfigureAwait(false);
return accounts?.Values.ToList();
}
public async Task<ClanAccountInfo?> GetPlayerClanInfo(long accountId,
2020-02-25 00:46:02 +03:00
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
var clanInfo = await GetFromBlitzApi<Dictionary<string, ClanAccountInfo>>(
realmType,
language,
"clans/accountinfo/",
$"account_id={accountId}").ConfigureAwait(false);
2020-12-27 12:39:25 +01:00
if (clanInfo != null && clanInfo.ContainsKey(accountId.ToString()))
2020-02-25 00:46:02 +03:00
{
return clanInfo[accountId.ToString()];
}
return null;
}
2020-03-06 17:33:40 +01:00
2020-12-27 12:39:25 +01:00
public async Task<ClanInfo?> GetClanInfo(
2020-03-06 17:33:40 +01:00
long clanId,
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
var clanInfo = await GetFromBlitzApi<Dictionary<string, ClanInfo>>(
realmType,
language,
"clans/info/",
$"clan_id={clanId}").ConfigureAwait(false);
2020-12-27 12:39:25 +01:00
if (clanInfo != null && clanInfo.ContainsKey(clanId.ToString()))
2020-03-06 17:33:40 +01:00
{
return clanInfo[clanId.ToString()];
}
return null;
}
2021-05-12 16:30:23 +02:00
public async Task<List<ClanAccountInfo>?> GetBulkPlayerClans(long[] accountIds, RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
2021-05-15 22:23:49 +02:00
if (accountIds.Length == 0)
{
return null;
}
2021-05-12 16:30:23 +02:00
var clanInfo = await GetFromBlitzApi<Dictionary<string, ClanAccountInfo>>(
realmType,
language,
"clans/accountinfo/",
$"account_id={string.Join(',', accountIds)}&fields=account_id,clan_id,account_name").ConfigureAwait(false);
return clanInfo?.Values.Where(c => c != null).ToList();
}
2020-03-19 22:00:47 +01:00
2021-05-12 16:30:23 +02:00
public async Task<List<ClanInfo>?> GetShortClansInfo(long[] clanIds, RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
2021-05-15 22:23:49 +02:00
if (clanIds.Length == 0)
{
return null;
}
2021-05-12 16:30:23 +02:00
var clanInfo = await GetFromBlitzApi<Dictionary<string, ClanInfo>>(
realmType,
language,
"clans/info/",
$"clan_id={string.Join(',', clanIds)}&fields=clan_id,tag").ConfigureAwait(false);
return clanInfo?.Values.ToList();
}
2020-05-29 23:03:32 +02:00
2020-09-18 16:07:25 +02:00
#region Tanks methods
2020-05-29 23:03:32 +02:00
2020-12-27 12:39:25 +01:00
public async Task<List<WotAccountTanksStatistics>?> GetPlayerAccountTanksInfo(
2020-09-18 16:07:25 +02:00
long accountId,
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
var tanks = await GetFromBlitzApi<Dictionary<string, List<WotAccountTanksStatistics>>>(
realmType,
language,
"tanks/stats/",
$"account_id={accountId}").ConfigureAwait(false);
2020-12-27 12:39:25 +01:00
return tanks?[accountId.ToString()] ?? new List<WotAccountTanksStatistics>();
2020-09-18 16:07:25 +02:00
}
2021-10-18 17:47:06 +03:00
#endregion
#region Authentication
public string LoginUrl(RealmType realm)
{
return GetWotUri(realm, "auth/login/", null);
}
2020-05-29 23:03:32 +02:00
2021-10-19 22:24:29 +03:00
public async Task<WargamingProlongInfo> ProlongAuthToken(RealmType realm, string oldToken)
{
// https://api.worldoftanks.ru/wot/auth/prolongate/?application_id=98805e6ac535e88491ae64d37b764370&access_token=1234566
string prolongUri = GetWotUri(
realm,
"auth/prolongate/",
new string[] { $"access_token={oldToken}" });
var prolongResult = await CallWgApi<WotAuthProlongateResponse>(prolongUri, true);
return new WargamingProlongInfo
{
AccessToken = prolongResult!.AccessToken,
AccountId = prolongResult.AccountId,
ExpirationTimeStamp = prolongResult.ExpirationTimeStamp
};
}
public async Task Logout(RealmType realm, string token)
{
// https://api.worldoftanks.ru/wot/auth/logout/?application_id=adc1387489cf9fc8d9a1d85dbd27763d
string prolongUri = GetWotUri(
realm,
"auth/logout/",
new string[] { $"access_token={token}" });
await CallWgApi<WotAuthProlongateResponse>(prolongUri, true);
}
2020-09-18 16:07:25 +02:00
#endregion
2020-05-29 23:03:32 +02:00
}
2020-02-15 16:00:42 +03:00
}