ebredy/apichainning

API Chainning

1.1.9 2019-06-24 13:03 UTC

This package is auto-updated.

Last update: 2024-03-25 06:48:04 UTC


README

Currently under development.

For more examples of chaining with Mock APIs - visit http://api-multi-request.demos.mikestowe.com/

REST API Multiple-Request Chaining

One of the challenges in using RESTful APIs driven by Hypermedia, as well as pulling in numerous and extensive microservices is the requirement to at times make several API calls in order to accomplish the task at hand. Today, that requires numerous HTTP calls as well, which depending on latency, can greatly slow script execution.

REST API Multiple-Request Chaining is a technique that groups numerous RESTful API calls together in a single HTTP request.

For example, as shown below, instead of having to do GET calls to /users/5 and then /messages/?userId=5 you could instead send a chain to a resource such as /multirequestchain that would handle the multiple calls for you.

REST API Multiple-Request Chaining is setup to allow for conditional calls, as well as provides back only the data you need (instead of all the data that would be returned from each call). This can also be used as a technique for data collection similar to that of GraphQL where instead of embedding objects as models, you're able to make numerous calls at once and get back only the data you request.

A Simple REST API Multiple-Request Chain

Each chain is made of 5 components, all required:

Component Description Example
doOn DoOn provides the condition on which the call should be performed. Options include "always", a logical if statement ($body.user.firstName == 'Jim') or an HTTP status code (with * being a wildcard). 4**
url The complete path (including querystring) for the call being performed /users/?lastName=Smith
method The HTTP method you wish to use to perform the call (such as GET, POST, etc) get
data A string or JSON Object of the data you wish to send via the call (typically with POST, PUT, PATCH, DELETE) { "firstName" : "Jim", "lastName" : "Smith" }
return The data (as an array) you wish to have returned. If you wish for all data to be returned, use boolean "true" or if you wish for no data to be returned, use boolean "false.". Also, you can use JSONPath expressions, which are described here ["firstName", "email", "_links"]
headers Request headers (as an array). This field is optional. ["Connection" => "keep-alive", "Accept-Encoding" => "gzip, deflate, br"]

A simple request where you want to retrieve a user's messages, but first need to make a call to the /users resource to obtain the information may look like this:

[
  {
    "doOn": "always",
    "url": "/users/5",
    "method": "get",
    "data": {},
    "return": "$.*._links"
  },
  {
    "doOn": 200,
    "url" : "$body._links.messages",
    "method": "get",
    "data": {
        "emailAddress": "$body.email"
    },
    "headers": {
        "Cache-Control": "no-cache",
        "Connection": "keep-alive"
    }
    "return": true,
  }
]

Multiple Conditional Calls

REST API Multiple-Request Chaining works in chronological order, with each call being considered the next highest priority, or the next natural step in the process.

The doOn specifies whether or not the call should be executed based on the previous call's response, or whether it should be skipped for another call that contains a doOn that matches the previous HTTP status code, applies a logical IF statement that returns true, or is specified as "always." In the event that no suitable match is found in the layer of the chain it is currently operating in, the chain will consider this an error and exit - returning back all the data up and including the last call that was attempted.

For example, in our previous chain - IF the /users/5 call returned a 404, the chain would have exited, providing you the details of that call, but NOT attempting the next call $body._links.messages as it required a status code of 200.

You can also layer multiple conditional calls by placing them in arrays, creating a new layer in the process. However, once the chain reaches the end of its child layers, it will exit - NOT iterating through the remaining parent layers.

