feat: prolong token mutation added

This commit is contained in:
Michael Shabanov committed 2021-10-19 22:24:29 +03:00
1 parent 11486b7303
commit c94a1033c7
7 files changed
+132 -15

No files matched your search

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WotBlitzStatisticsPro.Common.Model
{
/// <summary>
/// Prolong Wargaming Auth token response
/// </summary>
public class WargamingProlongInfo
{
/// <summary>
/// Player Account Id
/// </summary>
public long AccountId { get; set; }
/// <summary>
/// New access token
/// </summary>
public string AccessToken { get; set; } = string.Empty;
/// <summary>
/// New expiration unix timestamp
/// </summary>
public int ExpirationTimeStamp { get; set; }
}
}
@@ -0,0 +1,29 @@
using HotChocolate.Types;
using System.Threading.Tasks;
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.WgApiClient;
namespace WotBlitzStatisticsPro.GraphQl.Mutation
{
[ExtendObjectType(Name = "Mutation")]
public class WargamingAuthMutation
{
private readonly IWargamingAuthenticationClient _wargamingAuthenticationClient;
public WargamingAuthMutation(IWargamingAuthenticationClient wargamingAuthenticationClient)
{
_wargamingAuthenticationClient = wargamingAuthenticationClient;
}
/// <summary>
/// Prolongates Wargaming authentication token
/// </summary>
/// <param name="realm">Wargaming realm</param>
/// <param name="oldToken">Old token to prolongate</param>
/// <returns></returns>
public Task<WargamingProlongInfo> ProlongAuthToken(RealmType realm, string oldToken)
{
return _wargamingAuthenticationClient.ProlongAuthToken(realm, oldToken);
}
}
}
+1
View File
@@ -94,6 +94,7 @@ namespace WotBlitzStatisticsPro.GraphQl
.AddMutationType(d => d.Name("Mutation")) .AddMutationType(d => d.Name("Mutation"))
.AddType<WotBlitzStatisticsMutation>() .AddType<WotBlitzStatisticsMutation>()
.AddType<AuthenticationMutation>() .AddType<AuthenticationMutation>()
.AddType<WargamingAuthMutation>()
.AddType<AccountsSearchItemObjectType>() .AddType<AccountsSearchItemObjectType>()
.AddEnumType<MarkOfMastery>() .AddEnumType<MarkOfMastery>()
.AddEnumType<RealmType>() .AddEnumType<RealmType>()
@@ -6,5 +6,7 @@ namespace WotBlitzStatisticsPro.WgApiClient
public interface IWargamingAuthenticationClient public interface IWargamingAuthenticationClient
{ {
string LoginUrl(RealmType realm); string LoginUrl(RealmType realm);
Task<WargamingProlongInfo> ProlongAuthToken(RealmType realm, string oldToken);
} }
} }
@@ -0,0 +1,16 @@
using Newtonsoft.Json;
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public class WotAuthProlongateResponse
{
[JsonProperty("access_token")]
public string AccessToken { get; set; } = string.Empty;
[JsonProperty("account_id")]
public long AccountId { get; set; }
[JsonProperty("expires_at")]
public int ExpirationTimeStamp { get; set; }
}
}
@@ -41,7 +41,7 @@ namespace WotBlitzStatisticsPro.WgApiClient
_wotApiUrls[RealmType.Asia] = "https://api.worldoftanks.asia/wot/"; _wotApiUrls[RealmType.Asia] = "https://api.worldoftanks.asia/wot/";
} }
protected async Task<T?> GetFromBlitzApi<T>( protected Task<T?> GetFromBlitzApi<T>(
RealmType realmType, RealmType realmType,
RequestLanguage language, RequestLanguage language,
string method, string method,
@@ -49,10 +49,27 @@ namespace WotBlitzStatisticsPro.WgApiClient
{ {
string uri = GetBlitzUri(realmType, language, method, queryParameters); string uri = GetBlitzUri(realmType, language, method, queryParameters);
return CallWgApi<T>(uri);
}
protected async Task<T?> CallWgApi<T>(string uri, bool postMethod = false) where T : class
{
_httpClient.DefaultRequestHeaders.Accept.Clear(); _httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await _httpClient.GetAsync(uri); HttpResponseMessage? response;
if(postMethod)
{
// Create httpContent
var requestBody = TransformUriToRequestBody(uri);
var httpContent = new StringContent(requestBody, Encoding.UTF8, "application/x-www-form-urlencoded");
response = await _httpClient.PostAsync(uri, httpContent);
}
else
{
response = await _httpClient.GetAsync(uri);
}
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync(); var responseString = await response.Content.ReadAsStringAsync();
@@ -61,19 +78,20 @@ namespace WotBlitzStatisticsPro.WgApiClient
_logger.LogInformation($"HTTP GET {uri} - {responseBody.Status}"); _logger.LogInformation($"HTTP GET {uri} - {responseBody.Status}");
switch (responseBody.Status) switch (responseBody.Status)
{ {
case "ok": case "ok":
return responseBody.Data; return responseBody.Data;
case "error": case "error":
{ {
var error = responseBody.Error; var error = responseBody.Error;
var message = $"Field:{(error?.Field ?? "undefined")} Message:{(error?.Message ?? "undefined")} Value:{(error?.Value ?? "undefined")} Code:{(error?.Code ?? "undefined")}"; var message = $"Field:{(error?.Field ?? "undefined")} Message:{(error?.Message ?? "undefined")} Value:{(error?.Value ?? "undefined")} Code:{(error?.Code ?? "undefined")}";
throw new ArgumentException(message); throw new ArgumentException(message);
} }
default: default:
throw new ArgumentException($"Unexpected response body status '{responseBody.Status}'"); throw new ArgumentException($"Unexpected response body status '{responseBody.Status}'");
} }
} }
protected string GetWotUri(RealmType realmType, string method, string[]? queryParameters) protected string GetWotUri(RealmType realmType, string method, string[]? queryParameters)
{ {
@@ -108,5 +126,10 @@ namespace WotBlitzStatisticsPro.WgApiClient
} }
return uri.ToString(); return uri.ToString();
} }
private string TransformUriToRequestBody(string request)
{
return request.Substring(request.IndexOf('?') + 1);
}
} }
} }
@@ -215,6 +215,23 @@ namespace WotBlitzStatisticsPro.WgApiClient
return GetWotUri(realm, "auth/login/", null); return GetWotUri(realm, "auth/login/", null);
} }
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
};
}
#endregion #endregion
} }
} }