Creating Collections
This source file makes use of a series of sample helper functions that completes the entire flow of getting data needed to create a collection:
These helper utilities can be found in the Getting Started Exercise or by accessing them directly:
- API Configuration Helper
- Collection Data Helper
- Create Collections Helper
- Asset Search Helper
- Metadata Vocabulary Helper
- Metadata Template Helper
- Security Template Helper
Each of these Helpers perform a specific function to assist in API calls such as creating collections where data (especially ID's) is needed that isn't accessible otherwise.
Process
The sequence diagram below shows the typical process to create collections.
service/collection.js
const HttpUtil = require('../util/http');
const createCollection = async (request) => {
// Extract Parameters from Request
const { endpointUrl, apiKey, authorization, accessToken, collection } = request;
const { collectionType } = collection;
// Prepare Request Body
const body = {
collectionType,
name: collection.name,
metadataTemplateId: collection.metadataTemplateId,
metadataDocument: collection.metadataDocument || '[]',
securityTemplateIds: collection.securityTemplateIds,
collectionDocument: collection.collectionDocument || [],
};
// Get Body Content Length
const bodyLength = Buffer.byteLength(JSON.stringify(body));
// Prepare HTTP Request
const httpRequest = {
method: 'POST',
url: `${endpointUrl}/collection`,
headers: {
Accept: 'application/json',
Authorization: authorization,
accessToken,
'Content-Length': bodyLength,
'Content-Type': 'application/json',
'x-api-key': apiKey,
},
body,
};
// Send HTTP Request, Get HTTP Result
const result = await HttpUtil.sendHttpRequest(httpRequest);
// Extract Collection from HTTP Result
const newCollection = result.body;
// Return Collection
return newCollection;
};
module.exports = {
createCollection,
};