Added GraphQl Server and wg API client

This commit is contained in:
Michael Shabanov committed 2020-02-15 16:00:42 +03:00
1 parent a0ad7ca395
commit abc99c835c
25 files changed
+872 -1

No files matched your search

@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.WgApiClient
{
public interface IWargamingApiClient
{
Task<Dictionary<long, string>> FindAccounts(string nickName,
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En);
Task<AccountInfo> GetPlayerAccountInfo(long accountId,
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En);
}
}
@@ -0,0 +1,7 @@
namespace WotBlitzStatisticsPro.WgApiClient
{
public interface IWargamingApiSettings
{
string ApplicationId { get; set; }
}
}
@@ -0,0 +1,124 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public class AccountFullStatistics
{
///<summary>
/// Battles count
///</summary>
[JsonProperty("battles")]
public long? Battles { get; set; }
///<summary>
/// Capture points
///</summary>
[JsonProperty("capture_points")]
public long? CapturePoints { get; set; }
///<summary>
/// Total damage amount
///</summary>
[JsonProperty("damage_dealt")]
public long? DamageDealt { get; set; }
///<summary>
/// Total amount of received damage
///</summary>
[JsonProperty("damage_received")]
public long? DamageReceived { get; set; }
///<summary>
/// Dropped capture points
///</summary>
[JsonProperty("dropped_capture_points")]
public long? DroppedCapturePoints { get; set; }
///<summary>
/// Total amount of frags
///</summary>
[JsonProperty("frags")]
public long? Frags { get; set; }
///<summary>
/// Total amount of fras grater ten 8 lvl
///</summary>
[JsonProperty("frags8p")]
public long? Frags8P { get; set; }
///<summary>
/// Total amount of hits
///</summary>
[JsonProperty("hits")]
public long? Hits { get; set; }
///<summary>
/// Total amount of losses
///</summary>
[JsonProperty("losses")]
public long? Losses { get; set; }
///<summary>
/// Max frags per battle
///</summary>
[JsonProperty("max_frags")]
public long? MaxFrags { get; set; }
///<summary>
/// Tank id, which kills max frags per battle
///</summary>
[JsonProperty("max_frags_tank_id")]
public long? MaxFragsTankId { get; set; }
///<summary>
/// Max experience per battle
///</summary>
[JsonProperty("max_xp")]
public long? MaxXp { get; set; }
///<summary>
/// Tank Id which created max experiense per battle
///</summary>
[JsonProperty("max_xp_tank_id")]
public long? MaxXpTankId { get; set; }
///<summary>
/// Totla shots count
///</summary>
[JsonProperty("shots")]
public long? Shots { get; set; }
///<summary>
/// Totla count of spotted vehicles
///</summary>
[JsonProperty("spotted")]
public long? Spotted { get; set; }
///<summary>
/// Total count of survived battles
///</summary>
[JsonProperty("survived_battles")]
public long? SurvivedBattles { get; set; }
///<summary>
/// Total count of suvived and winned battles
///</summary>
[JsonProperty("win_and_survived")]
public long? WinAndSurvived { get; set; }
///<summary>
/// Total wins count
///</summary>
[JsonProperty("wins")]
public long? Wins { get; set; }
///<summary>
/// Total amount of experience
///</summary>
[JsonProperty("xp")]
public long? Xp { get; set; }
}
}
@@ -0,0 +1,49 @@
using Newtonsoft.Json;
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public class AccountInfo
{
///<summary>
/// Player account identifier
///</summary>
[JsonProperty("account_id")]
public long? AccountId { get; set; }
///<summary>
/// Account creation date
///</summary>
[JsonProperty("created_at")]
public int? CreatedAt { get; set; }
///<summary>
/// Last battle time
///</summary>
[JsonProperty("last_battle_time")]
public int? LastBattleTime { get; set; }
///<summary>
/// Player's nick
///</summary>
[JsonProperty("nickname")]
public string Nickname { get; set; }
///<summary>
/// Player information update date
///</summary>
[JsonProperty("updated_at")]
public int? UpdatedAt { get; set; }
///<summary>
/// Private account data
///</summary>
[JsonProperty("private")]
public AccountPrivateInfo Private { get; set; }
///<summary>
/// Player's statistics
///</summary>
[JsonProperty("statistics")]
public AccountStatistics Statistics { get; set; }
}
}
@@ -0,0 +1,69 @@
using Newtonsoft.Json;
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public class AccountPrivateInfo
{
///<summary>
/// Account ban info
///</summary>
[JsonProperty("ban_info")]
public string BanInfo { get; set; }
///<summary>
/// Account ban expiration date
///</summary>
[JsonProperty("ban_time")]
public int? BanTime { get; set; }
///<summary>
/// Total time in battles until destroy in seconds
///</summary>
[JsonProperty("battle_life_time")]
public int? BattleLifeTimeInSeconds { get; set; }
///<summary>
/// Amount of credits
///</summary>
[JsonProperty("credits")]
public long? Credits { get; set; }
///<summary>
/// Free experience
///</summary>
[JsonProperty("free_xp")]
public long? FreeXp { get; set; }
///<summary>
///Amount of gold
///</summary>
[JsonProperty("gold")]
public long? Gold { get; set; }
///<summary>
/// Is player has premium account
///</summary>
[JsonProperty("is_premium")]
public bool IsPremium { get; set; }
///<summary>
/// Premium accunt expiration time
///</summary>
[JsonProperty("premium_expires_at")]
private int? PremiumExpiresAt { get; set; }
///<summary>
///Группы контактов.
///Дополнительное поле.
///</summary>
[JsonProperty("grouped_contacts")]
public AccountPrivateInfoGroupedContacts GroupedContacts { get; set; }
///<summary>
///Ограничения аккаунта
///</summary>
[JsonProperty("restrictions")]
public AccountPrivateInfoRestrictions Restrictions { get; set; }
}
}
@@ -0,0 +1,26 @@
using Newtonsoft.Json;
using System.Collections.Generic;
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public class AccountPrivateInfoGroupedContacts
{
///<summary>
/// Blocked accountIds
///</summary>
[JsonProperty("blocked")]
public int[] Blocked { get; set; }
///<summary>
/// Groups
///</summary>
[JsonProperty("groups")]
public Dictionary<string, string> Groups { get; set; }
///<summary>
/// Ungrouped accountIds
///</summary>
[JsonProperty("ungrouped")]
public int[] Ungrouped { get; set; }
}
}
@@ -0,0 +1,13 @@
using Newtonsoft.Json;
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public class AccountPrivateInfoRestrictions
{
///<summary>
/// Clan chat ban time
///</summary>
[JsonProperty("chat_ban_time")]
public int? ChatBanTime { get; set; }
}
}
@@ -0,0 +1,20 @@
using Newtonsoft.Json;
using System.Collections.Generic;
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public class AccountStatistics
{
///<summary>
/// Amount of destroyed by player tanks.
///</summary>
[JsonProperty("frags")]
public Dictionary<string, string> Frags { get; set; }
///<summary>
/// Full player's statistics
///</summary>
[JsonProperty("all")]
public AccountFullStatistics All { get; set; }
}
}
@@ -0,0 +1,11 @@
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public enum RealmType
{
Ru,
Eu,
Na,
Asia
}
}
@@ -0,0 +1,9 @@
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public enum RequestLanguage
{
Ru,
En,
De
}
}
@@ -0,0 +1,40 @@
using Newtonsoft.Json;
namespace WotBlitzStatisticsPro.WgApiClient
{
internal class ResponseBody<T>
{
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("meta")]
public Meta Meta { get; set; }
[JsonProperty("data")]
public T Data { get; set; }
[JsonProperty("error")]
public Error Error { get; set; }
}
internal class Meta
{
[JsonProperty("count")]
public int Count { get; set; }
}
internal class Error
{
[JsonProperty("field")]
public string Field { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
}
@@ -0,0 +1,85 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.WgApiClient
{
public class WagramingApiClientBase
{
private Dictionary<RealmType, string> _blitzApiUrls = new Dictionary<RealmType, string>();
private Dictionary<RealmType, string> _wotApiUrls = new Dictionary<RealmType, string>();
private readonly HttpClient _httpClient;
private readonly IWargamingApiSettings _wargamingApiSettings;
public WagramingApiClientBase(
HttpClient httpClient,
IWargamingApiSettings wargamingApiSettings)
{
_httpClient = httpClient;
_wargamingApiSettings = wargamingApiSettings;
_blitzApiUrls[RealmType.Eu] = "https://api.wotblitz.eu/wotb/";
_blitzApiUrls[RealmType.Ru] = "https://api.wotblitz.ru/wotb/";
_blitzApiUrls[RealmType.Na] = "https://api.wotblitz.com/wotb/";
_blitzApiUrls[RealmType.Asia] = "https://api.wotblitz.asia/wotb/";
_wotApiUrls[RealmType.Eu] = "https://api.worldoftanks.eu/wot/";
_wotApiUrls[RealmType.Ru] = "https://api.worldoftanks.ru/wot/";
_wotApiUrls[RealmType.Na] = "https://api.worldoftanks.com/wot/";
_wotApiUrls[RealmType.Asia] = "https://api.worldoftanks.asia/wot/";
}
protected async Task<T> GetFromBlitzApi<T>(
RealmType realmType,
RequestLanguage language,
string method,
params string[] queryParameters)
{
string uri = GetUri(realmType, language, method, queryParameters);
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await _httpClient.GetAsync(uri);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var responseBody = JsonConvert.DeserializeObject<ResponseBody<T>>(responseString);
if (responseBody.Status == "ok")
{
return responseBody.Data;
}
if (responseBody.Status == "error")
{
var error = responseBody.Error;
var message = $"Field:{error.Field} Message:{error.Message} Value:{error.Value} Code:{error.Code}";
throw new ArgumentException(message);
}
return default(T);
}
private string GetUri(RealmType realmType, RequestLanguage language, string method, string[] queryParameters)
{
var uri = new StringBuilder($"{_blitzApiUrls[realmType]}{method}");
uri.Append("?application_id=")
.Append(_wargamingApiSettings.ApplicationId)
.Append("&language=")
.Append(language.ToString().ToLower());
if (queryParameters != null)
{
foreach (var param in queryParameters)
{
uri.Append("&")
.Append(param);
}
}
return uri.ToString();
}
}
}
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.WgApiClient
{
public class WargamingApiClient : WagramingApiClientBase, IWargamingApiClient
{
public WargamingApiClient(
HttpClient httpClient,
IWargamingApiSettings wargamingApiSettings) : base(httpClient, wargamingApiSettings)
{
}
public Task<Dictionary<long, string>> FindAccounts(string nickName,
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
throw new NotImplementedException();
}
public async Task<AccountInfo> GetPlayerAccountInfo(long accountId,
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En)
{
var account = await GetFromBlitzApi<Dictionary<string, AccountInfo>>(
realmType,
language,
"account/info/",
$"account_id={accountId}");
return account[accountId.ToString()];
}
}
}
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>