You are here

class LingotekHttp in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  2. 4.0.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  3. 3.0.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  4. 3.1.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  5. 3.2.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  6. 3.3.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  7. 3.4.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  8. 3.5.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  9. 3.6.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  10. 3.7.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
  11. 3.8.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp

Hierarchy

Expanded class hierarchy of LingotekHttp

1 file declares its use of LingotekHttp
LingotekApiUnitTest.php in tests/src/Unit/Remote/LingotekApiUnitTest.php
1 string reference to 'LingotekHttp'
lingotek.services.yml in ./lingotek.services.yml
lingotek.services.yml
1 service uses LingotekHttp
lingotek.http_client in ./lingotek.services.yml
Drupal\lingotek\Remote\LingotekHttp

File

src/Remote/LingotekHttp.php, line 13

Namespace

Drupal\lingotek\Remote
View source
class LingotekHttp implements LingotekHttpInterface {

  /**
   * The HTTP client to interact with the Lingotek service.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The Guzzle HTTP client.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory) {
    $this->httpClient = $http_client;
    $this->config = $config_factory
      ->get('lingotek.settings');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('http_client'), $container
      ->get('config.factory'));
  }

  /*
   * send a GET request
   */
  public function get($path, $args = array()) {
    $options = [];
    if (count($args)) {
      $options = [
        RequestOptions::QUERY => $args,
      ];
    }
    return $this->httpClient
      ->get($this
      ->getBaseUrl() . $path, [
      RequestOptions::HEADERS => $this
        ->getDefaultHeaders(),
    ] + $options);
  }

  /*
   * send a POST request
   */
  public function post($path, $args = array(), $use_multipart = FALSE) {
    $options = [];
    if (count($args) && $use_multipart) {
      $multipart = [];
      foreach ($args as $name => $contents) {
        $multipart[] = [
          'name' => $name,
          'contents' => $contents,
        ];
      }
      $options[RequestOptions::MULTIPART] = $multipart;
    }
    elseif (count($args) && !$use_multipart) {
      $options[RequestOptions::FORM_PARAMS] = $args;
    }
    return $this->httpClient
      ->post($this
      ->getBaseUrl() . $path, [
      RequestOptions::HEADERS => $this
        ->getDefaultHeaders(),
    ] + $options);
  }

  /*
   * send a DELETE request
   */
  public function delete($path, $args = array()) {

    // Let the post method masquerade as a DELETE
    $options = [];
    if (count($args)) {
      $options = [
        RequestOptions::QUERY => $args,
      ];
    }
    return $this->httpClient
      ->delete($this
      ->getBaseUrl() . $path, [
      RequestOptions::HEADERS => $this
        ->getDefaultHeaders() + [
        'X-HTTP-Method-Override' => 'DELETE',
      ],
    ] + $options);
  }

  /*
   * send a PATCH request
   */
  public function patch($path, $args = array(), $use_multipart = FALSE) {
    return $this->httpClient
      ->patch($this
      ->getBaseUrl() . $path, [
      RequestOptions::FORM_PARAMS => $args,
      RequestOptions::HEADERS => $this
        ->getDefaultHeaders() + [
        'X-HTTP-Method-Override' => 'PATCH',
      ],
    ]);
  }
  public function getCurrentToken() {
    return $this->config
      ->get('account.access_token');
  }
  protected function getDefaultHeaders() {
    $headers = [
      'Accept' => '*/*',
    ];
    if ($token = $this->config
      ->get('account.access_token')) {
      $headers['Authorization'] = 'bearer ' . $token;
    }
    return $headers;
  }
  protected function getBaseUrl() {
    $base_url = $this->config
      ->get('account.sandbox_host');
    if ($this->config
      ->get('account.use_production')) {
      $base_url = $this->config
        ->get('account.host');
    }
    return $base_url;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekHttp::$httpClient protected property The HTTP client to interact with the Lingotek service.
LingotekHttp::create public static function Overrides LingotekHttpInterface::create
LingotekHttp::delete public function Overrides LingotekHttpInterface::delete
LingotekHttp::get public function Overrides LingotekHttpInterface::get
LingotekHttp::getBaseUrl protected function
LingotekHttp::getCurrentToken public function Overrides LingotekHttpInterface::getCurrentToken
LingotekHttp::getDefaultHeaders protected function
LingotekHttp::patch public function Overrides LingotekHttpInterface::patch
LingotekHttp::post public function Overrides LingotekHttpInterface::post
LingotekHttp::__construct public function