Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Opensaucesystems\Lxd;

use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Opensaucesystems\Lxd\Exception\InvalidEndpointException;
use Opensaucesystems\Lxd\Exception\ClientConnectionException;
use Opensaucesystems\Lxd\Exception\ServerException;
Expand All @@ -11,11 +13,8 @@
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Message\MessageFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

class Client
{
Expand All @@ -32,7 +31,7 @@ class Client
/**
* The object that sends HTTP messages
*
* @var HttpClient
* @var ClientInterface
*/
private $httpClient;

Expand All @@ -44,9 +43,9 @@ class Client
private $pluginClient;

/**
* @var MessageFactory
* @var RequestFactoryInterface
*/
private $messageFactory;
private $requestFactory;

/**
* @var Plugin[]
Expand All @@ -59,14 +58,19 @@ class Client
* @var bool
*/
private $httpClientModified = true;
/**
* @var mixed|\Psr\Http\Message\StreamFactoryInterface
*/
private mixed $streamFactory;

/**
/**
* Create a new lxd client Instance
*/
public function __construct(HttpClient $httpClient = null, $apiVersion = null, $url = null)
public function __construct(?ClientInterface $httpClient = null, $apiVersion = null, $url = null)
{
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->messageFactory = MessageFactoryDiscovery::find();
$this->httpClient = $httpClient ?: Psr18ClientDiscovery::find();
$this->requestFactory = Psr17FactoryDiscovery::findServerRequestFactory();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$this->apiVersion = $apiVersion ?: '1.0';
$this->url = $url ?: 'https://127.0.0.1:8443';

Expand Down Expand Up @@ -96,7 +100,7 @@ public function setUrl($url)
$this->removePlugin(PathPrepend::class);
$this->removePlugin(PathTrimEnd::class);

$this->addPlugin(new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri($this->url)));
$this->addPlugin(new Plugin\AddHostPlugin(Psr17FactoryDiscovery::findUriFactory()->createUri($this->url)));
$this->addPlugin(new PathPrepend(sprintf('/%s', $this->getApiVersion())));
$this->addPlugin(new PathTrimEnd());
}
Expand Down Expand Up @@ -137,16 +141,17 @@ public function getHttpClient()

$this->pluginClient = new HttpMethodsClient(
new PluginClient($this->httpClient, $this->plugins),
$this->messageFactory
$this->requestFactory,
$this->streamFactory
);
}
return $this->pluginClient;
}

/**
* @param HttpClient $httpClient
* @param ClientInterface $httpClient
*/
public function setHttpClient(HttpClient $httpClient)
public function setHttpClient(ClientInterface $httpClient)
{
$this->httpClientModified = true;
$this->httpClient = $httpClient;
Expand Down