[
  {
    "doOn": "always",
    "url": "/users/5",
    "method": "get",
    "data": {},
    "return": [
        "email", "_links"
    ]
  },
  [
    {
      "doOn": 200,
      "url" : "$body._links.messages",
      "method": "get",
      "data": {
          "emailAddress": "$body.email"
      },
      "return": true,
    },
    {
      "doOn": "4*|5*",
      "url" : "/users",
      "method": "post",
      "data": {
        "firstName": "Jim",
        "lastName": "Smith",
        "emailAddress": "jim.smith@domain.ext"
      },
      "return": true,
    },
    [
      {
        "doOn": "201",
        "url": "$headers.link",
        "method": "get",
        "data": {},
        "return": [
            "email", "_links"
        ]
      },
      {
        "doOn": 200,
        "url" : "$body._links.sendMessage",
        "method": "post",
        "data": {
          "to": "$body.email",
          "subject": "Welcome $body.firstName",
          "body": "Hello and welcome to our site!"
        },
        "return": true,
      }
    ]
  ]
]

Complex IF Statements

The doOn property accepts an HTTP Status Code, "always" as a string, or a conditional logical IF statements:

Simple Equal/ Not Equal
Logic Example Meaning
== $body.firstName == 'Jim' firstName in Body response is equal to Jim
!= $body.firstName != 'Jim' firstName in Body response is not equal to Jim
>= $body.age >= 10 age in Body response is greater than or equal to 10
<= $body.age <= 10 age in Body response is less than or equal to 10
AND OR STATEMENTS
Logic Example Meaning
&& $body.firstName != 'Jim' && $body.age >= 10 First condition AND second condition must be matched
|| $body.firstName != 'Jim' || $body.age >= 10 Match EITHER condition
REGULAR EXPRESSIONS
Logic Example Meaning
regex() regex('/[a-z]/i', $body.firstName) Match a regular expression
!regex() regex('/[a-z]/i', $body.firstName) Does not match regular expression

Complex Example 1:

[
  {
    "doOn": "always",
    "url": "/users/5",
    "method": "get",
    "data": {},
    "return": [
        "firstName", "lastName", "email", "_links"
    ]
  },
  {
    "doOn": "($body.firstName == "Jim" && $body.lastName == "Smith") || regex('/Jim/i', $body.email)",
    "url" : "$body._links.messages",
    "method": "get",
    "data": {
        "emailAddress": "$body.email"
    },
    "return": true,
  }
]

Complex Example 2:

[
        {
            "doOn": "always",
            "url": "https://www.tn-apis.com/catalog/v1/events/${global.eventId}",
            "method": "get",
            "name": "api:ticketnetwork:catalog:events",
            "globals": {},
            "data": {},
            "headers": {
                "Authorization": "Bearer 9750bc1c-178d-36f7-802b-b6b9ebcb3efd",
                "x-listing-context": "website-config-id=3551",
                "Accept": "application/json"
            },
            "return": {
                "latitudeFromTN": "geoLocation.latitude",
                "longitudeFromTN": "geoLocation.longitude",
                // custom php callbacks are created with 
                //and called by prefixing them with callback.  An addition any php function 
                //can be called using the prefix "callback_"
                "dateFromTN": "${callback_extract_date(date.datetime,0,10)}",  
                "timeFromTN": "${callback_format_time(date.text.time)}",
                "venueId": "venue.id",
                "countryFromTN": "country.alphaCode",
                "stateFromTN": "stateProvince.text.abbr",
                "eventNameFromTN": "text.name",
                "cityFromTN": "city.text.name",
                "isResult": "${callback_has_results(venue.id)}"
            }
        },
        {
            "doOn": "200",
            "url": "https://www.way.com/way-service/home/suggestions",
            "method": "post",
            "name": "api:way:suggestions",
            "data": {
                "latitude": "$body.latitudeFromTN",
                "longitude": "$body.longitudeFromTN",
                "searchQuery": "TNvenue${body.venueId}WAY ${body.dateFromTN} ${body.timeFromTN}",
                "serviceType": "PARKING"
            },
            "headers": {
                "Content-Type": "application/json"
            },
            "return": {
                "wayEventId": "response[0].listingIdentifier",
                "wayVenueId": "response[0].venueId",
                "wayLatitude": "response[0].latitude",
                "wayLongitude": "response[0].longitude",
                "wayAddressLine1": "response[0].addressBo.addressLine1",
                "wayCity": "response[0].addressBo.city.cityName",
                "wayState": "response[0].addressBo.state.stateCode",
                "wayZipcode": "response[0].addressBo.zipcode.zipcode",
                "wayCountry": "response[0].addressBo.country.countryName",
                "wayHasResult": "${callback_has_results(response)}"
            }
        },
        {
            "doOn": "$body.wayHasResult == true",
            "url": "https://www.way.com/way-service/parking/search",
            "method": "post",
            "name": "api:way:parking:search:events",
            "data": {
                "eventId": "$body.wayEventId",
                "reqType": "jump",
                "venueId": "$body.wayVenueId",
                "latitude": "$body.wayLatitude",
                "longitude": "$body.wayLongitude",
                "pageIndex": 1,
                "parkingFilter": {
                    "nearBy": "Event"
                },
                "numberOfRecords": 10
            },
            "headers": {
                "Content-Type": "application/json"
            },
            "return": true
        }
]

