Skip to content

Commit

Permalink
Add api proxy options
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcrane committed Dec 4, 2023
1 parent 64aedaa commit 0ec1fd9
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
42 changes: 42 additions & 0 deletions LeedsExperiment/Fedora/ContentTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Fedora;

public static class ContentTypes
{
public const string JsonLd = "application/ld+json";
public const string NTriples = "application/n-triples";
public const string RdfXml = "application/rdf+xml";
public const string TextN3 = "text/n3";
public const string TextPlain = "text/plain";
public const string TextTurtle = "text/turtle";

public static string FormatAcceptsHeader(string contentType, string jsonLdMode = JsonLdModes.Expanded)
{
if (jsonLdMode == JsonLdModes.Expanded || contentType != JsonLd)
{
// expanded is the default
return contentType;
}

return $"{JsonLd}; profile=\"{jsonLdMode}\"";
}
}

public static class JsonLdModes
{
/// <summary>
/// The default Fedora JSON-LD representation
/// </summary>
public const string Expanded = "\"http://www.w3.org/ns/json-ld#expanded\"";


/// <summary>
/// Compacted JSON-LD (not the default)
/// </summary>
public const string Compacted = "\"http://www.w3.org/ns/json-ld#compacted\"";


/// <summary>
/// Flattened JSON-LD (not the default)
/// </summary>
public const string Flattened = "\"http://www.w3.org/ns/json-ld#flattened\"";
}
2 changes: 1 addition & 1 deletion LeedsExperiment/Fedora/IFedora.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Fedora
{
public interface IFedora
{
public Task<string> Proxy(string contentType, string path);
public Task<string> Proxy(string contentType, string path, string? jsonLdMode = null, bool preferContained = false);

}
}
34 changes: 34 additions & 0 deletions LeedsExperiment/Fedora/Prefer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Fedora;

public static class Prefer
{
/// <summary>
/// Include assertions from other Fedora resources to this node (excluded from representation by default)
/// </summary>
public const string PreferInboundReferences = "http://fedora.info/definitions/fcrepo#PreferInboundReferences";

/// <summary>
/// Embed server managed properties in the representation (enabled by default)
/// </summary>
public const string ServerManaged = "http://fedora.info/definitions/fcrepo#ServerManaged";

/// <summary>
/// Include/Exclude "ldp:contains" assertions to contained resources(enabled by default)
/// </summary>
public const string PreferContainment = "http://www.w3.org/ns/ldp#PreferContainment";

/// <summary>
/// Include/Exclude assertions to member resources established by the Direct and Indirect containers(enabled by default)
/// </summary>
public const string PreferMembership = "http://www.w3.org/ns/ldp#PreferMembership";

/// <summary>
/// Include/Exclude triples that would be present when the container is empty(enabled by default)
/// </summary>
public const string PreferMinimalContainer = "http://www.w3.org/ns/ldp#PreferMinimalContainer";

/// <summary>
/// Embed "child" resources in the returned representation
/// </summary>
public const string PreferContainedDescriptions = "http://www.w3.org/ns/oa#PreferContainedDescriptions";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ public FedoraController(IFedora fedora)
[Route("{contentTypeMajor}/{contentTypeMinor}/{*path}")]
public async Task<IActionResult> Index(string contentTypeMajor, string contentTypeMinor, string? path)
{
string? jsonld = Request.Query["jsonld"];
// Unlike Fedora, we will default to COMPACTED
string? jsonLdMode = JsonLdModes.Compacted;
if (jsonld == "expanded") { jsonLdMode = JsonLdModes.Expanded; }
if (jsonld == "flattened") { jsonLdMode = JsonLdModes.Flattened; }

bool contained = Convert.ToBoolean(Request.Query["contained"]);

// 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);
var result = await fedora.Proxy(contentType, fullPath, jsonLdMode, contained);
return Content(result, contentType);
}
}
Expand Down
22 changes: 20 additions & 2 deletions LeedsExperiment/Preservation/Fedora.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Preservation
{
Expand All @@ -18,11 +20,27 @@ public Fedora(HttpClient httpClient)
_httpClient = httpClient;
}

public async Task<string> Proxy(string contentType, string path)
public async Task<string> Proxy(string contentType, string path, string? jsonLdMode = null, bool preferContained = false)
{
var req = new HttpRequestMessage();
req.Headers.Accept.Clear();
req.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType));
var contentTypeHeader = new MediaTypeWithQualityHeaderValue(contentType);
if(contentType == ContentTypes.JsonLd)
{
if(jsonLdMode == JsonLdModes.Compacted)
{
contentTypeHeader.Parameters.Add(new NameValueHeaderValue("profile", JsonLdModes.Compacted));
}
if (jsonLdMode == JsonLdModes.Flattened)
{
contentTypeHeader.Parameters.Add(new NameValueHeaderValue("profile", JsonLdModes.Flattened));
}
}
req.Headers.Accept.Add(contentTypeHeader);
if (preferContained)
{
req.Headers.Add("Prefer", $"return=representation; include=\"{Prefer.PreferContainedDescriptions}\"");
}
req.RequestUri = new Uri(path, UriKind.Relative);
var response = await _httpClient.SendAsync(req);
var raw = await response.Content.ReadAsStringAsync();
Expand Down

0 comments on commit 0ec1fd9

Please sign in to comment.