REST

The specification also supports describing REST invocations in the functions definition using OpenAPI Paths Object.

Here is an example function definition for REST requests with method GET and request target corresponding with URI Template /users/{id}:

{
  "functions":[
    {
      "name":"queryUserById",
      "operation": {
        "/users": {
          "get": {
            "parameters": [{
              "name": "id",
              "in": "path",
              "required": true 
            }]
          }
        }
      },
      "type":"rest"
    }
  ]
}

Note that the Function Definition's operation property must follow the OpenAPI Paths Object specification definition.

The function can be referenced during workflow execution when the invocation of the REST service is desired. For example:

{
  "states":[
    {
      "name":"QueryUserInfo",
      "type":"operation",
      "actions":[
        {
          "functionRef":"queryUserById",
          "arguments":{
            "id":"${ .user.id }"
          }
        }
      ],
      "end":true
    }
  ]
}

Example of the POST request sending the state data as part of the body:

{
  "functions":[
    {
      "name": "createUser",
      "type": "rest",
      "operation": {
        "/users": {
          "post": {
            "requestBody": {
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "string"
                      },
                      "name": {
                        "type": "string"                     
                      },
                      "email": {
                        "type": "string"
                      }
                    },
                    "required": ["name", "email"]
                  }
                }
              }
            }
          }
        }
      }
    }
  ]
}

Note that the requestBody content attribute is described inline rather than a reference to an external document.

You can reference the createUser function and filter the input data to invoke it. Given the workflow input data:

{
  "order":{
    "id":"1234N",
    "products":[
      {
        "name":"Product 1"
      }
    ]
  },
  "user":{
    "name":"John Doe",
    "email":"[email protected]"
  }
}

Function invocation example:

{
  "states":[
    {
      "name":"CreateNewUser",
      "type":"operation",
      "actions":[
        {
          "functionRef":"createUser",
          "actionDataFilter":{
            "fromStateData":"${ .user }",
            "toStateData":"${ .user.id }"
          }
        }
      ],
      "end":true
    }
  ]
}

In this case, only the contents of the user attribute will be passed to the function. The user ID returned by the REST request body will then be added to the state data:

{
  "order":{
    "id":"1234N",
    "products":[
      {
        "name":"Product 1"
      }
    ]
  },
  "user":{
    "id":"5678U",
    "name":"John Doe",
    "email":"[email protected]"
  }
}

The specification does not support the Security Requirement Object since its redundat to function Auth Definition. If provided, this field is ignored.

Last updated