The following section provides a sample code for a Before Download Worker.
export default async (context: PlatformContext, data: BeforeDownloadRequest): Promise<BeforeDownloadResponse> => { let status: DownloadStatus = DownloadStatus.DOWNLOAD_UNSPECIFIED; let headers: { [key: string]: Header } = {}; try { // The in-browser HTTP client facilitates making calls to the JFrog REST APIs //To call an external endpoint, use 'await context.clients.axios.get("https://foo.com")' const res = await context.clients.platformHttp.get('/artifactory/api/v1/system/readiness'); // You should reach this part if the HTTP request status is successful (HTTP Status 399 or lower) if (res.status === 200) { status = DownloadStatus.DOWNLOAD_PROCEED; headers['Content-Type'] = { value: ['text/plain'] }; console.log("Artifactory ping success"); } else { status = DownloadStatus.DOWNLOAD_WARN; console.warn(`Request is successful but returned status other than 200. Status code : ${res.status}`); } } catch(error) { // The platformHttp client throws PlatformHttpClientError if the HTTP request status is 400 or higher status = DownloadStatus.DOWNLOAD_STOP; console.error(`Request failed with status code ${error.status || '<none>'} caused by : ${error.message}`) } return { status, message: 'Overwritten by worker-service if an error occurs.', headers, } }
Input Parameters
context
Provides baseUrl, token, and clients to communicate with the JFrog Platform (for more information, see PlatformContext).
data
The request with download details sent by Artifactory.
{ "metadata": { // Various immutable download metadata "repoPath": { // The repository path object of the request "key": "key", // The repository key "path": "path", // The path itself "id": "key:path", // The key:path combination "isRoot": false, // Is the path the root? "isFolder": false // Is the path a folder? }, "originalRepoPath": { // The original repository path if a virtual repository is involved "key": "key", "path": "path", "id": "key:path", "isRoot": false, "isFolder": false }, "name": "name", // The file name from path "headOnly": false, // Is it a head request? "checksum": false, // Is it a checksum request? "recursive": false, // Is it a recursive request? "modificationTime": 0, // When a modification has occurred "directoryRequest": false, // Is it a directory request? "metadata": false, // Is it a metadata request? "lastModified": 1, // Last modification time that occurred "ifModifiedSince": 0, // If a modification happened since the last modification time "servletContextUrl": "base", // The URL that points to Artifactory "uri": "jfrog.com", // The request URI "clientAddress": "localhost", // The client address "zipResourcePath": "", // The resource path of the requested zip "zipResourceRequest": false, // Is the request a zip resource request? "replaceHeadRequestWithGet": false, // Should the head request be replaced with GET? "repoType": 1 // Repository type }, "headers": { // The immutable request headers "key": { "value": [] } }, "userContext": { // The user context that sends the request "id": "id", // The username or subject "isToken": false, // Is the context an accessToken? "realm": "realm" // The realm of the user }, "repoPath": { // The response repository path "key": "key", "path": "path", "id": "key:path", "isRoot": false, "isFolder": false } }
Response
{ "data": { "status": 1, // The instruction on how to proceed "message": "Overwritten by worker-service if an error occurs.", // Message to print to the log. If there is an error it will be printed as a warning. "headers": { // The additional headers that can be sent as request "Content-Type": { "value": [ "text/plain" ] } } }, "executionStatus": "STATUS_SUCCESS" }
message and status : These are mandatory fields.
headers: This is an optional field.
Possible Statuses
DownloadStatus.DOWNLOAD_PROCEED
- The worker allows to proceed with download.DownloadStatus.DOWNLOAD_STOP
- The worker forbids download. Download will be aborted.DownloadStatus.DOWNLOAD_WARN
- The worker allows to proceed with download. A warning log with the provided message will be recorded in Artifactory.