class HttpClient in HTTP Client Manager 8.2
Same name and namespace in other branches
- 8 src/HttpClient.php \Drupal\http_client_manager\HttpClient
The http client.
Hierarchy
- class \Drupal\http_client_manager\HttpClient implements HttpClientInterface uses MessengerTrait, StringTranslationTrait
Expanded class hierarchy of HttpClient
1 file declares its use of HttpClient
- HttpClientTest.php in tests/
src/ Unit/ HttpClientTest.php
1 string reference to 'HttpClient'
1 service uses HttpClient
File
- src/
HttpClient.php, line 22
Namespace
Drupal\http_client_managerView source
class HttpClient implements HttpClientInterface {
use StringTranslationTrait;
use MessengerTrait;
/**
* The name of the service api of this http client instance.
*
* @var string
*/
protected $serviceApi;
/**
* Description definition.
*
* @var \GuzzleHttp\Command\Guzzle\Description
*/
protected $description;
/**
* The Http Service Api Handler service.
*
* @var HttpServiceApiHandler
*/
protected $apiHandler;
/**
* An array containing the Http Service Api description.
*
* @var array
*/
protected $api;
/**
* An array containing api source path info.
*
* @var array
*/
protected $apiSourceInfo;
/**
* Guzzle Client definition.
*
* @var \GuzzleHttp\Command\Guzzle\GuzzleClient
*/
protected $client;
/**
* Event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* An array containing all the Guzzle commands.
*
* @var array
*/
protected $commands;
/**
* The file locator used to find the service descriptions.
*
* @var \Symfony\Component\Config\FileLocator
*/
protected $fileLocator;
/**
* The file loader used to load the service descriptions.
*
* @var \Guzzle\Service\Loader\FileLoader
*/
protected $fileLoader;
/**
* Constructs an HttpClient object.
*
* @param string $serviceApi
* The service api name for this instance.
* @param \Drupal\http_client_manager\HttpServiceApiHandlerInterface $apiHandler
* The service api handler instance.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher instance.
*/
public function __construct($serviceApi, HttpServiceApiHandlerInterface $apiHandler, EventDispatcherInterface $event_dispatcher) {
$this->serviceApi = $serviceApi;
$this->apiHandler = $apiHandler;
$this->api = $this->apiHandler
->load($this->serviceApi);
$this->eventDispatcher = $event_dispatcher;
}
/**
* {@inheritdoc}
*/
public function getApi() {
return $this->api;
}
/**
* Get Api source path info.
*
* @return array
* An array containing api source path info.
*/
protected function getApiSourceInfo() {
if (empty($this->apiSourceInfo)) {
$this
->setApiSourceInfo();
}
return $this->apiSourceInfo;
}
/**
* Set Api source path info.
*/
protected function setApiSourceInfo() {
$this->apiSourceInfo = pathinfo($this->api['source']);
}
/**
* Get Client.
*
* @return \GuzzleHttp\Command\Guzzle\GuzzleClient
* The Configured Guzzle client instance.
*/
protected function getClient() {
if (empty($this->client)) {
$this
->setupGuzzleClient();
}
return $this->client;
}
/**
* Get Client by Command.
*
* Get or create an instance of the Guzzle Client, with overridden
* configurations or not, based on the given command name.
* If the service api description has been overridden via settings.php and
* has been defined a "commands['whitelist']" or "commands['blacklist']"
* property, the following algorithm will be applied:
* - Commands in blacklist or not in whitelist must not use service overrides.
* - All the other commands will use the service overrides.
*
* @param string $commandName
* The Guzzle Command name.
*
* @return \GuzzleHttp\Command\Guzzle\GuzzleClient
* The Configured Guzzle client instance
*/
protected function getClientByCommand($commandName) {
$api = $this
->getApi();
if (empty($api['orig']) || empty($api['commands'])) {
return $this
->getClient();
}
$cmds = $api['commands'];
$config = $api['config'];
// Commands in blacklist or not in whitelist must not use service overrides.
if (!empty($cmds['blacklist']) && in_array($commandName, $cmds['blacklist']) || !empty($cmds['whitelist']) && !in_array($commandName, $cmds['whitelist'])) {
$config = $api['orig']['config'];
$config['handler'] = $this
->getClientConfig()['handler'];
}
$this->client = $this
->createGuzzleClient($config);
return $this->client;
}
/**
* Setup Guzzle Client from *.http_services_api.yml files.
*/
private function setupGuzzleClient() {
$config = $this
->getClientConfig();
$this->client = $this
->createGuzzleClient($config);
}
/**
* Create a new Guzzle Client by config.
*
* @param array $config
* An array of configurations used to create a new Guzzle Client.
*
* @return \GuzzleHttp\Command\Guzzle\GuzzleClient
* A Guzzle Client instance.
*/
private function createGuzzleClient(array $config) {
$client = new Client($config);
return new GuzzleClient($client, $this
->loadServiceDescription($config));
}
/**
* {@inheritdoc}
*/
public function getClientConfig() {
$api = $this
->getApi();
$config = !empty($api['config']) ? $api['config'] : [];
$config['handler'] = HandlerStack::create();
if (isset($config['debug']) && !is_bool($config['debug'])) {
if ($debug_file = fopen($config['debug'], FILE_APPEND)) {
$config['debug'] = $debug_file;
}
else {
$message = $this
->t('HTTP Client Manager was unable to write to debug log file: @file', [
'@file' => $config['debug'],
]);
$this
->messenger()
->addWarning($message);
$config['debug'] = FALSE;
}
}
$event = new HttpClientHandlerStackEvent($config['handler'], $this->serviceApi);
$this->eventDispatcher
->dispatch(HttpClientEvents::HANDLER_STACK, $event);
return $config;
}
/**
* {@inheritdoc}
*/
protected function loadServiceDescription($config) {
$base_uri = $config['base_uri'];
if (empty($this->description[$base_uri])) {
$source = $this
->getApiSourceInfo();
$loader = $this
->getFileLoader();
$locator = $this
->getFileLocator();
$description = $loader
->load($locator
->locate($source['basename']));
$description['baseUrl'] = $base_uri;
$this->description[$base_uri] = new Description($description);
}
return $this->description[$base_uri];
}
/**
* Get File Locator.
*
* @return \Symfony\Component\Config\FileLocator
* The file locator used to find the service descriptions.
*/
protected function getFileLocator() {
if (empty($this->fileLocator)) {
$this
->initFileLocator();
}
return $this->fileLocator;
}
/**
* Set File Locator.
*/
protected function initFileLocator() {
$source = $this
->getApiSourceInfo();
$this->fileLocator = new FileLocator($source['dirname']);
}
/**
* Get File Loader.
*
* @return \Guzzle\Service\Loader\FileLoader
* The file loader used to load the service descriptions.
*/
protected function getFileLoader() {
if (empty($this->fileLoader)) {
$this
->initFileLoader();
}
return $this->fileLoader;
}
/**
* Set File Loader.
*/
protected function initFileLoader() {
$source = $this
->getApiSourceInfo();
$locator = $this
->getFileLocator();
switch ($source['extension']) {
case 'json':
$loader = new JsonLoader($locator);
break;
case 'yml':
$loader = new YamlLoader($locator);
break;
case 'php':
$loader = new PhpLoader($locator);
break;
default:
$allowed_extensions = [
'json',
'yml',
'php',
];
$message = sprintf('Invalid HTTP Services Api source provided: "%s". ', $source['filename']);
$message .= sprintf('File extension must be one of %s.', implode(', ', $allowed_extensions));
throw new \RuntimeException($message);
}
$this->fileLoader = $loader;
}
/**
* {@inheritdoc}
*/
public function getCommands() {
if (!empty($this->commands)) {
return $this->commands;
}
$description = $this
->getClient()
->getDescription();
$command_names = array_keys($description
->getOperations());
$this->commands = [];
foreach ($command_names as $command_name) {
$this->commands[$command_name] = $description
->getOperation($command_name);
}
return $this->commands;
}
/**
* {@inheritdoc}
*/
public function getCommand($commandName) {
if (!empty($this->commands[$commandName])) {
return $this->commands[$commandName];
}
return $this
->getClient()
->getDescription()
->getOperation($commandName);
}
/**
* {@inheritdoc}
*/
public function call($commandName, array $params = []) {
$client = $this
->getClientByCommand($commandName);
$command = $client
->getCommand($commandName, $params);
return $client
->execute($command);
}
/**
* Magic method implementation for commands execution.
*
* @param string $name
* The Guzzle command name.
* @param array $arguments
* The Guzzle command parameters array.
*
* @return \GuzzleHttp\Command\ResultInterface|mixed
* The Guzzle Command execution result.
*
* @see HttpClientInterface::call
*/
public function __call($name, array $arguments = []) {
$params = !empty($arguments[0]) ? $arguments[0] : [];
return $this
->call($name, $params);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HttpClient:: |
protected | property | An array containing the Http Service Api description. | |
HttpClient:: |
protected | property | The Http Service Api Handler service. | |
HttpClient:: |
protected | property | An array containing api source path info. | |
HttpClient:: |
protected | property | Guzzle Client definition. | |
HttpClient:: |
protected | property | An array containing all the Guzzle commands. | |
HttpClient:: |
protected | property | Description definition. | |
HttpClient:: |
protected | property | Event dispatcher. | |
HttpClient:: |
protected | property | The file loader used to load the service descriptions. | |
HttpClient:: |
protected | property | The file locator used to find the service descriptions. | |
HttpClient:: |
protected | property | The name of the service api of this http client instance. | |
HttpClient:: |
public | function |
Execute command call. Overrides HttpClientInterface:: |
|
HttpClient:: |
private | function | Create a new Guzzle Client by config. | |
HttpClient:: |
public | function |
Get Http Service Api data. Overrides HttpClientInterface:: |
|
HttpClient:: |
protected | function | Get Api source path info. | |
HttpClient:: |
protected | function | Get Client. | |
HttpClient:: |
protected | function | Get Client by Command. | |
HttpClient:: |
public | function | ||
HttpClient:: |
public | function |
Get single service api command by name. Overrides HttpClientInterface:: |
|
HttpClient:: |
public | function |
Get service api commands. Overrides HttpClientInterface:: |
|
HttpClient:: |
protected | function | Get File Loader. | |
HttpClient:: |
protected | function | Get File Locator. | |
HttpClient:: |
protected | function | Set File Loader. | |
HttpClient:: |
protected | function | Set File Locator. | |
HttpClient:: |
protected | function | ||
HttpClient:: |
protected | function | Set Api source path info. | |
HttpClient:: |
private | function | Setup Guzzle Client from *.http_services_api.yml files. | |
HttpClient:: |
public | function | Magic method implementation for commands execution. | |
HttpClient:: |
public | function | Constructs an HttpClient object. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |