如果我们需要使用magrite代理进行两步rest api调用?怎么去一样?

问题描述

我需要使用magrite代理进行rest api调用,但是挑战在于api调用包括两个步骤。

让我知道是否有人可以提供帮助。

解决方法

假设每次要获取数据时都获得一个新的访问令牌,则过程为:

  1. 致电获取令牌
  2. 从响应中提取令牌
  3. 调用令牌以获取数据

magritte rest插件支持此功能!

首选方法

如果您有权编辑magritte源,那么有一种更好的方法,您可以使用Auth Call Source(magritte-rest-auth-call-source)来自动完成所有同步过程。源将执行步骤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

呼叫将如下所示:

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

替代

如果数据和令牌位于不同的域名(即要求使用不同的源),或者如果您无法编辑源,则这是替代方法。但是,最好将所有令牌都保留在源中,因此仅在必要时使用此方法。

您可以在一个同步中拥有多个呼叫,并且呼叫之间的状态得以保留,示例可能如下所示:

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%}'

这种方法的一些陷阱和其他要点:

  • saveResponse在默认情况下为true(为清晰起见,我在第二个调用中将其保留),因此在第一次调用中需要将其显式设置为false,以避免将令牌保存在输出数据集中!
  • 如果数据和令牌位于不同的域之内(因此在不同的来源中),则可以向每个调用添加source: my_data/token_source,以将它们指向正确的来源。

更多详细信息

可以在文档中找到更多详细信息,打开Foundry文档并搜索REST API Plugin来找到它。