You are here

protected function Api::getResponse in SendGrid Integration 8

Same name and namespace in other branches
  1. 8.2 modules/sendgrid_integration_reports/src/Api.php \Drupal\sendgrid_integration_reports\Api::getResponse()

Returns response from SendGrid.

Parameters

string $path: Part of SendGrid endpoint.

array $query: Query params to the request.

string $onBehalfOf: Subuser to peform this request on behalf of.

Return value

bool|mixed Decoded json or FALSE.

5 calls to Api::getResponse()
Api::getBouncesBySubuser in modules/sendgrid_integration_reports/src/Api.php
Get bounces by subuser.
Api::getStats in modules/sendgrid_integration_reports/src/Api.php
Returns stats.
Api::getStatsBrowser in modules/sendgrid_integration_reports/src/Api.php
Returns browser stats.
Api::getStatsDevices in modules/sendgrid_integration_reports/src/Api.php
Returns devices stats.
Api::getSubusers in modules/sendgrid_integration_reports/src/Api.php
Get list of subusers.

File

modules/sendgrid_integration_reports/src/Api.php, line 141

Class

Api
Class SendGridReportsController.

Namespace

Drupal\sendgrid_integration_reports

Code

protected function getResponse($path, array $query, string $onBehalfOf = '') {

  // Set headers and create a Guzzle client to communicate with Sendgrid.
  $headers['Authorization'] = 'Bearer ' . $this->apiKey;
  if ($onBehalfOf) {
    $headers['on-behalf-of'] = $onBehalfOf;
  }
  $clienttest = new Client([
    'base_uri' => 'https://api.sendgrid.com/v3/',
    'headers' => $headers,
  ]);

  // Lets attempt the request and catch an error if it fails.
  try {
    $response = $clienttest
      ->get($path, [
      'query' => $query,
    ]);
  } catch (ClientException $e) {
    $code = Xss::filter($e
      ->getCode());
    $this->loggerFactory
      ->get('sendgrid_integration_reports')
      ->error(t('SendGrid Reports module failed to receive data. HTTP Error Code @errno', [
      '@errno' => $code,
    ]));
    $this->messenger
      ->addError(t('SendGrid Reports module failed to receive data. See logs.'));
    return FALSE;
  }

  // Sanitize return before using in Drupal.
  $body = Xss::filter($response
    ->getBody());
  return json_decode($body);
}