You are here

public function Hubspot::request in HubSpot 8

Make a request to hubspot.

This function will return an array containing the hubspot response at the index 'value'. In the event that the module is unable to make the request an array with the key 'error', and value of an error message, will be returned instead.

If the hubspot bearer token is expired, an attempt will be made to renew the token.

Parameters

string $method: HTTP Request method.

string $url: HTTP Request URL.

array $options: Guzzle Request options.

Return value

array Hubspot return array.

Throws

\GuzzleHttp\Exception\GuzzleException

3 calls to Hubspot::request()
Hubspot::getHubspotForms in src/Hubspot.php
Get hubspot forms and fields from their API.
Hubspot::hubspotGetRecent in src/Hubspot.php
Gets the most recent HubSpot leads.
Hubspot::submitHubspotForm in src/Hubspot.php
Submit a hubspot form.

File

src/Hubspot.php, line 205

Class

Hubspot
Define a service for interacting with the HubSpot CRM.

Namespace

Drupal\hubspot

Code

public function request(string $method, string $url, array $options = []) : array {
  $access_token = $this->state
    ->get('hubspot.hubspot_access_token');
  if (empty($access_token)) {
    return [
      'error' => $this
        ->t('This site is not connected to a HubSpot Account.'),
    ];
  }
  $options += [
    'headers' => [
      'Authorization' => 'Bearer ' . $access_token,
    ],
  ];
  try {
    $response = $this->httpClient
      ->request($method, $url, $options);
  } catch (RequestException $e) {

    // We need to reauthenticate with hubspot.
    global $base_url;
    $refresh_token = $this->state
      ->get('hubspot.hubspot_refresh_token');
    try {
      $reauth = $this->httpClient
        ->post('https://api.hubapi.com/oauth/v1/token', [
        'headers' => [
          'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
        ],
        'form_params' => [
          'grant_type' => 'refresh_token',
          'client_id' => $this->config
            ->get('hubspot_client_id'),
          'client_secret' => $this->config
            ->get('hubspot_client_secret'),
          'redirect_uri' => $base_url . Url::fromRoute('hubspot.oauth_connect')
            ->toString(),
          'refresh_token' => $refresh_token,
        ],
      ])
        ->getBody();
      $data = Json::decode($reauth);
      $this->state
        ->set('hubspot.hubspot_access_token', $data['access_token']);
      $this->state
        ->set('hubspot.hubspot_refresh_token', $data['refresh_token']);
      $this->state
        ->set('hubspot.hubspot_expires_in', $data['expires_in'] + $this->currentRequest->server
        ->get('REQUEST_TIME'));
      $response = $this->httpClient
        ->request($method, $url, $options);
    } catch (RequestException $e) {
      $this->logger
        ->error($this
        ->t('Unable to execute request: %message', [
        '%message' => $e
          ->getMessage(),
      ]));
      return [
        'error' => $e
          ->getMessage(),
      ];
    }
  }
  $data = $response
    ->getBody()
    ->getContents();
  return [
    'value' => Json::decode($data),
    'response' => $response,
  ];
}