wabbajack/Wabbajack.Networking.NexusApi/ProxiedNexusApi.cs
2021-10-23 10:51:17 -06:00

54 lines
2.0 KiB
C#

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;
namespace Wabbajack.Networking.NexusApi;
public class ProxiedNexusApi : NexusApi
{
private readonly ITokenProvider<WabbajackApiState> _apiState;
private readonly Configuration _wabbajackClientConfiguration;
public HashSet<string> ProxiedEndpoints = new()
{
Endpoints.ModInfo,
Endpoints.ModFiles,
Endpoints.ModFile
};
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;
}
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;
}
protected override ResponseMetadata ParseHeaders(HttpResponseMessage result)
{
if (result.RequestMessage!.RequestUri!.Host == "build.wabbajack.org")
return new ResponseMetadata {IsReal = false};
return base.ParseHeaders(result);
}
}