Response Transformation in Post-processing

This procedure shows how to create a flow that transforms the response in a post-processing operation.

The example code implements a flow with a GET API call for a range of values from Google Sheets. It uses the convertResult expression to convert the output to an array of values.

  1. Add a flow with a Get call to the Google Sheets API.
    "flows": {  
       "getRowFlow" :{
          "id": "getRowFlow",
          "description": "getRowFlow",
          "specVersion": "0.8",
          "version": "0.1",
          "start": "startState",
          "functions": [
            {
              "name": "generalRestFunc",
              "type": "custom",
              "operation": "connectivity::rest"
            }
          ],
          "states":[
            {
              "name":"startState",
              "type":"operation",
              "actions":[
                {
                  "functionRef": {
                    "refName": "generalRestFunc",
                    "arguments": {
                      "uri": "https://shhets-svc.api.com/v4/spreadsheets/{spreadsheetId}/values/{range}",
                      "method": "GET",
                      "parameters": {
                        "spreadsheetId": "${ .configuration.spreadsheetId }",
                        "range": "${ .configuration.sheetId + \"!A1:Z\" + (.input.rowNumber|tostring) }",
                        "majorDimension": "COLUMNS"
                      }
                    }
                  },
                  "actionDataFilter": {
                    "results": "${ .body.values }",
                    "toStateData": "${ .values }"
                  }
                }
              ],
              "end": true
            }
          ]
        }
    }
  2. Configure the post-processing transformation.
    1. Add a function definition in the flow.
    2. Refer the function defined from the new action in state.
    "functions": [
           ...
           {
              "name": "convertResult",
              "type": "expression",
              "operation": "(if .configuration.rowOperation == \"lastRow\" then (.lastLineNumber-1) else (.input.rowNumber-1) end) as $rowNum | .values | map({key:.[0], value:.[$rowNum]}) | from_entries"
            }
    ]
    ...
     "states":[
            {
              "name":"startState",
              "type":"operation",
              "actions":[
                ...
               {
                  "functionRef": "convertResult",
                  "actionDataFilter": {
                    "toStateData": "${ .output }"
                  }
                }
            ]
  3. Run the effective flow.
    1. Make a Get call to the Google Sheets API for the range till the row number for which action has been configured.
    2. Use the convertResult expression to convert the output to an array of values.
    "flows": {   
       "getRowFlow" :{
          "id": "getRowFlow",
          "description": "getRowFlow",
          "specVersion": "0.8",
          "version": "0.1",
          "start": "startState",
          "functions": [
            {
              "name": "generalRestFunc",
              "type": "custom",
              "operation": "connectivity::rest"
            },
            {
              "name": "convertResult",
              "type": "expression",
              "operation": "(if .configuration.rowOperation == \"lastRow\" then (.lastLineNumber-1) else (.input.rowNumber-1) end) as $rowNum | .values | map({key:.[0], value:.[$rowNum]}) | from_entries"
            }
          ],
          "states":[
            {
              "name":"startState",
              "type":"operation",
              "actions":[
                {
                  "functionRef": {
                    "refName": "generalRestFunc",
                    "arguments": {
                      "uri": "https://shhets-svc.api.com/v4/spreadsheets/{spreadsheetId}/values/{range}",
                      "method": "GET",
                      "parameters": {
                        "spreadsheetId": "${ .configuration.spreadsheetId }",
                        "range": "${ .configuration.sheetId + \"!A1:Z\" + (.input.rowNumber|tostring) }",
                        "majorDimension": "COLUMNS"
                      }
                    }
                  },
                  "actionDataFilter": {
                    "results": "${ .body.values }",
                    "toStateData": "${ .values }"
                  }
                },
                {
                  "functionRef": "convertResult",
                  "actionDataFilter": {
                    "toStateData": "${ .output }"
                  }
                }
              ],
              "end": true
            }
          ]
        }
    }