wabbajack/Wabbajack.Networking.NexusApi/ProxiedNexusApi.cs

54 lines
2.0 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Wabbajack.DTOs;
using Wabbajack.DTOs.Logins;
using Wabbajack.Networking.Http.Interfaces;
using Wabbajack.Networking.NexusApi.DTOs;
using Wabbajack.Networking.WabbajackClientApi;
using Wabbajack.RateLimiter;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Networking.NexusApi;
public class ProxiedNexusApi : NexusApi
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private readonly ITokenProvider<WabbajackApiState> _apiState;
private readonly Configuration _wabbajackClientConfiguration;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public HashSet<string> ProxiedEndpoints = new()
{
Endpoints.ModInfo,
Endpoints.ModFiles,
Endpoints.ModFile
};
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public ProxiedNexusApi(ITokenProvider<NexusApiState> apiKey, ILogger<ProxiedNexusApi> logger, HttpClient client,
IResource<HttpClient> limiter,
ApplicationInfo appInfo, JsonSerializerOptions jsonOptions, ITokenProvider<WabbajackApiState> apiState,
Configuration wabbajackClientConfiguration)
: base(apiKey, logger, client, limiter, appInfo, jsonOptions)
{
_apiState = apiState;
_wabbajackClientConfiguration = wabbajackClientConfiguration;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
protected override async ValueTask<HttpRequestMessage> GenerateMessage(HttpMethod method, string uri,
params object[] parameters)
{
var msg = await base.GenerateMessage(method, uri, parameters);
if (ProxiedEndpoints.Contains(uri))
msg.RequestUri = new Uri($"https://build.wabbajack.org/{string.Format(uri, parameters)}");
msg.Headers.Add(_wabbajackClientConfiguration.MetricsKeyHeader, (await _apiState.Get())!.MetricsKey);
return msg;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
protected override ResponseMetadata ParseHeaders(HttpResponseMessage result)
{
if (result.RequestMessage!.RequestUri!.Host == "build.wabbajack.org")
return new ResponseMetadata {IsReal = false};
return base.ParseHeaders(result);
2021-09-27 12:42:46 +00:00
}
}