You are here

class SendinblueHttpClient in SendinBlue 8

Same name and namespace in other branches
  1. 8.2 src/Tools/Http/SendinblueHttpClient.php \Drupal\sendinblue\Tools\Http\SendinblueHttpClient

Sendinblue REST client.

Hierarchy

Expanded class hierarchy of SendinblueHttpClient

2 files declare their use of SendinblueHttpClient
SendinblueApiV2.php in src/Tools/Api/SendinblueApiV2.php
SendinblueApiV3.php in src/Tools/Api/SendinblueApiV3.php
1 string reference to 'SendinblueHttpClient'
sendinblue.services.yml in ./sendinblue.services.yml
sendinblue.services.yml
1 service uses SendinblueHttpClient
sendinblue.api.http_client in ./sendinblue.services.yml
Drupal\sendinblue\Tools\Http\SendinblueHttpClient

File

src/Tools/Http/SendinblueHttpClient.php, line 13

Namespace

Drupal\sendinblue\Tools\Http
View source
class SendinblueHttpClient {
  const WEBHOOK_WS_SIB_URL = 'http://ws.mailin.fr/';

  /**
   * Sib ApiKey.
   *
   * @var string
   */
  public $apiKey;

  /**
   * Sib BaseURL.
   *
   * @var string
   */
  public $baseUrl;

  /**
   * GuzzleClient to comm with Sib.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  public $client;

  /**
   * Logger Service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactory
   */
  protected $loggerFactory;

  /**
   * SendinblueHttpClient constructor.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   LoggerChannelFactory.
   * @param \GuzzleHttp\ClientInterface $http_client
   *   ClientInterface.
   */
  public function __construct(LoggerChannelFactoryInterface $logger_factory, ClientInterface $http_client) {
    if (!function_exists('curl_init')) {
      $msg = 'SendinBlue requires CURL module';
      $logger_factory
        ->get('sendinblue')
        ->error($msg);
      return;
    }
    $this->loggerFactory = $logger_factory;
    $this->client = $http_client;
  }

  /**
   * Set the APIKEy for use HTTP cURLs.
   *
   * @param string $apikey
   *   Sendinblue APIKEY.
   */
  public function setApiKey($apikey) {
    $this->apiKey = $apikey;
  }

  /**
   * Set the URL for use HTTP cURLs.
   *
   * @param string $baseUrl
   *   SendInBlue URL.
   */
  public function setBaseUrl($baseUrl) {
    $this->baseUrl = $baseUrl;
  }

  /**
   * Do CURL request with authorization.
   *
   * @param string $resource
   *   A request action of api.
   * @param string $method
   *   A method of curl request.
   * @param string $input
   *   A data of curl request.
   *
   * @return array
   *   An associate array with respond data.
   */
  private function doRequest($resource, $method, $input) {
    if (!function_exists('curl_init')) {
      $msg = 'SendinBlue requires CURL module';
      $this->loggerFactory
        ->get('sendinblue')
        ->error($msg);
      return NULL;
    }
    $called_url = $this->baseUrl . "/" . $resource;
    $options = [
      'headers' => [
        'api-key' => $this->apiKey,
        'Content-Type' => 'application/json',
      ],
      'verify' => FALSE,
    ];
    if (!empty($input)) {
      $options['body'] = $input;
    }
    try {
      $clientRequest = $this->client
        ->request($method, $called_url, $options);
      $body = $clientRequest
        ->getBody();
    } catch (RequestException $e) {
      $this->loggerFactory
        ->get('sendinblue')
        ->error('Curl error: @error', [
        '@error' => $e
          ->getMessage(),
      ]);
    }
    return Json::decode($body);
  }

  /**
   * Do CURL request directly into sendinblue.
   *
   * @param string $called_url
   *   URL.
   * @param string $method
   *   cURL method.
   * @param array $data
   *   A data of curl request.
   * @param array $options
   *   A data of curl options.
   *
   * @return array
   *   An associate array with respond data.
   *
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public function doRequestDirect(string $called_url, string $method, array $data, array $options) {
    if (!function_exists('curl_init')) {
      $msg = 'SendinBlue requires CURL module';
      $this->loggerFactory
        ->get('sendinblue')
        ->error($msg);
      return NULL;
    }
    try {
      $clientRequest = $this->client
        ->request($method, $called_url, $options);
      $body = $clientRequest
        ->getBody();
    } catch (RequestException $e) {
      $this->loggerFactory
        ->get('sendinblue')
        ->error('Curl error: @error', [
        '@error' => $e
          ->getMessage(),
      ]);
    }
    return Json::decode($body);
  }

  /**
   * Get Request of API.
   *
   * @param string $resource
   *   A request action.
   * @param string $input
   *   A data of curl request.
   *
   * @return array
   *   A respond data.
   */
  public function get($resource, $input) {
    return $this
      ->doRequest($resource, 'GET', $input);
  }

  /**
   * Put Request of API.
   *
   * @param string $resource
   *   A request action.
   * @param string $input
   *   A data of curl request.
   *
   * @return array
   *   A respond data.
   */
  public function put($resource, $input) {
    return $this
      ->doRequest($resource, 'PUT', $input);
  }

  /**
   * Post Request of API.
   *
   * @param string $resource
   *   A request action.
   * @param string $input
   *   A data of curl request.
   *
   * @return array
   *   A respond data.
   */
  public function post($resource, $input) {
    return $this
      ->doRequest($resource, 'POST', $input);
  }

  /**
   * Delete Request of API.
   *
   * @param string $resource
   *   A request action.
   * @param string $input
   *   A data of curl request.
   *
   * @return array
   *   A respond data.
   */
  public function delete($resource, $input) {
    return $this
      ->doRequest($resource, 'DELETE', $input);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SendinblueHttpClient::$apiKey public property Sib ApiKey.
SendinblueHttpClient::$baseUrl public property Sib BaseURL.
SendinblueHttpClient::$client public property GuzzleClient to comm with Sib.
SendinblueHttpClient::$loggerFactory protected property Logger Service.
SendinblueHttpClient::delete public function Delete Request of API.
SendinblueHttpClient::doRequest private function Do CURL request with authorization.
SendinblueHttpClient::doRequestDirect public function Do CURL request directly into sendinblue.
SendinblueHttpClient::get public function Get Request of API.
SendinblueHttpClient::post public function Post Request of API.
SendinblueHttpClient::put public function Put Request of API.
SendinblueHttpClient::setApiKey public function Set the APIKEy for use HTTP cURLs.
SendinblueHttpClient::setBaseUrl public function Set the URL for use HTTP cURLs.
SendinblueHttpClient::WEBHOOK_WS_SIB_URL constant
SendinblueHttpClient::__construct public function SendinblueHttpClient constructor.