You are here

public function PardotClient::executePardotOperation in Pardot Integration 2.x

Execute the Pardot API operation.

Parameters

string $url: Endpoint URL.

array $request_data: Data to pass into the request.

bool $retry: Flag to know if this is a retry or not.

string $method: (Optional) Request method to use. Defaults to POST.

Return value

mixed Request result. Usually an array, or FALSE on error.

Overrides PardotClientInterface::executePardotOperation

3 calls to PardotClient::executePardotOperation()
PardotClient::assignPardotProspect in src/Service/PardotClient.php
Assigns activity for a Visitor ID to a Prospect ID in Pardot.
PardotClient::getPardotProspect in src/Service/PardotClient.php
Retrieve a Pardot Prospect ID using an email address.
PardotClient::retry in src/Service/PardotClient.php
Retry the original operation with fresh token.

File

src/Service/PardotClient.php, line 185

Class

PardotClient
Provides methods to execute Pardot API operations.

Namespace

Drupal\pardot\Service

Code

public function executePardotOperation(string $url, array $request_data, $retry = FALSE, $method = 'POST') {
  if (!$url) {
    $this->logger
      ->error($this
      ->t('Attempted to execute a Pardot API operation without the endpoint URL'));
    return FALSE;
  }

  // Send a Pardot API request.
  try {
    $response = $this->httpClient
      ->request($method, $url, $request_data);
    $decoded_response = Json::decode($response
      ->getBody()
      ->getContents());

    // Got an error from Pardot.
    // I'm not sure it will ever enter this if statement better safe than sorry i guess.
    if (is_array($decoded_response) && isset($decoded_response['err'])) {

      // If token is invalid then get new one.
      $this->logger
        ->error($this
        ->t("Pardot API operation failed with code: @code. Message: @message", [
        '@code' => $decoded_response['@attributes']['err_code'] ?? 'Unknown Code',
        '@message' => $decoded_response['err'],
      ]));
      return FALSE;
    }
    else {
      $this->logger
        ->notice($this
        ->t("Pardot submission successful. @code.", [
        '@code' => 200,
      ]));
      return $decoded_response;
    }
  } catch (\Exception $e) {
    $decoded_response = Json::decode($e
      ->getResponse()
      ->getBody()
      ->getContents());
    $err_code = $decoded_response['@attributes']['err_code'] ?? 0;
    $err_codes = [
      184,
      401,
    ];
    if (is_array($decoded_response) && isset($decoded_response['err']) && in_array($err_code, $err_codes) && !$retry) {
      return $this
        ->retry($url, $request_data);
    }
    $this->logger
      ->error("Pardot API operation failed. Operation URL: {$url}. Error message: {$e->getMessage()}");
    return FALSE;
  }
}