Edit in GitHubLog an issue

Tutorial Step 6 - Navigating Between API Calls

While the execution information is interesting, what we actually want to send in the notification sent to Microsoft Teams or Slack is the program name. This isn't in the execution response but has to be requested from a different URL, one following the pattern /api/program/{programId}.

While it is possible for you to formulate the URL, it is a best practice to follow the links provided in the API response. The Cloud Manager API responses use a convention named Hypertext Application Language (HAL for short) to define links. But you don't really need to know too many details of HAL to use the API. The important part is that in the API response, there is a _links object which contains a set of objects. Each of these objects has a meaningful name that tells the API consumer where the link goes and the href property of the object has the destination.

For example, in an execution response, you will see this:

Copied to your clipboard
1"_links": {
2 "http://ns.adobe.com/adobecloud/rel/pipeline": {
3 "href": "/api/program/1234/pipeline/5678",
4 "templated": false
5 },
6 "http://ns.adobe.com/adobecloud/rel/program": {
7 "href": "/api/program/1234",
8 "templated": false
9 },
10 "self": {
11 "href": "/api/program/1234/pipeline/5678/execution/9012",
12 "templated": false
13 }
14}

The Cloud Manager API uses templatized links in a few places; in these cases templated will be true, but that's not the case for the program links we need to follow for this tutorial.

Note that these links are relative to the domain name for the API. As with the path, while you could prepend cloudmanager.adobe.io yourself, it is a best practice to have the links be treated as relative links.

This might be overkill for this tutorial, since we only need a single link, but getting a link from an API response is a common enough task that it makes sense to make a separate function for this. It's fairly straightforward object navigation:

Copied to your clipboard
1function getLink (obj, linkType) {
2 return obj['_links'][linkType].href
3}

Updating the getExecution Method

To get the program data based on the execution, first you get the link to the program from the execution response. Remember -- at this point it will be a server-relative path. Then, you use the Node.js URL class to turn that path into an absolute URL and pass this URL to the makeApiCall function to get the program. Finally, the program response is added to the execution response.

Although the URL class is built-in to Node.js, it does need to be imported from the url module:

Copied to your clipboard
const { URL } = require('url')

The updated getExecution function looks like this:

Copied to your clipboard
1async function getExecution (executionUrl) {
2 const accessToken = await getAccessToken()
3
4 const execution = await makeApiCall(accessToken, executionUrl, 'GET')
5
6 const REL_PROGRAM = 'http://ns.adobe.com/adobecloud/rel/program'
7 const programLink = getLink(execution, REL_PROGRAM)
8 const programUrl = new URL(programLink, executionUrl)
9 const program = await makeApiCall(accessToken, programUrl)
10
11 execution.program = program
12
13 return execution
14}

Logging the Program Name in the Webhook

Now that getExecution returns the Program information as part of the execution object, we can easily change the log message to output the program name instead of 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 for ${execution.program.name} 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 and final step. Continue to Step 7.

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