RemoteManager.php in Entity Share 8
File
modules/entity_share_client/src/Service/RemoteManager.php
View source
<?php
namespace Drupal\entity_share_client\Service;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Http\ClientFactory;
use Drupal\entity_share_client\Entity\RemoteInterface;
class RemoteManager implements RemoteManagerInterface {
protected $httpClientFactory;
public function __construct(ClientFactory $http_client_factory) {
$this->httpClientFactory = $http_client_factory;
}
public function prepareClient(RemoteInterface $remote) {
$http_client = $this->httpClientFactory
->fromOptions([
'base_uri' => $remote
->get('url'),
'cookies' => TRUE,
'allow_redirects' => TRUE,
]);
$http_client
->post('/user/login', [
'form_params' => [
'name' => $remote
->get('basic_auth_username'),
'pass' => $remote
->get('basic_auth_password'),
'form_id' => 'user_login_form',
],
]);
return $http_client;
}
public function prepareJsonApiClient(RemoteInterface $remote) {
return $this->httpClientFactory
->fromOptions([
'base_uri' => $remote
->get('url'),
'auth' => [
$remote
->get('basic_auth_username'),
$remote
->get('basic_auth_password'),
],
'headers' => [
'Content-type' => 'application/vnd.api+json',
],
]);
}
public function getChannelsInfos(RemoteInterface $remote) {
$http_client = $this
->prepareJsonApiClient($remote);
$json_response = $http_client
->get('entity_share')
->getBody()
->getContents();
$json = Json::decode($json_response);
return $json['data']['channels'];
}
}