You are here

public function RestClient::apiCall in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 src/Rest/RestClient.php \Drupal\salesforce\Rest\RestClient::apiCall()
  2. 5.0.x src/Rest/RestClient.php \Drupal\salesforce\Rest\RestClient::apiCall()

Make a call to the Salesforce REST API.

Parameters

string $path: Path to resource. If $path begins with a slash, the resource will be considered absolute, and only the instance URL will be pre-pended. This can be used, for example, to issue an API call to a custom Apex Rest endpoint. If $path does not begin with a slash, the resource will be considered relative and the Rest API Endpoint will be pre-pended.

array $params: Parameters to provide.

string $method: Method to initiate the call, such as GET or POST. Defaults to GET.

bool $returnObject: If true, return a Drupal\salesforce\Rest\RestResponse; Otherwise, return json-decoded response body only. Defaults to FALSE for backwards compatibility.

Return value

mixed Response object or response data.

Throws

\GuzzleHttp\Exception\RequestException

Overrides RestClientInterface::apiCall

14 calls to RestClient::apiCall()
RestClient::getDeleted in src/Rest/RestClient.php
Retrieves objects deleted within the given timeframe.
RestClient::getUpdated in src/Rest/RestClient.php
Return a list of SFIDs for the given object for the given timeframe.
RestClient::listResources in src/Rest/RestClient.php
Return a list of available resources for the configured API version.
RestClient::objectCreate in src/Rest/RestClient.php
Create a new object of the given type.
RestClient::objectDelete in src/Rest/RestClient.php
Delete a Salesforce object.

... See full list

1 method overrides RestClient::apiCall()
TestRestClient::apiCall in src/Tests/TestRestClient.php
Short-circuit api calls.

File

src/Rest/RestClient.php, line 175

Class

RestClient
Objects, properties, and methods to communicate with the Salesforce REST API.

Namespace

Drupal\salesforce\Rest

Code

public function apiCall($path, array $params = [], $method = 'GET', $returnObject = FALSE) {
  if (!$this
    ->isInit()) {
    throw new RestException(NULL, $this
      ->t('RestClient is not initialized.'));
  }
  if (strpos($path, '/') === 0) {
    $url = $this->authProvider
      ->getInstanceUrl() . $path;
  }
  else {
    $url = $this->authProvider
      ->getApiEndpoint() . $path;
  }
  try {
    $this->response = new RestResponse($this
      ->apiHttpRequest($url, $params, $method));
  } catch (RequestException $e) {

    // RequestException gets thrown for any response status but 2XX.
    $this->response = $e
      ->getResponse();

    // Any exceptions besides 401 get bubbled up.
    if (!$this->response || $this->response
      ->getStatusCode() != 401) {
      throw new RestException($this->response, $e
        ->getMessage(), $e
        ->getCode(), $e);
    }
  }
  if ($this->response
    ->getStatusCode() == 401) {

    // The session ID or OAuth token used has expired or is invalid: refresh
    // token. If refresh_token() throws an exception, or if apiHttpRequest()
    // throws anything but a RequestException, let it bubble up.
    $this->authToken = $this->authManager
      ->refreshToken();
    try {
      $this->response = new RestResponse($this
        ->apiHttpRequest($url, $params, $method));
    } catch (RequestException $e) {
      $this->response = $e
        ->getResponse();
      throw new RestException($this->response, $e
        ->getMessage(), $e
        ->getCode(), $e);
    }
  }
  if (empty($this->response) || (int) floor($this->response
    ->getStatusCode() / 100) != 2) {
    throw new RestException($this->response, $this
      ->t('Unknown error occurred during API call "@call": status code @code : @reason', [
      '@call' => $path,
      '@code' => $this->response
        ->getStatusCode(),
      '@reason' => $this->response
        ->getReasonPhrase(),
    ]));
  }
  $this
    ->updateApiUsage($this->response);
  if ($returnObject) {
    return $this->response;
  }
  else {
    return $this->response->data;
  }
}