Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump iiif-net and use exposed options with iiif-net derrived collections #44

Merged
merged 7 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/IIIFPresentation/API.Tests/Integration/GetCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public async Task Get_RootFlat_ReturnsEntryPointFlat_WhenAuthAndHeader()
collection!.Id.Should().Be($"http://localhost/1/collections/{RootCollection.Id}");
collection.PublicId.Should().Be("http://localhost/1");
collection.Items!.Count.Should().Be(2);
collection.Items[0].Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
collection.Items.OfType<Collection>().First().Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
collection.TotalItems.Should().Be(2);
collection.CreatedBy.Should().Be("admin");
collection.Behavior.Should().Contain("public-iiif");
Expand All @@ -167,7 +167,7 @@ public async Task Get_ChildFlat_ReturnsEntryPointFlat_WhenCalledByChildId()
collection!.Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
collection.PublicId.Should().Be("http://localhost/1/first-child");
collection.Items!.Count.Should().Be(1);
collection.Items[0].Id.Should().Be("http://localhost/1/collections/SecondChildCollection");
collection.Items.OfType<Collection>().First().Id.Should().Be("http://localhost/1/collections/SecondChildCollection");
collection.TotalItems.Should().Be(1);
collection.CreatedBy.Should().Be("admin");
collection.Behavior.Should().Contain("public-iiif");
Expand Down Expand Up @@ -236,7 +236,7 @@ public async Task Get_ChildFlat_ReturnsReducedItems_WhenCalledWithSmallPageSize(
collection.View.Page.Should().Be(1);
collection.View.TotalPages.Should().Be(2);
collection.Items!.Count.Should().Be(1);
collection.Items[0].Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
collection.Items.OfType<Collection>().First().Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
}

[Fact]
Expand All @@ -257,7 +257,7 @@ public async Task Get_RootFlat_ReturnsSecondPage_WhenCalledWithSmallPageSize()
collection.View.Page.Should().Be(2);
collection.View.TotalPages.Should().Be(2);
collection.Items!.Count.Should().Be(1);
collection.Items[0].Id.Should().Be("http://localhost/1/collections/NonPublic");
collection.Items.OfType<Collection>().First().Id.Should().Be("http://localhost/1/collections/NonPublic");
}

[Fact]
Expand Down Expand Up @@ -322,7 +322,7 @@ public async Task Get_ChildFlat_ReturnsCorrectItem_WhenCalledWithSmallPageSizeAn
collection.View.Page.Should().Be(1);
collection.View.TotalPages.Should().Be(2);
collection.Items!.Count.Should().Be(1);
collection.Items[0].Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
collection.Items.OfType<Collection>().First().Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
}

[Theory]
Expand All @@ -348,7 +348,7 @@ public async Task Get_RootFlat_ReturnsFirstPageWithSecondItem_WhenCalledWithSmal
collection.View.Page.Should().Be(1);
collection.View.TotalPages.Should().Be(2);
collection.Items!.Count.Should().Be(1);
collection.Items[0].Id.Should().Be("http://localhost/1/collections/NonPublic");
collection.Items.OfType<Collection>().First().Id.Should().Be("http://localhost/1/collections/NonPublic");
}

[Fact]
Expand All @@ -371,6 +371,6 @@ public async Task Get_ChildFlat_IgnoresOrderBy_WhenCalledWithInvalidOrderBy()
collection.View.Page.Should().Be(1);
collection.View.TotalPages.Should().Be(2);
collection.Items!.Count.Should().Be(1);
collection.Items[0].Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
collection.Items.OfType<Collection>().First().Id.Should().Be("http://localhost/1/collections/FirstChildCollection");
}
}
2 changes: 1 addition & 1 deletion src/IIIFPresentation/API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.9.2" />
<PackageReference Include="iiif-net" Version="0.2.8" />
<PackageReference Include="iiif-net" Version="0.2.9" />
<PackageReference Include="LazyCache.AspNetCore" Version="2.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
Expand Down
54 changes: 26 additions & 28 deletions src/IIIFPresentation/API/Converters/CollectionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@

