You are here

class HttpClient in HTTP Client Manager 8

Same name and namespace in other branches
  1. 8.2 src/HttpClient.php \Drupal\http_client_manager\HttpClient

Hierarchy

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'
http_client_manager.services.yml in ./http_client_manager.services.yml
http_client_manager.services.yml
1 service uses HttpClient
http_client_manager.client_base in ./http_client_manager.services.yml
Drupal\http_client_manager\HttpClient

File

src/HttpClient.php, line 10

Namespace

Drupal\http_client_manager
View 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

Namesort descending Modifiers Type Description Overrides
HttpClient::$api protected property An array containing the Http Service Api description.
HttpClient::$apiHandler protected property The Http Service Api Handler service.
HttpClient::$client protected property Guzzle Client definition.
HttpClient::$eventDispatcher protected property Event dispatcher.
HttpClient::$serviceApi protected property The name of the service api of this http client instance.
HttpClient::call public function Execute command call. Overrides HttpClientInterface::call
HttpClient::getApi public function Get Http Service Api data. Overrides HttpClientInterface::getApi
HttpClient::getClient protected function Get Client.
HttpClient::getCommand public function Get single service api command by name. Overrides HttpClientInterface::getCommand
HttpClient::getCommands public function Get service api commands. Overrides HttpClientInterface::getCommands
HttpClient::setupGuzzleClient private function Setup Guzzle Client from *.http_services_api.yml files.
HttpClient::__call public function Magic method implementation for commands execution.
HttpClient::__construct public function Constructs an HttpClient object
HttpClientInterface::HEADER_API constant Header name definition for Service Api.
HttpClientInterface::HEADER_COMMAND constant Header name definition for Command name.