Skip to content

Commit

Permalink
fedora proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcrane committed Nov 27, 2023
1 parent 3316134 commit 64aedaa
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
14 changes: 14 additions & 0 deletions LeedsExperiment/Fedora/IFedora.cs
Original file line number Diff line number Diff line change
@@ -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<string> Proxy(string contentType, string path);

}
}
28 changes: 28 additions & 0 deletions LeedsExperiment/Preservation.API/Controllers/FedoraController.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> 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);
}
}
}
12 changes: 12 additions & 0 deletions LeedsExperiment/Preservation.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Fedora;
using System.Net.Http.Headers;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -7,6 +10,15 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddHttpClient<IFedora, Preservation.Fedora>(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.
Expand Down
32 changes: 32 additions & 0 deletions LeedsExperiment/Preservation/Fedora.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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;
}
}
}
4 changes: 4 additions & 0 deletions LeedsExperiment/Preservation/Preservation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Fedora\Fedora.csproj" />
</ItemGroup>

</Project>

0 comments on commit 64aedaa

Please sign in to comment.