public static class CollectionConverter
{
public static Collection ToHierarchicalCollection(this Models.Database.Collections.Collection dbAsset,
public static Collection ToHierarchicalCollection(this Models.Database.Collections.Collection dbAsset,
UrlRoots urlRoots, List<Models.Database.Collections.Collection>? items)
{
var collection = new Collection()
{
Id = dbAsset.GenerateHierarchicalCollectionId(urlRoots),
Label = dbAsset.Label,
Items = items != null ? items.Select(x => new Collection()
{
Id = x.GenerateHierarchicalCollectionId(urlRoots),
Label = x.Label
}).ToList<ICollectionItem>() : []
Items = items != null
? items.Select(x => new Collection()
{
Id = x.GenerateHierarchicalCollectionId(urlRoots),
Label = x.Label
}).ToList<ICollectionItem>()
: []
};

collection.EnsurePresentation3Context();

return collection;
Expand All @@ -32,11 +34,11 @@
UrlRoots urlRoots, int pageSize, int currentPage, int totalItems,
List<Models.Database.Collections.Collection>? items, string? orderQueryParam = null)
{
var totalPages = (int)Math.Ceiling(totalItems == 0 ? 1 : (double)totalItems / pageSize);
var totalPages = (int) Math.Ceiling(totalItems == 0 ? 1 : (double) totalItems / pageSize);

var orderQueryParamConverted = string.IsNullOrEmpty(orderQueryParam) ? string.Empty : $"&{orderQueryParam}";
return new FlatCollection()

return new()
{
Id = dbAsset.GenerateFlatCollectionId(urlRoots),
Context = new List<string>
Expand All @@ -49,32 +51,29 @@
Behavior = new List<string>()
.AppendIf(dbAsset.IsPublic, Behavior.IsPublic)
.AppendIf(dbAsset.IsStorageCollection, Behavior.IsStorageCollection),
Type = PresentationType.Collection,
Slug = dbAsset.Slug,
Parent = dbAsset.Parent != null
? dbAsset.GenerateFlatCollectionParent(urlRoots)
: null,

ItemsOrder = dbAsset.ItemsOrder,
Items = items != null
? items.Select(i => new Item
? items.Select(i => (ICollectionItem) new Collection()
{
Id = i.GenerateFlatCollectionId(urlRoots),
Label = i.Label,
Type = PresentationType.Collection
Label = i.Label
}).ToList()
: [],

PartOf = dbAsset.Parent != null
? new List<PartOf>
{
new()
?
[
new PartOf(nameof(PresentationType.Collection))
{
Id = $"{urlRoots.BaseUrl}/{dbAsset.CustomerId}/{dbAsset.Parent}",
Label = dbAsset.Label,
Type = PresentationType.Collection
Label = dbAsset.Label
}
}
]
: null,

TotalItems = totalItems,
Expand All @@ -83,20 +82,18 @@

SeeAlso =
[
new()
new(nameof(PresentationType.Collection))
{
Id = dbAsset.GenerateHierarchicalCollectionId(urlRoots),
Type = PresentationType.Collection,
Label = dbAsset.Label,
Profile = ["Public"]
Profile = "Public"
},

new()
new(nameof(PresentationType.Collection))
{
Id = $"{dbAsset.GenerateHierarchicalCollectionId(urlRoots)}/iiif",
Type = PresentationType.Collection,
Label = dbAsset.Label,
Profile = ["api-hierarchical"]
Profile = "api-hierarchical"
}
],

Expand All @@ -113,7 +110,7 @@
{
var view = new View()
{
Id = dbAsset.GenerateFlatCollectionViewId(urlRoots, currentPage, pageSize, orderQueryParam),

Check warning on line 113 in src/IIIFPresentation/API/Converters/CollectionConverter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Possible null reference argument for parameter 'orderQueryParam' in 'string CollectionHelperX.GenerateFlatCollectionViewId(Collection collection, UrlRoots urlRoots, int currentPage, int pageSize, string orderQueryParam)'.

Check warning on line 113 in src/IIIFPresentation/API/Converters/CollectionConverter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Possible null reference argument for parameter 'orderQueryParam' in 'string CollectionHelperX.GenerateFlatCollectionViewId(Collection collection, UrlRoots urlRoots, int currentPage, int pageSize, string orderQueryParam)'.
Type = PresentationType.PartialCollectionView,
Page = currentPage,
PageSize = pageSize,
Expand All @@ -123,15 +120,16 @@
if (currentPage > 1)
{
view.First = dbAsset.GenerateFlatCollectionViewFirst(urlRoots, pageSize, orderQueryParam);
view.Previous = dbAsset.GenerateFlatCollectionViewPrevious(urlRoots, currentPage, pageSize, orderQueryParam);
view.Previous =
dbAsset.GenerateFlatCollectionViewPrevious(urlRoots, currentPage, pageSize, orderQueryParam);
}

if (totalPages > currentPage)
{
view.Next = dbAsset.GenerateFlatCollectionViewNext(urlRoots, currentPage, pageSize, orderQueryParam);
view.Last = dbAsset.GenerateFlatCollectionViewLast(urlRoots, totalPages, pageSize, orderQueryParam);
}

return view;
}
}
7 changes: 6 additions & 1 deletion src/IIIFPresentation/API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using API.Features.Storage.Validators;
using API.Infrastructure;
using API.Settings;
using Core.Response;
using IIIF.Presentation.V3;
using Microsoft.AspNetCore.HttpOverrides;
using Models.API.Collection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Repository;
using Serilog;

Expand Down Expand Up @@ -38,7 +43,7 @@
opts.ForwardedHeaders = ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto;
});

builder.Services.ConfigureHttpJsonOptions( options =>
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.WriteIndented = true;
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
Expand Down
2 changes: 1 addition & 1 deletion src/IIIFPresentation/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="iiif-net" Version="0.2.8" />
<PackageReference Include="iiif-net" Version="0.2.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand Down
57 changes: 30 additions & 27 deletions src/IIIFPresentation/Core/Response/HttpResponseMessageX.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Core.Exceptions;
using System.Runtime.Serialization;
using Core.Exceptions;
using IIIF;
using IIIF.Serialisation;
using Newtonsoft.Json;
Expand All @@ -7,6 +8,8 @@ namespace Core.Response;

public static class HttpResponseMessageX
{
public static StreamingContext? SerializationContext { get; set; }

/// <summary>
/// Read HttpResponseMessage as JSON using Newtonsoft for conversion.
/// </summary>
Expand All @@ -23,10 +26,10 @@ public static class HttpResponseMessageX
if (!response.IsJsonResponse()) return default;

var contentStream = await response.Content.ReadAsStreamAsync();

return contentStream.FromJsonStream<T>();
}

/// <summary>
/// Read HttpResponseMessage as JSON using Newtonsoft for conversion.
/// </summary>
Expand All @@ -36,29 +39,28 @@ public static class HttpResponseMessageX
/// <typeparam name="T">Type to convert response to</typeparam>
/// <returns>Converted Http response.</returns>
public static async Task<T?> ReadAsPresentationJsonAsync<T>(this HttpResponseMessage response,
bool ensureSuccess = true, JsonSerializerSettings? settings = null)
bool ensureSuccess = true, JsonSerializerSettings? settings = null) where T : new()
{
if (ensureSuccess) response.EnsureSuccessStatusCode();

if (!response.IsJsonResponse()) return default;

var contentStream = await response.Content.ReadAsStreamAsync();

using var streamReader = new StreamReader(contentStream);
using var jsonReader = new JsonTextReader(streamReader);
await using var jsonReader = new JsonTextReader(streamReader);

JsonSerializer serializer = new();
if (settings == null) return serializer.Deserialize<T>(jsonReader);

if (settings.ContractResolver != null)
{
serializer.ContractResolver = settings.ContractResolver;
}
serializer.NullValueHandling = settings.NullValueHandling;

return serializer.Deserialize<T>(jsonReader);
settings ??= new(IIIFSerialiserX.DeserializerSettings);
if (SerializationContext.HasValue)
settings.Context = SerializationContext.Value;

var serializer = JsonSerializer.Create(settings);

var result = new T();
serializer.Populate(jsonReader, result);
return result;
}

/// <summary>
/// Check if the <see cref="HttpResponseMessage"/> object contains a JSON response
/// e.g. application/json, application/ld+json
Expand All @@ -70,15 +72,15 @@ public static bool IsJsonResponse(this HttpResponseMessage response)
var mediaType = response.Content.Headers.ContentType?.MediaType;
return mediaType != null && mediaType.Contains("json");
}

public static async Task<T?> ReadAsIIIFResponseAsync<T>(this HttpResponseMessage response,
JsonSerializerSettings? settings = null) where T : JsonLdBase
{
if ((int)response.StatusCode < 400)
if ((int) response.StatusCode < 400)
{
return await response.ReadWithIIIFContext<T>(true, settings);
}

try
{
return await response.ReadAsIIIFJsonAsync<T>(false, settings);
Expand All @@ -88,15 +90,15 @@ public static bool IsJsonResponse(this HttpResponseMessage response)
throw new PresentationException("Could not convert response JSON to error", ex);
}
}

public static async Task<T?> ReadAsPresentationResponseAsync<T>(this HttpResponseMessage response,
JsonSerializerSettings? settings = null)
JsonSerializerSettings? settings = null) where T : new()
{
if ((int)response.StatusCode < 400)
if ((int) response.StatusCode < 400)
{
return await response.ReadWithContext<T>(true, settings);
}

try
{
return await response.ReadAsPresentationJsonAsync<T>(false, settings);
Expand All @@ -115,13 +117,14 @@ public static bool IsJsonResponse(this HttpResponseMessage response)
var json = await response.ReadAsIIIFJsonAsync<T>(ensureSuccess, settings ?? new JsonSerializerSettings());
return json;
}

private static async Task<T?> ReadWithContext<T>(
this HttpResponseMessage response,
bool ensureSuccess,
JsonSerializerSettings? settings)
JsonSerializerSettings? settings) where T : new()
{
var json = await response.ReadAsPresentationJsonAsync<T>(ensureSuccess, settings ?? new JsonSerializerSettings());
var json = await response.ReadAsPresentationJsonAsync<T>(ensureSuccess,
settings ?? new JsonSerializerSettings(IIIFSerialiserX.DeserializerSettings));
return json;
}
}
Loading
Loading