class SendinblueHttpClient in SendinBlue 8
Same name and namespace in other branches
- 8.2 src/Tools/Http/SendinblueHttpClient.php \Drupal\sendinblue\Tools\Http\SendinblueHttpClient
Sendinblue REST client.
Hierarchy
- class \Drupal\sendinblue\Tools\Http\SendinblueHttpClient
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'
1 service uses SendinblueHttpClient
File
- src/
Tools/ Http/ SendinblueHttpClient.php, line 13
Namespace
Drupal\sendinblue\Tools\HttpView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SendinblueHttpClient:: |
public | property | Sib ApiKey. | |
SendinblueHttpClient:: |
public | property | Sib BaseURL. | |
SendinblueHttpClient:: |
public | property | GuzzleClient to comm with Sib. | |
SendinblueHttpClient:: |
protected | property | Logger Service. | |
SendinblueHttpClient:: |
public | function | Delete Request of API. | |
SendinblueHttpClient:: |
private | function | Do CURL request with authorization. | |
SendinblueHttpClient:: |
public | function | Do CURL request directly into sendinblue. | |
SendinblueHttpClient:: |
public | function | Get Request of API. | |
SendinblueHttpClient:: |
public | function | Post Request of API. | |
SendinblueHttpClient:: |
public | function | Put Request of API. | |
SendinblueHttpClient:: |
public | function | Set the APIKEy for use HTTP cURLs. | |
SendinblueHttpClient:: |
public | function | Set the URL for use HTTP cURLs. | |
SendinblueHttpClient:: |
constant | |||
SendinblueHttpClient:: |
public | function | SendinblueHttpClient constructor. |