feat: http header passing mechanism

This commit is contained in:
Mikhail Shabanov committed 2021-09-02 18:43:00 +02:00
1 parent 64ad05dc92
commit 05059b0eda
6 files changed
+71 -13

No files matched your search

@@ -19,6 +19,9 @@ namespace WotBlitzStatisticsPro.Blazor.Pages
[Inject]
public IStringLocalizer<App> Localize { get; set; }
[Inject]
public INotificationsService Notifications { get; set; }
[Parameter]
public long AccountId { get; set; }
@@ -35,13 +38,20 @@ namespace WotBlitzStatisticsPro.Blazor.Pages
var settings = await LocalStorageService.ReadSettings();
CurrentRealmType = settings.RealmType;
// Maybe add refresh button instead of calling mutation each time
if(loggedInInfo != null && loggedInInfo.AccountId == AccountId)
try
{
await GraphQlBackendService.CollectPlayerInfo(AccountId, CurrentRealmType, loggedInInfo.AccessToken);
}
// Maybe add refresh button instead of calling mutation each time
if(loggedInInfo != null && loggedInInfo.AccountId == AccountId)
{
await GraphQlBackendService.CollectPlayerInfo(AccountId, CurrentRealmType, loggedInInfo.AccessToken);
}
(AccountInfo, AchievementsBySection) = await GraphQlBackendService.GetPlayerInfo(AccountId, CurrentRealmType);
(AccountInfo, AchievementsBySection) = await GraphQlBackendService.GetPlayerInfo(AccountId, CurrentRealmType);
}
catch (System.Exception e)
{
Notifications.ReportError("Can not get data from backend", e.Message);
}
}
}
}
+9 -3
View File
@@ -11,6 +11,7 @@ using Radzen;
using WotBlitzStatisticsPro.Blazor.Model;
using WotBlitzStatisticsPro.Blazor.Services;
using WotBlitzStatisticsPro.Blazor.Services.Mock;
using WotBlitzStatisticsPro.Blazor.GraphQl;
namespace WotBlitzStatisticsPro.Blazor
{
@@ -36,10 +37,15 @@ namespace WotBlitzStatisticsPro.Blazor
builder.Services.AddScoped<ILocalStorageService, LocalStorageService>();
builder.Services.AddSingleton<INotificationsService, NotificationsService>();
builder.Services.AddSingleton<IWargamingAuthTokenHeaderHelper, WargamingAuthTokenHeaderHelper>();
builder.Services.AddTransient<WargamingAuthTokenHeaderHandler>();
// StrawberryShake GraphQL Client
builder.Services
.AddWotBlitzStatisticsProClient()
.ConfigureHttpClient(client => client.BaseAddress = new Uri($"{builder.HostEnvironment.BaseAddress}graphql/"));
builder.Services.AddHttpClient(
WotBlitzStatisticsProClient.ClientName,
client => client.BaseAddress = new Uri($"{builder.HostEnvironment.BaseAddress}graphql/"))
.AddHttpMessageHandler<WargamingAuthTokenHeaderHandler>();
builder.Services.AddWotBlitzStatisticsProClient();
// ToDo: Asp Net hosting environment variables don't work here. I don't know why
//var useMock = Environment.GetEnvironmentVariable("USE_GRAPH_QL_MOCK");
@@ -12,13 +12,16 @@ namespace WotBlitzStatisticsPro.Blazor.Services
{
private readonly WotBlitzStatisticsProClient _client;
private readonly INotificationsService _notificationsService;
private readonly IWargamingAuthTokenHeaderHelper _wargamingAuthTokenHeaderHelper;
public GraphQlBackendService(
WotBlitzStatisticsProClient client,
INotificationsService notificationsService)
INotificationsService notificationsService,
IWargamingAuthTokenHeaderHelper wargamingAuthTokenHeaderHelper)
{
_client = client;
_notificationsService = notificationsService;
_wargamingAuthTokenHeaderHelper = wargamingAuthTokenHeaderHelper;
}
public async Task<IReadOnlyList<IPlayerShortInfo>?> FindPlayers(string accountNick, RealmType realmType)
@@ -57,11 +60,11 @@ namespace WotBlitzStatisticsPro.Blazor.Services
throw new System.NotImplementedException();
}
public Task CollectPlayerInfo(long accountId, RealmType realmType, string accessToken)
public async Task CollectPlayerInfo(long accountId, RealmType realmType, string accessToken)
{
// ToDo: Call mutation with accessToken in the context
return _client.UpdatePlayer.ExecuteAsync(accountId, realmType, RequestLanguage.En);
_wargamingAuthTokenHeaderHelper.WargamingToken = accessToken;
var result = await _client.UpdatePlayer.ExecuteAsync(accountId, realmType, RequestLanguage.En);
CheckErrors(result.Errors);
}
private RequestLanguage GetLanguage()
@@ -0,0 +1,7 @@
namespace WotBlitzStatisticsPro.Blazor.Services
{
public interface IWargamingAuthTokenHeaderHelper
{
string? WargamingToken { get; set; }
}
}
@@ -0,0 +1,25 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace WotBlitzStatisticsPro.Blazor.Services
{
public class WargamingAuthTokenHeaderHandler: DelegatingHandler
{
private readonly IWargamingAuthTokenHeaderHelper _wargamingAuthTokenHeaderHelper;
public WargamingAuthTokenHeaderHandler(IWargamingAuthTokenHeaderHelper wargamingAuthTokenHeaderHelper)
{
_wargamingAuthTokenHeaderHelper = wargamingAuthTokenHeaderHelper;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (!string.IsNullOrEmpty(_wargamingAuthTokenHeaderHelper.WargamingToken))
{
request.Headers.Add("WargamingToken", _wargamingAuthTokenHeaderHelper.WargamingToken);
}
return base.SendAsync(request, cancellationToken);
}
}
}
@@ -0,0 +1,7 @@
namespace WotBlitzStatisticsPro.Blazor.Services
{
public class WargamingAuthTokenHeaderHelper : IWargamingAuthTokenHeaderHelper
{
public string? WargamingToken { get; set; } = null;
}
}