Skip to content

Commit

Permalink
Request validation and OpenAPI schema update
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid committed Aug 9, 2023
1 parent 97ebacf commit 5fa78cd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
25 changes: 25 additions & 0 deletions docs/docs/spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ paths:
description: "The resource is authorized"
403:
description: "The resource is not authorized"
/file-sets/{id}/download:
get:
operationId: getFileSetDownload
tags:
- FileSet
parameters:
- name: id
description: Id of the file set
in: path
required: true
schema:
type: string
- name: email
description: Email to send the download link to
in: query
required: true
schema:
type: string
responses:
200:
description: "The download is being created"
400:
description: "Bad Request"
405:
description: "Method Not allowed for work type, file set role combo"
/works/{id}:
get:
operationId: getWorkById
Expand Down
50 changes: 43 additions & 7 deletions src/handlers/get-file-set-download.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,59 @@
const { wrap } = require("./middleware");
const { getFileSet } = require("../api/opensearch");
// const opensearchResponse = require("../api/response/opensearch");
const opensearchResponse = require("../api/response/opensearch");

/**
* A simple function to get a FileSet by id
*/
exports.handler = wrap(async (event) => {
const id = event.pathParameters.id;
const email = event.queryStringParameters?.email
if(!email){
return invalidRequest(400, "Query string must include email address")
}
const allowPrivate =
event.userToken.isSuperUser() || event.userToken.isReadingRoom();
const allowUnpublished = event.userToken.isSuperUser();
const esResponse = await getFileSet(id, { allowPrivate, allowUnpublished });
console.log(esResponse);

if (esResponse.statusCode == "200") {
const doc = JSON.parse(esResponse.body)
if (downloadAvailable(doc)) {
return processDownload(doc._source.streaming_url, email);
} else {
return invalidRequest(405, "Download only allowed for role: Access, work_type: Video or Audio, with a valid streaming_url")
}
} else {
return await opensearchResponse.transform(esResponse);
}
});

function downloadAvailable(doc) {
console.log("doc.found", doc.found)
console.log("doc._source.role", doc._source.role)
console.log("doc._source.streaming_url", doc._source.streaming_url)
return (
doc.found &&
doc._source.role === "Access" &&
doc._source.streaming_url != null
);
}

function processDownload(streaming_url) {
return {
statusCode: 200,
headers: {
"content-type": "application/json",
},
body: "Hello World",
headers: { "content-type": "text/plain" },
body: JSON.stringify({
message: `Creating download for ${streaming_url}. Check your email for a link.`,
}),
};
// return await opensearchResponse.transform(esResponse);
});
}

function invalidRequest(code, message) {
return {
statusCode: code,
headers: { "content-type": "text/plain" },
body: JSON.stringify({ message: message }),
};
};

0 comments on commit 5fa78cd

Please sign in to comment.