You are here

public function RestExampleClientCalls::index in Examples for Developers 3.x

Retrieve a list of nodes from the remote server.

When we retrieve entities we use GET.

Parameters

int $node_id: The ID of the remote node, if needed. If the ID is NULL, all nodes will be fetched.

Return value

mixed JSON formatted string with the nodes from the remote server.

Throws

\RuntimeException

\GuzzleHttp\Exception\GuzzleException

File

modules/rest_example/src/RestExampleClientCalls.php, line 94

Class

RestExampleClientCalls
Here we interact with the remote service.

Namespace

Drupal\rest_example

Code

public function index($node_id = NULL) {

  // If the configurated URL is an empty string, return nothing.
  if (empty($this->remoteUrl)) {
    return '';
  }
  $id = '';
  if (!empty($node_id) && is_numeric($node_id)) {
    $id = '/' . $node_id;
  }
  $response = $this->client
    ->request('GET', $this->remoteUrl . '/rest/node' . $id, [
    'headers' => $this->clientHeaders,
  ]);
  return Json::decode($response
    ->getBody()
    ->getContents());
}