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.
I need to make a rest api call using magrite agent, but the challenge is the api call comprises of two steps.
Let me know if anyone can help.
Assuming you get a new access token each time you want to get the data, the process is:
The magritte rest plugin supports this!
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
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!source: my_data/token_source to each call to point them at the right source.More detail can be found in the documentation, open the Foundry docs and search for REST API Plugin to find it.