feat: Passing Wargaming Token for Gather application mutation

This commit is contained in:
Mikhail Shabanov committed 2021-04-02 17:27:11 +02:00
1 parent 0b5da2f7c6
commit c9f933235c
8 files changed
+50 -17

No files matched your search

@@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using HotChocolate;
using HotChocolate.AspNetCore.Authorization; using HotChocolate.AspNetCore.Authorization;
using HotChocolate.Types; using HotChocolate.Types;
using WotBlitzStatisticsPro.Common.Model; using WotBlitzStatisticsPro.Common.Model;
@@ -40,10 +41,15 @@ namespace WotBlitzStatisticsPro.GraphQl.Mutation
/// <param name="realmType">Account region</param> /// <param name="realmType">Account region</param>
/// <param name="accountId">AccountId</param> /// <param name="accountId">AccountId</param>
/// <param name="requestLanguage">Request language</param> /// <param name="requestLanguage">Request language</param>
/// <param name="wgToken">Wargaming authentication token</param>
/// <returns></returns> /// <returns></returns>
public Task<AccountInfoResponse> GatherAccountInformation(RealmType realmType, long accountId, RequestLanguage requestLanguage) public Task<AccountInfoResponse> GatherAccountInformation(
RealmType realmType,
long accountId,
RequestLanguage requestLanguage,
[GlobalState("WgToken")] string wgToken)
{ {
return _wargamingAccounts.GatherAndSaveAccountInformation(realmType, accountId, requestLanguage); return _wargamingAccounts.GatherAndSaveAccountInformation(realmType, accountId, requestLanguage, wgToken);
} }
} }
} }
+10 -1
View File
@@ -94,7 +94,16 @@ namespace WotBlitzStatisticsPro.GraphQl
.AddHttpRequestInterceptor( .AddHttpRequestInterceptor(
(context, executor, builder, ct) => (context, executor, builder, ct) =>
{ {
// your code // Get WgAccessToken from header and push it into context
if (context.Request.Headers.ContainsKey("WargamingToken"))
{
var token = context.Request.Headers["WargamingToken"];
if (token.Count == 1)
{
builder.SetProperty("WgToken", token[0]);
}
}
return ValueTask.CompletedTask; return ValueTask.CompletedTask;
}) })
; ;
@@ -4,15 +4,17 @@ namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContex
{ {
public class AccountRequest public class AccountRequest
{ {
public AccountRequest(long accountId, RealmType realmType, RequestLanguage requestLanguage) public AccountRequest(long accountId, RealmType realmType, RequestLanguage requestLanguage, string? authenticationToken = null)
{ {
AccountId = accountId; AccountId = accountId;
RealmType = realmType; RealmType = realmType;
RequestLanguage = requestLanguage; RequestLanguage = requestLanguage;
AuthenticationToken = authenticationToken;
} }
public long AccountId { get; } public long AccountId { get; }
public RealmType RealmType { get; } public RealmType RealmType { get; }
public RequestLanguage RequestLanguage { get; } public RequestLanguage RequestLanguage { get; }
public string? AuthenticationToken { get; }
} }
} }
@@ -29,7 +29,7 @@ namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
public async Task Invoke(IOperationContext context, Func<IOperationContext, Task>? next) public async Task Invoke(IOperationContext context, Func<IOperationContext, Task>? next)
{ {
var accountInfo = await var accountInfo = await
_wargamingApi.GetPlayerAccountInfo(context.Request.AccountId, context.Request.RealmType, context.Request.RequestLanguage); _wargamingApi.GetPlayerAccountInfo(context.Request.AccountId, context.Request.RealmType, context.Request.RequestLanguage, context.Request.AuthenticationToken);
if (accountInfo == null) if (accountInfo == null)
{ {
@@ -8,7 +8,7 @@ namespace WotBlitzStatisticsPro.Logic
{ {
Task<AccountInfoResponse> GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage); Task<AccountInfoResponse> GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage);
Task<AccountInfoResponse> GatherAndSaveAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage); Task<AccountInfoResponse> GatherAndSaveAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage, string wargamingToken);
Task<AccountInfoHistoryResponse> GetAccountInfoHistory(RealmType realm, long accountId, DateTime startDate, Task<AccountInfoHistoryResponse> GetAccountInfoHistory(RealmType realm, long accountId, DateTime startDate,
RequestLanguage requestLanguage); RequestLanguage requestLanguage);
@@ -20,7 +20,9 @@ namespace WotBlitzStatisticsPro.Logic
_logger = logger; _logger = logger;
} }
public async Task<AccountInfoResponse> GatherAccountInformation(RealmType realm, long accountId, public async Task<AccountInfoResponse> GatherAccountInformation(
RealmType realm,
long accountId,
RequestLanguage requestLanguage) RequestLanguage requestLanguage)
{ {
var contextData = new AccountInformationPipelineContextData(); var contextData = new AccountInformationPipelineContextData();
@@ -56,13 +58,17 @@ namespace WotBlitzStatisticsPro.Logic
return contextData?.Response ?? new AccountInfoResponse(); return contextData?.Response ?? new AccountInfoResponse();
} }
public async Task<AccountInfoResponse> GatherAndSaveAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage) public async Task<AccountInfoResponse> GatherAndSaveAccountInformation(
RealmType realm,
long accountId,
RequestLanguage requestLanguage,
string wargamingToken)
{ {
var contextData = new AccountInformationPipelineContextData(); var contextData = new AccountInformationPipelineContextData();
try try
{ {
var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage)); var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage, wargamingToken));
context.AddOrReplace(contextData); context.AddOrReplace(contextData);
var pipeline = new Pipeline<IOperationContext>(_operationFactory); var pipeline = new Pipeline<IOperationContext>(_operationFactory);
@@ -17,7 +17,8 @@ namespace WotBlitzStatisticsPro.WgApiClient
Task<WotAccountInfo?> GetPlayerAccountInfo(long accountId, Task<WotAccountInfo?> GetPlayerAccountInfo(long accountId,
RealmType realmType = RealmType.Ru, RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En); RequestLanguage language = RequestLanguage.En,
string? authenticationToken = null);
Task<ClanAccountInfo?> GetPlayerClanInfo(long accountId, Task<ClanAccountInfo?> GetPlayerClanInfo(long accountId,
RealmType realmType = RealmType.Ru, RealmType realmType = RealmType.Ru,
@@ -94,15 +94,24 @@ namespace WotBlitzStatisticsPro.WgApiClient
public async Task<WotAccountInfo?> GetPlayerAccountInfo(long accountId, public async Task<WotAccountInfo?> GetPlayerAccountInfo(
long accountId,
RealmType realmType = RealmType.Ru, RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En) RequestLanguage language = RequestLanguage.En,
string? authenticationToken = null)
{ {
var account = await GetFromBlitzApi<Dictionary<string, WotAccountInfo>>( var account = authenticationToken == null
realmType, ? await GetFromBlitzApi<Dictionary<string, WotAccountInfo>>(
language, realmType,
"account/info/", language,
$"account_id={accountId}").ConfigureAwait(false); "account/info/",
$"account_id={accountId}").ConfigureAwait(false)
: await GetFromBlitzApi<Dictionary<string, WotAccountInfo>>(
realmType,
language,
"account/info/",
$"account_id={accountId}",
$"access_token={authenticationToken}").ConfigureAwait(false);
return account?[accountId.ToString()]; return account?[accountId.ToString()];
} }