Skip to content

Commit

Permalink
use psr/http-factory instead of php-http/message-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
nikow13 committed Nov 26, 2024
1 parent 8bdbcd0 commit 5ab847d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"allow-plugins": {
"php-http/discovery": true
}
},
"extra": {
Expand Down
28 changes: 18 additions & 10 deletions src/Rest/RestApiBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Ubirak\RestApiBehatExtension\Rest;

use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\Psr17Factory;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Tolerance\Operation\Callback;
Expand All @@ -15,7 +16,7 @@

class RestApiBrowser
{
/** @var HttpClient */
/** @var ClientInterface */
private $httpClient;

/** @var RequestInterface */
Expand All @@ -33,23 +34,23 @@ class RestApiBrowser
/** @var string */
private $host;

/** @var MessageFactoryDiscovery */
/** @var RequestFactoryInterface */
private $messageFactory;

/**
* @param string $host
*/
public function __construct($host, HttpClient $httpClient = null)
public function __construct($host, ClientInterface $httpClient = null)
{
$this->host = $host;
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->messageFactory = MessageFactoryDiscovery::find();
$this->httpClient = $httpClient ?: Psr18ClientDiscovery::find();
$this->messageFactory = new Psr17Factory();
}

/**
* Allow to override the httpClient to use yours with specific middleware for example.
*/
public function useHttpClient(HttpClient $httpClient)
public function useHttpClient(ClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
Expand Down Expand Up @@ -105,7 +106,14 @@ public function sendRequest($method, $uri, $body = null)
$this->setRequestHeader('Content-Type', $html->getContentTypeHeaderValue());
}

$this->request = $this->messageFactory->createRequest($method, $uri, $this->requestHeaders, $body);
$this->request = $this->messageFactory->createRequest($method, $uri);
foreach ($this->requestHeaders as $keyHeader => $valueHeader) {
$this->request = $this->request->withHeader($keyHeader, $valueHeader);
}
if (null !== $body) {
$this->request = $this->request->withBody($this->messageFactory->createStream($body));
}

$this->response = $this->httpClient->sendRequest($this->request);
$this->requestHeaders = [];

Expand Down

0 comments on commit 5ab847d

Please sign in to comment.