In case we need to make two step rest api call using magrite agent? How to go about the same?

Viewed 528

I need to make a rest api call using magrite agent, but the challenge is the api call comprises of two steps.

  • POST call to receive the access token
  • GET call to use the bearer token and call the query

Let me know if anyone can help.

1 Answers

Assuming you get a new access token each time you want to get the data, the process is:

  1. Call to get the token
  2. Extract the token from the response
  3. Call with the token to get the data

The magritte rest plugin supports this!

Preferred Approach

If you have access to edit the magritte source then there is a better way, you can use an Auth Call Source (magritte-rest-auth-call-source) to automate this process for all syncs. The source will perform steps 1 & 2:

type: magritte-rest-v2
sourceMap:
  ...
  my_new_auth_source:
    type: magritte-rest-auth-call-source
    url: 'https://my-api.com/'
  headers:
    Authorization: '{%access_token%}'
  authCall:
    type: magritte-rest-call
    method: GET
    saveResponse: false
    path: /get/access/token
    extractor:
      - type: magritte-rest-json-extractor
        assign:
          access_token: /token/json/path

The call would then look like:

type: rest-source-adapter2
oneFilePerResponse: false
cacheToDisk: false
restCalls:
  - type: magritte-rest-call
    path: /get/data
    method: GET

Alternative

If the data and the token are at different domain names (i.e. requiring different sources), or if you cannot edit the source, then this is an alternative. Keeping any tokens in the source however is preferred, so only use this method if you have to.

You can have multiple calls in a single sync, and state is preserved between the calls, an example might look something like this:

type: rest-source-adapter2
oneFilePerResponse: false
cacheToDisk: false
restCalls:
  - type: magritte-rest-call
    path: /get/access/token
    method: GET
    saveResponse: false
    extractor:
      - type: magritte-rest-json-extractor
        assign:
          access_token: /token/json/path
  - type: magritte-rest-call
    path: /get/data
    method: GET
    saveResponse: true
    parameters:
      token: '{%access_token%}'

A couple of gotchas & other points with this approach:

  • saveResponse is true by default (I left it in the second call for clarity), so it needs to be explicitly set to false in the first call to avoid saving your token in the output dataset!
  • in the event that the data and token are behind different domains (so in different sources) you can add source: my_data/token_source to each call to point them at the right source.

More Detail

More detail can be found in the documentation, open the Foundry docs and search for REST API Plugin to find it.

Related