From c9f933235c347edb1b4a2acaed3a588d8e681575 Mon Sep 17 00:00:00 2001 From: Mikhail Shabanov Date: Fri, 2 Apr 2021 17:27:11 +0200 Subject: [PATCH] feat: Passing Wargaming Token for Gather application mutation --- .../Mutation/WotBlitzStatisticsMutation.cs | 10 ++++++-- WotBlitzStatisticsPro.GraphQl/Startup.cs | 11 ++++++++- .../OperationContext/AccountRequest.cs | 4 +++- .../Operations/GetAccountInfoOperation.cs | 2 +- .../IWargamingAccounts.cs | 2 +- .../WargamingAccounts.cs | 12 +++++++--- .../IWargamingApiClient.cs | 3 ++- .../WargamingApiClient.cs | 23 +++++++++++++------ 8 files changed, 50 insertions(+), 17 deletions(-) diff --git a/WotBlitzStatisticsPro.GraphQl/Mutation/WotBlitzStatisticsMutation.cs b/WotBlitzStatisticsPro.GraphQl/Mutation/WotBlitzStatisticsMutation.cs index 8d0da36..645e8d7 100644 --- a/WotBlitzStatisticsPro.GraphQl/Mutation/WotBlitzStatisticsMutation.cs +++ b/WotBlitzStatisticsPro.GraphQl/Mutation/WotBlitzStatisticsMutation.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using HotChocolate; using HotChocolate.AspNetCore.Authorization; using HotChocolate.Types; using WotBlitzStatisticsPro.Common.Model; @@ -40,10 +41,15 @@ namespace WotBlitzStatisticsPro.GraphQl.Mutation /// Account region /// AccountId /// Request language + /// Wargaming authentication token /// - public Task GatherAccountInformation(RealmType realmType, long accountId, RequestLanguage requestLanguage) + public Task GatherAccountInformation( + RealmType realmType, + long accountId, + RequestLanguage requestLanguage, + [GlobalState("WgToken")] string wgToken) { - return _wargamingAccounts.GatherAndSaveAccountInformation(realmType, accountId, requestLanguage); + return _wargamingAccounts.GatherAndSaveAccountInformation(realmType, accountId, requestLanguage, wgToken); } } } \ No newline at end of file diff --git a/WotBlitzStatisticsPro.GraphQl/Startup.cs b/WotBlitzStatisticsPro.GraphQl/Startup.cs index 395e8b7..858568e 100644 --- a/WotBlitzStatisticsPro.GraphQl/Startup.cs +++ b/WotBlitzStatisticsPro.GraphQl/Startup.cs @@ -94,7 +94,16 @@ namespace WotBlitzStatisticsPro.GraphQl .AddHttpRequestInterceptor( (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; }) ; diff --git a/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/OperationContext/AccountRequest.cs b/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/OperationContext/AccountRequest.cs index 9eaa134..372ab27 100644 --- a/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/OperationContext/AccountRequest.cs +++ b/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/OperationContext/AccountRequest.cs @@ -4,15 +4,17 @@ namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContex { public class AccountRequest { - public AccountRequest(long accountId, RealmType realmType, RequestLanguage requestLanguage) + public AccountRequest(long accountId, RealmType realmType, RequestLanguage requestLanguage, string? authenticationToken = null) { AccountId = accountId; RealmType = realmType; RequestLanguage = requestLanguage; + AuthenticationToken = authenticationToken; } public long AccountId { get; } public RealmType RealmType { get; } public RequestLanguage RequestLanguage { get; } + public string? AuthenticationToken { get; } } } \ No newline at end of file diff --git a/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/Operations/GetAccountInfoOperation.cs b/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/Operations/GetAccountInfoOperation.cs index 85663d9..65601ff 100644 --- a/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/Operations/GetAccountInfoOperation.cs +++ b/WotBlitzStatisticsPro.Logic/AccountInformationPipeline/Operations/GetAccountInfoOperation.cs @@ -29,7 +29,7 @@ namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations public async Task Invoke(IOperationContext context, Func? next) { 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) { diff --git a/WotBlitzStatisticsPro.Logic/IWargamingAccounts.cs b/WotBlitzStatisticsPro.Logic/IWargamingAccounts.cs index cbda944..8c57f63 100644 --- a/WotBlitzStatisticsPro.Logic/IWargamingAccounts.cs +++ b/WotBlitzStatisticsPro.Logic/IWargamingAccounts.cs @@ -8,7 +8,7 @@ namespace WotBlitzStatisticsPro.Logic { Task GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage); - Task GatherAndSaveAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage); + Task GatherAndSaveAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage, string wargamingToken); Task GetAccountInfoHistory(RealmType realm, long accountId, DateTime startDate, RequestLanguage requestLanguage); diff --git a/WotBlitzStatisticsPro.Logic/WargamingAccounts.cs b/WotBlitzStatisticsPro.Logic/WargamingAccounts.cs index 20eca77..0421b1e 100644 --- a/WotBlitzStatisticsPro.Logic/WargamingAccounts.cs +++ b/WotBlitzStatisticsPro.Logic/WargamingAccounts.cs @@ -20,7 +20,9 @@ namespace WotBlitzStatisticsPro.Logic _logger = logger; } - public async Task GatherAccountInformation(RealmType realm, long accountId, + public async Task GatherAccountInformation( + RealmType realm, + long accountId, RequestLanguage requestLanguage) { var contextData = new AccountInformationPipelineContextData(); @@ -56,13 +58,17 @@ namespace WotBlitzStatisticsPro.Logic return contextData?.Response ?? new AccountInfoResponse(); } - public async Task GatherAndSaveAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage) + public async Task GatherAndSaveAccountInformation( + RealmType realm, + long accountId, + RequestLanguage requestLanguage, + string wargamingToken) { var contextData = new AccountInformationPipelineContextData(); try { - var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage)); + var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage, wargamingToken)); context.AddOrReplace(contextData); var pipeline = new Pipeline(_operationFactory); diff --git a/WotBlitzStatisticsPro.WgApiClient/IWargamingApiClient.cs b/WotBlitzStatisticsPro.WgApiClient/IWargamingApiClient.cs index 744ed58..969489c 100644 --- a/WotBlitzStatisticsPro.WgApiClient/IWargamingApiClient.cs +++ b/WotBlitzStatisticsPro.WgApiClient/IWargamingApiClient.cs @@ -17,7 +17,8 @@ namespace WotBlitzStatisticsPro.WgApiClient Task GetPlayerAccountInfo(long accountId, RealmType realmType = RealmType.Ru, - RequestLanguage language = RequestLanguage.En); + RequestLanguage language = RequestLanguage.En, + string? authenticationToken = null); Task GetPlayerClanInfo(long accountId, RealmType realmType = RealmType.Ru, diff --git a/WotBlitzStatisticsPro.WgApiClient/WargamingApiClient.cs b/WotBlitzStatisticsPro.WgApiClient/WargamingApiClient.cs index 28f2d3a..a51b1a2 100644 --- a/WotBlitzStatisticsPro.WgApiClient/WargamingApiClient.cs +++ b/WotBlitzStatisticsPro.WgApiClient/WargamingApiClient.cs @@ -94,15 +94,24 @@ namespace WotBlitzStatisticsPro.WgApiClient - public async Task GetPlayerAccountInfo(long accountId, + public async Task GetPlayerAccountInfo( + long accountId, RealmType realmType = RealmType.Ru, - RequestLanguage language = RequestLanguage.En) + RequestLanguage language = RequestLanguage.En, + string? authenticationToken = null) { - var account = await GetFromBlitzApi>( - realmType, - language, - "account/info/", - $"account_id={accountId}").ConfigureAwait(false); + var account = authenticationToken == null + ? await GetFromBlitzApi>( + realmType, + language, + "account/info/", + $"account_id={accountId}").ConfigureAwait(false) + : await GetFromBlitzApi>( + realmType, + language, + "account/info/", + $"account_id={accountId}", + $"access_token={authenticationToken}").ConfigureAwait(false); return account?[accountId.ToString()]; }