wabbajack/Wabbajack.Server/Controllers/NexusCache.cs

71 lines
2.4 KiB
C#
Raw Normal View History

using System;
2021-11-27 19:55:59 +00:00
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
2021-11-27 19:55:59 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
2021-09-27 12:42:46 +00:00
using Wabbajack.Networking.NexusApi;
using Wabbajack.Networking.NexusApi.DTOs;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.BuildServer.Controllers;
//[Authorize]
[ApiController]
[Authorize(Roles = "User")]
[Route("/v1/games/")]
public class NexusCache : ControllerBase
{
2021-10-23 16:51:17 +00:00
private readonly NexusApi _api;
private readonly ILogger<NexusCache> _logger;
private AppSettings _settings;
2021-11-27 19:55:59 +00:00
private readonly HttpClient _client;
2021-10-23 16:51:17 +00:00
2021-11-27 19:55:59 +00:00
public NexusCache(ILogger<NexusCache> logger,AppSettings settings, HttpClient client)
2021-10-23 16:51:17 +00:00
{
_settings = settings;
_logger = logger;
2021-11-27 19:55:59 +00:00
_client = client;
}
private async Task ForwardToNexus(HttpRequest src)
{
_logger.LogInformation("Nexus Cache Forwarding: {path}", src.Path);
var request = new HttpRequestMessage(HttpMethod.Get, (Uri?)new Uri("https://api.nexusmods.com/" + src.Path));
request.Headers.Add("apikey", (string?)src.Headers["apikey"]);
request.Headers.Add("User-Agent", (string?)src.Headers.UserAgent);
using var response = await _client.SendAsync(request);
Response.Headers.ContentType = "application/json";
Response.StatusCode = (int)response.StatusCode;
await response.Content.CopyToAsync(Response.Body);
2021-10-23 16:51:17 +00:00
}
/// <summary>
/// Looks up the mod details for a given Gamename/ModId pair. If the entry is not found in the cache it will
/// be requested from the server (using the caller's Nexus API key if provided).
/// </summary>
/// <param name="db"></param>
/// <param name="GameName">The Nexus game name</param>
/// <param name="ModId">The Nexus mod id</param>
/// <returns>A Mod Info result</returns>
[HttpGet]
[Route("{GameName}/mods/{ModId}.json")]
2021-11-27 19:55:59 +00:00
public async Task GetModInfo(string GameName, long ModId)
{
2021-11-27 19:55:59 +00:00
await ForwardToNexus(Request);
2021-10-23 16:51:17 +00:00
}
[HttpGet]
[Route("{GameName}/mods/{ModId}/files.json")]
2021-11-27 19:55:59 +00:00
public async Task GetModFiles(string GameName, long ModId)
2021-10-23 16:51:17 +00:00
{
2021-11-27 19:55:59 +00:00
await ForwardToNexus(Request);
2021-10-23 16:51:17 +00:00
}
[HttpGet]
[Route("{GameName}/mods/{ModId}/files/{FileId}.json")]
2021-11-27 19:55:59 +00:00
public async Task GetModFile(string GameName, long ModId, long FileId)
2021-10-23 16:51:17 +00:00
{
2021-11-27 19:55:59 +00:00
await ForwardToNexus(Request);
}
2021-10-23 16:51:17 +00:00
}