mirror of
https://github.com/DrMboga/WotBlitzStatisticsBlazor.git
synced 2026-10-21 10:27:18 +02:00
feat: prolong token mutation added
This commit is contained in:
1 parent
11486b7303
commit
c94a1033c7
7 files changed
+132
-15
No files matched your search
@@ -6,5 +6,7 @@ namespace WotBlitzStatisticsPro.WgApiClient
|
||||
public interface IWargamingAuthenticationClient
|
||||
{
|
||||
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/";
|
||||
}
|
||||
|
||||
protected async Task<T?> GetFromBlitzApi<T>(
|
||||
protected Task<T?> GetFromBlitzApi<T>(
|
||||
RealmType realmType,
|
||||
RequestLanguage language,
|
||||
string method,
|
||||
@@ -49,10 +49,27 @@ namespace WotBlitzStatisticsPro.WgApiClient
|
||||
{
|
||||
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.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();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
|
||||
@@ -61,19 +78,20 @@ namespace WotBlitzStatisticsPro.WgApiClient
|
||||
_logger.LogInformation($"HTTP GET {uri} - {responseBody.Status}");
|
||||
|
||||
switch (responseBody.Status)
|
||||
{
|
||||
case "ok":
|
||||
return responseBody.Data;
|
||||
case "error":
|
||||
{
|
||||
var error = responseBody.Error;
|
||||
var message = $"Field:{(error?.Field ?? "undefined")} Message:{(error?.Message ?? "undefined")} Value:{(error?.Value ?? "undefined")} Code:{(error?.Code ?? "undefined")}";
|
||||
throw new ArgumentException(message);
|
||||
}
|
||||
default:
|
||||
throw new ArgumentException($"Unexpected response body status '{responseBody.Status}'");
|
||||
}
|
||||
}
|
||||
{
|
||||
case "ok":
|
||||
return responseBody.Data;
|
||||
case "error":
|
||||
{
|
||||
var error = responseBody.Error;
|
||||
var message = $"Field:{(error?.Field ?? "undefined")} Message:{(error?.Message ?? "undefined")} Value:{(error?.Value ?? "undefined")} Code:{(error?.Code ?? "undefined")}";
|
||||
throw new ArgumentException(message);
|
||||
}
|
||||
default:
|
||||
throw new ArgumentException($"Unexpected response body status '{responseBody.Status}'");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected string GetWotUri(RealmType realmType, string method, string[]? queryParameters)
|
||||
{
|
||||
@@ -108,5 +126,10 @@ namespace WotBlitzStatisticsPro.WgApiClient
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user