You are here

class SimpleRestMessage in Acquia Cloud Site Factory Connector 8

Same name in this branch
  1. 8 acsf_init/lib/sites/g/SimpleRest.php \Acquia\SimpleRest\SimpleRestMessage
  2. 8 acsf_init/lib/cloud_hooks/common/pre-web-activate/000-acquia-deployment.php \SimpleRestMessage
Same name and namespace in other branches
  1. 8.2 acsf_init/lib/sites/g/SimpleRest.php \Acquia\SimpleRest\SimpleRestMessage

Class SimpleRestMessage.

A simple class used to send REST requests to the Site Factory.

Hierarchy

Expanded class hierarchy of SimpleRestMessage

1 file declares its use of SimpleRestMessage
sites.inc in acsf_init/lib/sites/g/sites.inc
ACSF helper functions for Drupal's multi-site directory aliasing feature.

File

acsf_init/lib/sites/g/SimpleRest.php, line 65
Contains classes needed for sending requests to the Site Factory.

Namespace

Acquia\SimpleRest
View source
class SimpleRestMessage {

  /**
   * Maximum amount of retries before giving up sending a message.
   *
   * @var int
   */
  private $retryMax = 3;

  /**
   * Number of seconds to wait before trying again after sending failed.
   *
   * @var int
   */
  private $retryWait = 5;

  /**
   * The hosting sitegroup name.
   *
   * @var string
   */
  private $site;

  /**
   * The hosting environment name.
   *
   * @var string
   */
  private $env;

  /**
   * Creates a new instance of SimpleRestMessage.
   *
   * @param string $site
   *   The hosting sitegroup name.
   * @param string $env
   *   The hosting environment name.
   */
  public function __construct($site, $env) {
    $this->site = $site;
    $this->env = $env;
  }

  /**
   * Sends a request.
   *
   * @param string $method
   *   The request method.  Either 'POST' or 'GET'.
   * @param string $endpoint
   *   The request endpoint.
   * @param array $parameters
   *   Any required parameters for the request. Note: parameters are currently
   *   only implemented for POST requests. To add support for GET parameters
   *   would require changes in this method.
   * @param SimpleRestCreds $creds
   *   The credentials to use for the Site Factory request.
   *
   * @throws Exception
   *   If the request fails.
   *
   * @return \SimpleRestResponse
   *   The response.
   */
  public function send($method, $endpoint, array $parameters, SimpleRestCreds $creds) {
    $error = '';
    $user_agent = sprintf('%s.%s %s', $this->site, $this->env, gethostname());
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_USERPWD, $creds->name . ":" . $creds->password);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

    // If it is not a GET request, set the method here.
    if ($method != 'GET') {
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    }

    // If we are sending parameters, set the query string or POST fields here.
    $query_string = '';
    if ($method != 'GET' && !empty($parameters)) {
      $data_string = json_encode($parameters);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
      curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string),
      ]);
    }
    $full_url = sprintf('%s/%s%s', $creds->url, $endpoint, $query_string);
    curl_setopt($curl, CURLOPT_URL, $full_url);
    $attempts = 0;
    $response = FALSE;
    while (!$response && ++$attempts <= $this->retryMax) {
      $response = curl_exec($curl);
      if (!$response) {
        $error = curl_error($curl);
        sleep($this->retryWait);
      }
    }
    if (!$response) {
      throw new Exception(sprintf('Error reaching url "%s" with method "%s." Returned error "%s."', $full_url, $method, $error));
    }
    $response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    $response_body = json_decode($response, TRUE);
    if (!is_array($response_body)) {
      $response_body = [];
    }
    curl_close($curl);
    return new SimpleRestResponse($endpoint, $response_code, $response_body);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SimpleRestMessage::$env private property The hosting environment name.
SimpleRestMessage::$retryMax private property Maximum amount of retries before giving up sending a message.
SimpleRestMessage::$retryWait private property Number of seconds to wait before trying again after sending failed.
SimpleRestMessage::$site private property The hosting sitegroup name.
SimpleRestMessage::send public function Sends a request.
SimpleRestMessage::__construct public function Creates a new instance of SimpleRestMessage.