View source
<?php
namespace Drupal\lingotek\Remote;
use Drupal\Core\Config\ConfigFactoryInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LingotekHttp implements LingotekHttpInterface {
protected $httpClient;
public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory) {
$this->httpClient = $http_client;
$this->config = $config_factory
->get('lingotek.settings');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('http_client'), $container
->get('config.factory'));
}
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);
}
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);
}
public function delete($path, $args = []) {
$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);
}
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);
}
public function getCurrentToken() {
return $this->config
->get('account.access_token');
}
protected function getDefaultHeaders() {
$headers = [
'Accept' => '*/*',
];
if ($token = $this
->getCurrentToken()) {
$headers['Authorization'] = 'bearer ' . $token;
}
return $headers;
}
protected function getBaseUrl() {
$base_url = $this->config
->get('account.host');
return $base_url;
}
}