Skip to content

Commit

Permalink
Add ?as=iiif param to top level collections endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewjordan committed Jul 22, 2024
1 parent df48b0f commit 9d1c62d
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 32 deletions.
5 changes: 4 additions & 1 deletion node/src/api/request/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ function extractRequestedModels(requestedModels) {

function validModels(models, format) {
if (format === "iiif") {
return models.length == 1 && models.every((model) => model === "works");
return (
models.length == 1 &&
models.every((model) => model === "works" || "collections")
);
}
return models.every(isAllowed);
}
Expand Down
105 changes: 77 additions & 28 deletions node/src/api/response/iiif/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ async function buildCollection(responseBody, pageInfo) {
}

function getItems(hits, pageInfo) {
const items = hits.map((item) => loadItem(item["_source"]));
/**
* If the queryUrl pathname is "/collections" then we know this
* is a top-level "collection of collections" and set itemType as
* "Collection". Otherwise, we know this is a collection of Manifests
*/
const queryUrl = new URL(pageInfo.query_url);
const pathname = queryUrl.pathname.split("/").pop();
const itemType = pathname === "collections" ? "Collection" : "Manifest";

const items = hits.map((item) => loadItem(item["_source"], itemType));

if (pageInfo?.next_url) {
items.push({
Expand Down Expand Up @@ -120,36 +129,76 @@ function homepageUrl(pageInfo) {
return result;
}

function loadItem(item) {
return {
id: item.iiif_manifest,
type: "Manifest",
homepage: [
{
id: new URL(`/items/${item.id}`, dcUrl()),
type: "Text",
format: "text/html",
label: {
none: [`${item.title}`],
function loadItem(item, itemType) {
if (itemType === "Manifest") {
return {
id: item.iiif_manifest,
type: "Manifest",
homepage: [
{
id: new URL(`/items/${item.id}`, dcUrl()),
type: "Text",
format: "text/html",
label: {
none: [`${item.title}`],
},
},
],
label: {
none: [`${item.title}`],
},
],
label: {
none: [`${item.title}`],
},
summary: {
none: [`${item.work_type}`],
},
thumbnail: [
{
id: item.thumbnail,
format: "image/jpeg",
type: "Image",
width: 400,
height: 400,
summary: {
none: [`${item.work_type}`],
},
],
};
thumbnail: [
{
id: item.thumbnail,
format: "image/jpeg",
type: "Image",
width: 400,
height: 400,
},
],
};
}

if (itemType === "Collection") {
return {
id: `${item.api_link}?as=iiif`,
type: "Collection",
label: {
none: [`${item.title}`],
},
...(item.description && {
summary: {
none: [`${item.description}`],
},
}),
...(item.thumbnail && {
thumbnail: [
{
id: item.thumbnail,
type: "Image",
format: "image/jpeg",
width: 400,
height: 400,
},
],
}),
...(item.canonical_link && {
homepage: [
{
id: new URL(`/collections/${item.id}`, dcUrl()),
type: "Text",
format: "text/html",
label: {
none: [`${item.title}`],
},
},
],
}),
};
}
}

module.exports = { transform };
25 changes: 22 additions & 3 deletions node/src/handlers/get-collections.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
const { doSearch } = require("./search-runner");
const { wrap } = require("./middleware");

const getCollections = async (event) => {
event.pathParameters.models = "collections";
event.body = { query: { match_all: {} } };
return doSearch(event, { includeToken: false });
};

const getCollectionsAsIiif = async (event) => {
event.pathParameters.models = "collections";
event.body = { query: { match_all: {} } };
event.queryStringParameters.collectionLabel =
"Northwestern Digital Collections";
event.queryStringParameters.collectionSummary = "Something fancy";

return doSearch(event, {
includeToken: false,
parameterOverrides: { as: "iiif" },
});
};

/**
* A simple function to get Collections
*/
exports.handler = wrap(async (event) => {
event.pathParameters.models = "collections";
event.body = { query: { match_all: {} } };
return doSearch(event, { includeToken: false });
return event.queryStringParameters?.as === "iiif"
? getCollectionsAsIiif(event)
: getCollections(event);
});
24 changes: 24 additions & 0 deletions node/test/integration/get-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,29 @@ describe("Collections route", () => {
const url = new URL(query_url);
expect(url.pathname).to.eq("/api/v2/search/collections");
});

it("returns top level collection as a IIIF collection", async () => {
mock
.post("/dc-v2-collection/_search", makeQuery({ size: 10, from: 0 }))
.reply(200, helpers.testFixture("mocks/collections.json"));

const event = helpers
.mockEvent("GET", "/collections")
.queryParams({ as: "iiif" })
.render();
const result = await handler(event);
expect(result.statusCode).to.eq(200);
expect(result).to.have.header(
"content-type",
/application\/json;.*charset=UTF-8/
);
const resultBody = JSON.parse(result.body);
expect(resultBody.type).to.eq("Collection");
expect(resultBody.label.none[0]).to.eq(
"Northwestern Digital Collections"
);
expect(resultBody.summary.none[0]).to.eq("Something fancy");
expect(resultBody.items.length).to.eq(69);
});
});
});
41 changes: 41 additions & 0 deletions node/test/unit/api/response/iiif/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,44 @@ describe("IIIF Collection response transformer", () => {
expect(body.homepage[0].id).to.contain("search?similar=1234");
});
});

describe("IIIF Collection response for top level colllections", () => {
helpers.saveEnvironment();

let pager = new Paginator(
"http://dcapi.library.northwestern.edu/api/v2/",
"collections",
["collections"],
{ query: { match_all: {} } },
"iiif",
{
includeToken: false,
parameterOverrides: { as: "iiif" },
queryStringParameters: {
as: "iiif",
collectionLabel: "Northwestern Digital Collections",
collectionSummary: "Something fancy",
},
}
);

pager.pageInfo.query_url =
"http://dcapi.library.northwestern.edu/api/v2/collections?as=iiif";

it("transform a collection of collections response", async () => {
const response = {
statusCode: 200,
body: helpers.testFixture("mocks/collections.json"),
};

const result = await transformer.transform(response, pager);
expect(result.statusCode).to.eq(200);

const body = JSON.parse(result.body);
expect(body.type).to.eq("Collection");
expect(body.label.none[0]).to.eq("Northwestern Digital Collections");
expect(body.summary.none[0]).to.eq("Something fancy");
expect(body.items.length).to.eq(69);
expect(body.items[0].type).to.eq("Collection");
});
});

0 comments on commit 9d1c62d

Please sign in to comment.