class LingotekHttp in Lingotek Translation 3.4.x
Same name and namespace in other branches
- 8 src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 8.2 src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 4.0.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 3.0.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 3.1.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 3.2.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 3.3.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 3.5.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 3.6.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 3.7.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
- 3.8.x src/Remote/LingotekHttp.php \Drupal\lingotek\Remote\LingotekHttp
Lingotek HTTP implementation using Guzzle.
Hierarchy
- class \Drupal\lingotek\Remote\LingotekHttp implements LingotekHttpInterface
Expanded class hierarchy of LingotekHttp
1 file declares its use of LingotekHttp
- LingotekHttpUnitTest.php in tests/
src/ Unit/ Remote/ LingotekHttpUnitTest.php
1 string reference to 'LingotekHttp'
1 service uses LingotekHttp
File
- src/
Remote/ LingotekHttp.php, line 13
Namespace
Drupal\lingotek\RemoteView 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'));
}
/**
* {@inheritdoc}
*/
public function get($path, $args = []) {
$options = [];
if (count($args)) {
$options = [
RequestOptions::QUERY => $args,
];
}
return $this->httpClient
->get($this
->getBaseUrl() . $path, [
RequestOptions::HEADERS => $this
->getDefaultHeaders(),
] + $options);
}
/**
* {@inheritdoc}
*/
public function post($path, $args = [], $use_multipart = FALSE) {
$options = [];
if (count($args) && $use_multipart) {
$multipart = [];
foreach ($args as $name => $contents) {
if (is_array($contents)) {
foreach ($contents as $content) {
$multipart[] = [
'name' => $name,
'contents' => $content,
];
}
}
else {
$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);
}
/**
* {@inheritdoc}
*/
public function delete($path, $args = []) {
// 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);
}
/**
* {@inheritdoc}
*/
public function patch($path, $args = [], $use_multipart = FALSE) {
$options = [];
if (count($args) && $use_multipart) {
$multipart = [];
foreach ($args as $name => $contents) {
if (is_array($contents)) {
foreach ($contents as $content) {
$multipart[] = [
'name' => $name,
'contents' => $content,
];
}
}
else {
$multipart[] = [
'name' => $name,
'contents' => $contents,
];
}
}
$options[RequestOptions::MULTIPART] = $multipart;
}
elseif (count($args) && !$use_multipart) {
$options[RequestOptions::FORM_PARAMS] = $args;
}
return $this->httpClient
->patch($this
->getBaseUrl() . $path, [
RequestOptions::HEADERS => $this
->getDefaultHeaders() + [
'X-HTTP-Method-Override' => 'PATCH',
],
] + $options);
}
/**
* {@inheritdoc}
*/
public function getCurrentToken() {
return $this->config
->get('account.access_token');
}
/**
* Get the headers that are used in every request.
*
* @return string[]
*/
protected function getDefaultHeaders() {
$headers = [
'Accept' => '*/*',
];
if ($token = $this
->getCurrentToken()) {
$headers['Authorization'] = 'bearer ' . $token;
}
return $headers;
}
/**
* Gets the API base url.
*
* @return string
* The API base url.
*/
protected function getBaseUrl() {
$base_url = $this->config
->get('account.host');
return $base_url;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LingotekHttp:: |
protected | property | The HTTP client to interact with the Lingotek service. | |
LingotekHttp:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
LingotekHttp:: |
public | function |
Send a DELETE request. Overrides LingotekHttpInterface:: |
|
LingotekHttp:: |
public | function |
Send a GET request. Overrides LingotekHttpInterface:: |
|
LingotekHttp:: |
protected | function | Gets the API base url. | |
LingotekHttp:: |
public | function |
Gets the current configured token. Overrides LingotekHttpInterface:: |
|
LingotekHttp:: |
protected | function | Get the headers that are used in every request. | |
LingotekHttp:: |
public | function |
Send a PATCH request. Overrides LingotekHttpInterface:: |
|
LingotekHttp:: |
public | function |
Send a POST request. Overrides LingotekHttpInterface:: |
|
LingotekHttp:: |
public | function |