Complex Example 3(using headers/ accept :application/xml):

 [
        {
            "doOn": "always",
            "url": "https://link-search.api.cj.com/v2/link-search?website-id=9007016&link-type=banner&advertiser-ids=joined",
            "method": "get",
            "name": "api:cjmanual:v2:linksearch",
            "globals": {},
            "data": {},
            "headers": {
                "Authorization": "Bearer 351q6ebydq98pwv1fttxcgpsqs",
                "Accept": "application/xml"
            },
            "return": true
        }
    ]

Responses

Because conditional chaining and errors are possible when using REST API Multiple-Request Chaining, the response object needs to return three primary properties:

Property Type Definition
callsReqested integer The number of conditionally applicable calls requested in the chain - this will not necessarily be the total number of calls in the request
callsCompleted integer The number of calls that were completed successfully. This helps indicate an error IF the chain had multiple calls, but a condition could not be matched and therefore caused the chain to exit
responses array the list of applicable responses to the API chain calls that were performed, including the last call that could either be executed either because the chain was completed or exited due to being unable to meet a necessary condition.

Within the responses array, each call object needs to include:

Property Definition Example
url the full path of the call that was attempted /users?firstName=Jim
method the method that was used to make the call get
status the HTTP status code the call returned 200
response a response object containing the headers (as an object) and the body (as an object or string depending on content-type) "headers" : { "content-type" : "application/json" }, "body" : { "user" : { "firstName" : "Jim" } }
{
  "callsRequested" : 2,
  "callsCompleted" : 2,
  "responses" : [
    {
      "url" : "/users/5",
      "method" : "get",
      "status" : 200,
      "response" : {
        "headers" : {},
        "body" : {
          "email": "user@user.ext",
          "_links": {}
        }
      }
    },

    {
      "url" : "/messages/?userId=5",
      "method" : "get",
      "status" : 200,
      "response" : {
        "headers" : {},
        "body" : {}
      }
    }
  ]
}

FAQs

How do you send headers with REST API Multiple-Request Chaining?

Headers are sent as they always have been, and will automatically be applied to each of the calls requested in the chain. At this time, REST API Multiple-Request Chaining does not support the ability to send specific headers for each call independently - however, if needed, this may certainly be added in the future.

How does this vary from IO State Driven APIs

REST API Multiple-Request Chaining is designed specifically for RESTful APIs utilizing hypermedia over a static or cached state file. This allows for the available paths to be truly dynamic based on the individual calls within the chain, and also prevents the need to make multiple requests to obtain the IO State or available OPTIONS. REST API Multiple-Request Chaining is also designed to expand beyond just the use of a primary key, letting you pull in information from the headers and body of the previous call when performing an action on the next chain. Each link/ call is also conditional, meaning you can specify when that link should be called, and receive the appropriate error response upon a link/ call failure. For more you can see Owen's IO State implemention here.