Skip to content

Commit

Permalink
Use the batch named query to retrieve asset details
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLewis-digirati committed Jan 13, 2025
1 parent bbae7b6 commit fbc6f26
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task HandleMessage_DoesNotUpdateAnything_WhenBatchNotTracked()

// Act and Assert
(await sut.HandleMessage(message, CancellationToken.None)).Should().BeTrue();
A.CallTo(() => dlcsClient.RetrieveImagesForManifest(A<int>._, A<string>._, A<CancellationToken>._))
A.CallTo(() => dlcsClient.RetrieveImagesForManifest(A<int>._, A<List<int>>._, A<CancellationToken>._))
.MustNotHaveHappened();
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public async Task HandleMessage_UpdatesBatchedImages_WhenBatchTracked()

var message = GetMessage(batchMessage);

A.CallTo(() => dlcsClient.RetrieveImagesForManifest(A<int>._, A<string>._, A<CancellationToken>._))
A.CallTo(() => dlcsClient.RetrieveImagesForManifest(A<int>._, A<List<int>>._, A<CancellationToken>._))
.Returns(new IIIF.Presentation.V3.Manifest
{
Items = new List<Canvas>
Expand Down Expand Up @@ -149,7 +149,7 @@ public async Task HandleMessage_UpdatesBatchedImages_WhenBatchTracked()

// Assert
handleMessage.Should().BeTrue();
A.CallTo(() => dlcsClient.RetrieveImagesForManifest(A<int>._, A<string>._, A<CancellationToken>._))
A.CallTo(() => dlcsClient.RetrieveImagesForManifest(A<int>._, A<List<int>>._, A<CancellationToken>._))
.MustHaveHappened();
var batch = dbContext.Batches.Single(b => b.Id == batchId);
batch.Status.Should().Be(BatchStatus.Completed);
Expand Down Expand Up @@ -201,7 +201,7 @@ public async Task HandleMessage_DoesNotUpdateBatchedImages_WhenAnotherBatchWaiti

// Act and Assert
(await sut.HandleMessage(message, CancellationToken.None)).Should().BeTrue();
A.CallTo(() => dlcsClient.RetrieveImagesForManifest(A<int>._, A<string>._, A<CancellationToken>._))
A.CallTo(() => dlcsClient.RetrieveImagesForManifest(A<int>._, A<List<int>>._, A<CancellationToken>._))
.MustNotHaveHappened();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ private async Task UpdateAssetsIfRequired(BatchCompletionMessage batchCompletion
"Attempting to complete assets in batch {BatchId} for customer {CustomerId} with the manifest {ManifestId}",
batch.Id, batch.CustomerId, batch.ManifestId);

var batches = dbContext.Batches.Where(b => b.ManifestId == batch.ManifestId).Select(b => b.Id).ToList();

var generatedManifest =
await dlcsOrchestratorClient.RetrieveImagesForManifest(batch.CustomerId, batch.ManifestId!,
await dlcsOrchestratorClient.RetrieveImagesForManifest(batch.CustomerId, batches,
cancellationToken);

UpdateCanvasPaintings(generatedManifest, batch);
Expand Down
77 changes: 77 additions & 0 deletions src/IIIFPresentation/DLCS.Tests/API/DlcsOrchestratorClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Net;
using DLCS.API;
using DLCS.Exceptions;
using IIIF.Presentation.V3;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Stubbery;

namespace DLCS.Tests.API;

public class DlcsOrchestratorClientTests
{
[Theory]
[InlineData(HttpStatusCode.Forbidden)]
[InlineData(HttpStatusCode.Conflict)]
[InlineData(HttpStatusCode.BadRequest)]
public async Task RetrieveImagesForManifest_Throws_IfDownstreamNon200_NoReturnedError(HttpStatusCode httpStatusCode)
{
using var stub = new ApiStub();
const int customerId = 3;
stub.Get($"/iiif-resource/{customerId}/batch-query/1,2", (_, _) => string.Empty).StatusCode((int)httpStatusCode);
var sut = GetClient(stub);

Func<Task> action = () => sut.RetrieveImagesForManifest(customerId, [1, 2], CancellationToken.None);
await action.Should().ThrowAsync<DlcsException>().WithMessage("Could not find a DlcsError in response");
}

[Theory]
[InlineData(HttpStatusCode.Forbidden)]
[InlineData(HttpStatusCode.Conflict)]
[InlineData(HttpStatusCode.BadRequest)]
public async Task RetrieveImagesForManifest_Throws_IfDownstreamNon200_WithReturnedError(HttpStatusCode httpStatusCode)
{
using var stub = new ApiStub();
const int customerId = 4;
stub.Get($"/iiif-resource/{customerId}/batch-query/1,2", (_, _) => "{\"description\":\"I am broken\"}")
.StatusCode((int)httpStatusCode);
var sut = GetClient(stub);

Func<Task> action = () => sut.RetrieveImagesForManifest(customerId, [1, 2], CancellationToken.None);
await action.Should().ThrowAsync<DlcsException>().WithMessage("I am broken");
}

[Fact]
public async Task RetrieveImagesForManifest_ReturnsManifest()
{
using var stub = new ApiStub();
const int customerId = 5;
stub.Get($"/iiif-resource/{customerId}/batch-query/1",
(_, _) => "{\"id\":\"some/id\", \"type\": \"Manifest\" }")
.StatusCode(200);
var sut = GetClient(stub);
var expected = new Manifest() { Id = "some/id" };

var retrievedImages = await sut.RetrieveImagesForManifest(customerId, [1], CancellationToken.None);

retrievedImages.Should().BeEquivalentTo(expected);
}

private static DlcsOrchestratorClient GetClient(ApiStub stub)
{
stub.EnsureStarted();

var httpClient = new HttpClient
{
BaseAddress = new Uri(stub.Address)
};

var options = Options.Create(new DlcsSettings()
{
ApiUri = new Uri("https://localhost"),
MaxBatchSize = 1
});

return new DlcsOrchestratorClient(httpClient, options, new NullLogger<DlcsOrchestratorClient>());
}
}
19 changes: 11 additions & 8 deletions src/IIIFPresentation/DLCS/API/DlcsOrchestratorClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IDlcsOrchestratorClient
/// <summary>
/// Retrieves a DLCS generated manifest of images for a given presentation manifest id
/// </summary>
public Task<Manifest?> RetrieveImagesForManifest(int customerId, string manifestId,
public Task<Manifest?> RetrieveImagesForManifest(int customerId, List<int> batches,
CancellationToken cancellationToken = default);
}

Expand All @@ -18,18 +18,21 @@ public class DlcsOrchestratorClient(
IOptions<DlcsSettings> dlcsOptions,
ILogger<DlcsOrchestratorClient> logger) : IDlcsOrchestratorClient
{
DlcsSettings settings = dlcsOptions.Value;
private readonly DlcsSettings settings = dlcsOptions.Value;

public async Task<Manifest?> RetrieveImagesForManifest(int customerId, string manifestId,
public async Task<Manifest?> RetrieveImagesForManifest(int customerId, List<int> batches,
CancellationToken cancellationToken = default)
{
var batchString = string.Join(',', batches);

logger.LogTrace(
"performing a call to retrieve images for customer {CustomerId} using the manifest {ManifestId}",
customerId, manifestId);

"performing a call to retrieve images for customer {CustomerId} for the batches '{Batches}'",
customerId, batchString);
var response =
await httpClient.GetAsync($"/iiif-resource/{customerId}/{settings.ManifestNamedQueryName}/{manifestId}",
await httpClient.GetAsync($"/iiif-resource/{customerId}/{settings.ManifestNamedQueryName}/{batchString}",
cancellationToken);
return await response.ReadAsIIIFResponse<Manifest>(cancellationToken);

return await response.ReadAsIIIFResponse<Manifest>(cancellationToken);
}
}
2 changes: 1 addition & 1 deletion src/IIIFPresentation/DLCS/DlcsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public class DlcsSettings
/// <summary>
/// The name of the query used for retrieving images
/// </summary>
public string ManifestNamedQueryName { get; set; } = "interim-presentation";
public string ManifestNamedQueryName { get; set; } = "batch-query";
}

0 comments on commit fbc6f26

Please sign in to comment.