cached_service

Fetch information on an external service using cache

Definition

any cached_service(string $service, string $operation, any $data, boolean $safeMode)

Fetches information from a connected service. The service needs to first be defined under Connected services. Faster than service in case of repeated calls because of caching, and includes a safe mode.

Parameters

string $service

Name of the service.

string $operation

Name of the operation.

any $data

Data to send.

boolean $safeMode

If true, treat errors as warnings and return null on error.

Returns

any

The response body of an external service.

Examples

{
  "post": {
    "id": "3"
  },
  "wrongPost": {
    "id": "0"
  }
}
{
  a: cached_service('JSONPlaceholder', 'post', {
    id: post.id
  }, `true`),
  b: cached_service('JSONPlaceholder', 'post', {            // Warning.
    id: wrongPost.id
  }, `true`),
  c: cached_service('JSONPlaceholder', 'post', {
    id: wrongPost.id
  }, `false`)
}
{
  "a": {
    "userId": 1,
    "id": 3,
    "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
    "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantiu... (TRUNCATED)"
  },
  "b": null,
  "c": null
}

Remarks

The examples b and c both try to fetch a post with a non-existent id. b has $safeMode set to true, so it returns null and raises a warning instead of an error. It might seem like c will then throw an error since it's not run in safe mode, but instead it also returns null. This is because the result from the previous call is cached and returned with no error. If the unsafe call is performed first, an error is raised as expected. If there is no cached call, the function works equivalently to service, except that it includes an option for safe mode.