class HttpClient in HTTP Client Manager 8
Same name and namespace in other branches
- 8.2 src/HttpClient.php \Drupal\http_client_manager\HttpClient
Hierarchy
- class \Drupal\http_client_manager\HttpClient implements HttpClientInterface
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 10
Namespace
Drupal\http_client_managerView source
class HttpClient implements HttpClientInterface {
/**
* The name of the service api of this http client instance.
*
* @var string
*/
protected $serviceApi;
/**
* The Http Service Api Handler service.
*
* @var HttpServiceApiHandler
*/
protected $apiHandler;
/**
* An array containing the Http Service Api description.
*
* @var array
*/
protected $api;
/**
* Guzzle Client definition.
*
* @var \Guzzle\Service\Client
*/
protected $client;
/**
* Event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* 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 Client.
*
* @return \Guzzle\Service\Client
* The Configured Guzzle client instance.
*/
protected function getClient() {
if (empty($this->client)) {
$this
->setupGuzzleClient();
}
return $this->client;
}
/**
* Setup Guzzle Client from *.http_services_api.yml files.
*/
private function setupGuzzleClient() {
$api = $this
->getApi();
$this->client = new Client($api['base_url'], $api['config']);
$this->client
->setDescription(ServiceDescription::factory($api['source']));
$this->client
->setEventDispatcher($this->eventDispatcher);
}
/**
* {@inheritdoc}
*/
public function getCommands() {
return $this
->getClient()
->getDescription()
->getOperations();
}
/**
* {@inheritdoc}
*/
public function getCommand($commandName) {
return $this
->getClient()
->getCommand($commandName)
->getOperation();
}
/**
* {@inheritdoc}
*/
public function call($commandName, array $params = []) {
return $this
->getClient()
->getCommand($commandName, $params)
->execute();
}
/**
* Magic method implementation for commands execution.
*
* @param string $name
* The Guzzle command name.
* @param array $arguments
* The Guzzle command parameters array.
*
* @return \Guzzle\Service\Resource\Model|array
* 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 | Guzzle Client definition. | |
HttpClient:: |
protected | property | Event dispatcher. | |
HttpClient:: |
protected | property | The name of the service api of this http client instance. | |
HttpClient:: |
public | function |
Execute command call. Overrides HttpClientInterface:: |
|
HttpClient:: |
public | function |
Get Http Service Api data. Overrides HttpClientInterface:: |
|
HttpClient:: |
protected | function | Get Client. | |
HttpClient:: |
public | function |
Get single service api command by name. Overrides HttpClientInterface:: |
|
HttpClient:: |
public | function |
Get service api commands. Overrides HttpClientInterface:: |
|
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 | |
HttpClientInterface:: |
constant | Header name definition for Service Api. | ||
HttpClientInterface:: |
constant | Header name definition for Command name. |