You are here

protected function JanrainCaptureApi::call in Janrain Registration 8

Performs the HTTP request to Janrain.

Parameters

string $command: The Capture command to perform.

array $data: The data set to pass via POST.

string $access_token: The client access token to use when performing user-specific calls.

Return value

\stdClass The HTTP request result data.

Throws

\GuzzleHttp\Exception\TransferException

\Drupal\janrain_capture\Exception\JsonParseError

\Drupal\janrain_capture\Exception\JanrainApiCallError

2 calls to JanrainCaptureApi::call()
JanrainCaptureApi::getEntity in src/JanrainCaptureApi.php
Returns the user's profile data.
JanrainCaptureApi::getToken in src/JanrainCaptureApi.php
Returns requested access token.

File

src/JanrainCaptureApi.php, line 339

Class

JanrainCaptureApi
The integration between Janrain and Drupal.

Namespace

Drupal\janrain_capture

Code

protected function call($command, array $data = [], string $access_token = NULL) : \stdClass {
  $headers = [];
  if ($access_token !== NULL) {
    $headers['Authorization'] = "OAuth {$access_token}";
  }
  $data['client_id'] = $this->clientId;
  $data['client_secret'] = $this->clientSecret;
  try {
    $result = $this->httpClient
      ->post($this->captureAddress . '/' . $command, [
      'headers' => $headers,
      'form_params' => $data,
    ]);
  } catch (RequestException $e) {
    $result = $e
      ->getResponse();
  } catch (TransferException $e) {
    $this->logger
      ->error('The exception is thrown during API call: @message', [
      '@message' => $e
        ->getMessage(),
    ]);
    throw $e;
  }
  $body = (string) $result
    ->getBody();
  $json = json_decode($body);
  if ($json === NULL) {
    $this->logger
      ->error('JSON parse error for response data: @data', [
      '@data' => $body,
    ]);
    throw new JsonParseError($body);
  }
  if ($json->stat === 'error') {
    $this->logger
      ->error('Error response received: @response', [
      '@response' => var_export($json, TRUE),
    ]);
    throw new JanrainApiCallError($json->error_description, $json->code, $json);
  }
  return $json;
}