You are here

protected function GoogleAnalyticsReportsApiFeed::request in Google Analytics Reports 8.3

Execute a query.

1 call to GoogleAnalyticsReportsApiFeed::request()
GoogleAnalyticsReportsApiFeed::query in google_analytics_reports_api/src/GoogleAnalyticsReportsApiFeed.php
Public query method for all Core Reporting API features.

File

google_analytics_reports_api/src/GoogleAnalyticsReportsApiFeed.php, line 499

Class

GoogleAnalyticsReportsApiFeed
Class GoogleAnalyticsReportsApiFeed.

Namespace

Drupal\google_analytics_reports_api

Code

protected function request($url, $params = [], $headers = [], $method = 'GET') {
  $options = [
    'method' => $method,
    'headers' => $headers,
  ];
  if (count($params) > 0) {
    if ($method == 'GET') {
      $url .= '?' . http_build_query($params);
    }
    else {
      $options['data'] = http_build_query($params);
    }
  }
  try {
    $client = new Client();
    if ($method == 'GET') {
      $response = $client
        ->get($url, $options);
    }
    else {
      $response = $client
        ->post($url, $options);
    }
    $this->response = $response
      ->getBody()
      ->getContents();
    if ($response
      ->getStatusCode() == 200) {
      $this->results = json_decode($this->response);
    }
    else {
      $error_vars = [
        '@code' => $response
          ->getStatusCode(),
        '@details' => print_r(json_decode($this->response), TRUE),
      ];
      $this->error = $this
        ->t('<strong>Code</strong>: @code, <strong>Error</strong>: <pre>@details</pre>', $error_vars);
      $this->loggerFactory
        ->error('<strong>Code</strong>: @code, <strong>Error</strong>: <pre>@details</pre>', $error_vars);
    }
  } catch (ClientException $e) {
    $response = $e
      ->getResponse();
    $this->response = $response
      ->getBody()
      ->getContents();
    $error_vars = [
      '@code' => $response
        ->getStatusCode(),
      '@message' => $e
        ->getMessage(),
      '@details' => print_r(json_decode($this->response), TRUE),
    ];
    $this->error = $this
      ->t('<strong>Code</strong>: @code, <strong>Error</strong>: @message, <strong>Message</strong>: <pre>@details</pre>', $error_vars);
    $this->loggerFactory
      ->error('<strong>Code</strong>: @code, <strong>Error</strong>: <pre>@details</pre>', $error_vars);
  }
}