ClientService.php in Sparkpost email 8.2
File
src/ClientService.php
View source
<?php
namespace Drupal\sparkpost;
use Drupal\Core\Config\ConfigFactory;
use GuzzleHttp\Client;
use SparkPost\SparkPost;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
class ClientService implements ClientServiceInterface {
const EMAIL_REGEX = '/^\\s*(.+?)\\s*<\\s*([^>]+)\\s*>$/';
protected $configFactory;
protected $client;
public function __construct(Client $client, ConfigFactory $configFactory) {
$this->client = $client;
$this->configFactory = $configFactory;
}
public function getClient() {
$config = $this->configFactory
->get('sparkpost.settings');
$httpClient = new GuzzleAdapter($this->client);
$options = [
'key' => $config
->get('api_key'),
];
if (!empty($config
->get('api_hostname'))) {
$options['host'] = $config
->get('api_hostname');
}
return new SparkPost($httpClient, $options);
}
public function sendMessage(array $message) {
$client = $this
->getClient();
try {
$promise = $client->transmissions
->post($message);
$response = $promise
->wait();
return $response
->getBody();
} catch (\Exception $e) {
\Drupal::logger('sparkpost')
->error($e
->getMessage());
throw $e;
}
}
public function sendRequest($endpoint, array $data, $method = 'GET') {
$client = $this
->getClient();
$promise = $client
->request($method, $endpoint, $data);
$response = $promise
->wait();
return $response
->getBody();
}
}