Edit in GitHubLog an issue

Tutorial Step 5 - Getting Execution Data

Now that we have an access token, we can use it to make API calls into Cloud Manager to get more information about the execution (and in the next step the program).

Adding Dependencies

For the API calls, we'll use the node-fetch library. If you are editing the script locally, you'll need to install this package:

Copied to your clipboard
npm install node-fetch

If you are running the webhook in Glitch, you'll need to edit the package.json file manually and add these this package to the dependencies object. Take a look at the Remix link below if you need help doing this.

The header of the script also needs to be updated to include this dependency.

Copied to your clipboard
const fetch = require('node-fetch')

Writing a Generic makeApiCall function

Making an API call to Cloud Manager requires several headers to be passed. Since we are ultimately going to be making two separate API calls (one for the execution and one for the program), it makes sense to centralize this logic into a new function.

The function itself is pretty simple -- it accepts the access token, a URL, and an HTTP method and then makes a request to that URL with the supplied method setting the three required headers:

  • x-gw-ims-org-id - the Organization ID (contained in the ORGANIZATION_ID variable)
  • x-api-key - the Client ID (contained in the CLIENT_ID variable)
  • Authorization - contains the access token

The function then returns the response body as a JavaScript object.

Copied to your clipboard
1async function makeApiCall (accessToken, url, method) {
2 const response = await fetch(url, {
3 'method': method,
4 'headers': {
5 'x-gw-ims-org-id': process.env.ORGANIZATION_ID,
6 'x-api-key': process.env.CLIENT_ID,
7 'Authorization': `Bearer ${accessToken}`
8 }
9 })
10
11 return response.json()
12}

Writing a Specific getExecution Function

With the generic function in place, the function to get an execution is pretty simple. It just needs to call the getAccessToken function (created in the last step) and then makes a GET request to the execution URL.

Copied to your clipboard
1async function getExecution (executionUrl) {
2 const accessToken = await getAccessToken()
3
4 return makeApiCall(accessToken, executionUrl, 'GET')
5}

Getting the Execution in the Webhook

Finally, we can call the getExecution function with the URL contained in the event payload. There's a variety of information in the execution response (take a look at the API Reference for all the details), but for now let's just log the execution id.

Copied to your clipboard
1if (STARTED === event['@type'] &&
2 EXECUTION === event['xdmEventEnvelope:objectType']) {
3 console.log('received execution start event')
4
5 const executionUrl = event['activitystreams:object']['@id']
6
7 getExecution(executionUrl).then(execution => {
8 console.log(`Execution ${execution.id} started`)
9 })
10}

Running the Updated Webhook

If you are running the script locally, you'll need to stop and restart the node process. You don't need to restart ngrok. In fact, if you do restart ngrok, the URL will likely change and you'll need to go back into the Adobe Developer Console and update the Webhook URL.

If you are running the script through Glitch, Glitch will restart automatically. If you don't want to update your existing Glitch project (or lost it), you can click the button below to start over.

Remix in Glitch

Next Step

With all that done, you're ready to proceed to the next step. Continue to Step 6.

  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.