From 64aedaa9e9c909587f925c8d07c9292dd82a8285 Mon Sep 17 00:00:00 2001 From: tomcrane Date: Mon, 27 Nov 2023 19:06:24 +0000 Subject: [PATCH] fedora proxy --- LeedsExperiment/Fedora/IFedora.cs | 14 ++++++++ .../Controllers/FedoraController.cs | 28 ++++++++++++++++ LeedsExperiment/Preservation.API/Program.cs | 12 +++++++ LeedsExperiment/Preservation/Fedora.cs | 32 +++++++++++++++++++ .../Preservation/Preservation.csproj | 4 +++ 5 files changed, 90 insertions(+) create mode 100644 LeedsExperiment/Fedora/IFedora.cs create mode 100644 LeedsExperiment/Preservation.API/Controllers/FedoraController.cs create mode 100644 LeedsExperiment/Preservation/Fedora.cs diff --git a/LeedsExperiment/Fedora/IFedora.cs b/LeedsExperiment/Fedora/IFedora.cs new file mode 100644 index 0000000..5b556c1 --- /dev/null +++ b/LeedsExperiment/Fedora/IFedora.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Fedora +{ + public interface IFedora + { + public Task Proxy(string contentType, string path); + + } +} diff --git a/LeedsExperiment/Preservation.API/Controllers/FedoraController.cs b/LeedsExperiment/Preservation.API/Controllers/FedoraController.cs new file mode 100644 index 0000000..e9e347c --- /dev/null +++ b/LeedsExperiment/Preservation.API/Controllers/FedoraController.cs @@ -0,0 +1,28 @@ +using Fedora; +using Microsoft.AspNetCore.Mvc; + +namespace Preservation.API.Controllers +{ + [ApiController] + [Route("api/fedora")] + public class FedoraController : Controller + { + private IFedora fedora; + + public FedoraController(IFedora fedora) + { + this.fedora = fedora; + } + + [HttpGet(Name = "FedoraProxy")] + [Route("{contentTypeMajor}/{contentTypeMinor}/{*path}")] + public async Task Index(string contentTypeMajor, string contentTypeMinor, string? path) + { + // in WebAPI, path is not giving us the full path + var fullPath = string.Join("/", Request.Path.ToString().Split('/')[5..]); + var contentType = $"{contentTypeMajor}/{contentTypeMinor}"; + var result = await fedora.Proxy(contentType, fullPath); + return Content(result, contentType); + } + } +} diff --git a/LeedsExperiment/Preservation.API/Program.cs b/LeedsExperiment/Preservation.API/Program.cs index 632a615..8a2540a 100644 --- a/LeedsExperiment/Preservation.API/Program.cs +++ b/LeedsExperiment/Preservation.API/Program.cs @@ -1,3 +1,6 @@ +using Fedora; +using System.Net.Http.Headers; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -7,6 +10,15 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddHttpClient(client => +{ + client.BaseAddress = new Uri(builder.Configuration["FedoraApiRoot"]!); + var authHeader = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes( + $"{builder.Configuration["FedoraAdminUser"]}:{builder.Configuration["FedoraAdminPassword"]}")); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader); + client.DefaultRequestHeaders.Add("Accept", "application/ld+json"); +}); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/LeedsExperiment/Preservation/Fedora.cs b/LeedsExperiment/Preservation/Fedora.cs new file mode 100644 index 0000000..cf8c289 --- /dev/null +++ b/LeedsExperiment/Preservation/Fedora.cs @@ -0,0 +1,32 @@ +using Fedora; +using Preservation.API; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Json; +using System.Text; +using System.Threading.Tasks; + +namespace Preservation +{ + public class Fedora : IFedora + { + private readonly HttpClient _httpClient; + + public Fedora(HttpClient httpClient) + { + _httpClient = httpClient; + } + + public async Task Proxy(string contentType, string path) + { + var req = new HttpRequestMessage(); + req.Headers.Accept.Clear(); + req.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType)); + req.RequestUri = new Uri(path, UriKind.Relative); + var response = await _httpClient.SendAsync(req); + var raw = await response.Content.ReadAsStringAsync(); + return raw; + } + } +} diff --git a/LeedsExperiment/Preservation/Preservation.csproj b/LeedsExperiment/Preservation/Preservation.csproj index fa71b7a..2bd0513 100644 --- a/LeedsExperiment/Preservation/Preservation.csproj +++ b/LeedsExperiment/Preservation/Preservation.csproj @@ -6,4 +6,8 @@ enable + + + +