Skip to content

Commit

Permalink
Merge ad69991 into a35aa7f
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubpilaralmamedia committed Jan 15, 2024
2 parents a35aa7f + ad69991 commit 4cad1cd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
## Unreleased
### Changed
- Require PHP ^7.3
### Added
- Replace abandoned package php-http/message-factory. Using psr/http-factory instead.

## 4.1.0 - 2021-08-19
### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"php-http/discovery": "^1.0",
"php-http/httplug": "^1.1 || ^2.0",
"php-http/message": "^1.6",
"php-http/message-factory": "^1.0",
"php-http/promise": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"ramsey/uuid": "^3.7 || ^4.0"
},
Expand Down
58 changes: 37 additions & 21 deletions src/Http/RequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\Plugin\HeaderSetPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MessageFactory;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Lmc\Matej\Http\Plugin\ExceptionPlugin;
use Lmc\Matej\Matej;
use Lmc\Matej\Model\Request;
use Lmc\Matej\Model\Response;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* Encapsulates HTTP layer, ie. request/response handling.
Expand All @@ -31,12 +32,14 @@ class RequestManager
protected $accountId;
/** @var string */
protected $apiKey;
/** @var HttpClient */
/** @var ClientInterface */
protected $httpClient;
/** @var MessageFactory */
/** @var RequestFactoryInterface */
protected $messageFactory;
/** @var ResponseDecoderInterface */
protected $responseDecoder;
/** @var StreamFactoryInterface */
protected $streamFactory;

public function __construct(string $accountId, string $apiKey)
{
Expand All @@ -56,13 +59,13 @@ public function sendRequest(Request $request): Response
}

/** @codeCoverageIgnore */
public function setHttpClient(HttpClient $httpClient): void
public function setHttpClient(ClientInterface $httpClient): void
{
$this->httpClient = $httpClient;
}

/** @codeCoverageIgnore */
public function setMessageFactory(MessageFactory $messageFactory): void
public function setMessageFactory(RequestFactoryInterface $messageFactory): void
{
$this->messageFactory = $messageFactory;
}
Expand All @@ -73,27 +76,33 @@ public function setResponseDecoder(ResponseDecoderInterface $responseDecoder): v
$this->responseDecoder = $responseDecoder;
}

/** @codeCoverageIgnore */
public function setStreamFactory(StreamFactoryInterface $streamFactory): void
{
$this->streamFactory = $streamFactory;
}

/** @codeCoverageIgnore */
public function setBaseUrl(string $baseUrl): void
{
$this->baseUrl = $baseUrl;
}

protected function getHttpClient(): HttpClient
protected function getHttpClient(): ClientInterface
{
if ($this->httpClient === null) {
// @codeCoverageIgnoreStart
$this->httpClient = HttpClientDiscovery::find();
$this->httpClient = Psr18ClientDiscovery::find();
// @codeCoverageIgnoreEnd
}

return $this->httpClient;
}

protected function getMessageFactory(): MessageFactory
protected function getMessageFactory(): RequestFactoryInterface
{
if ($this->messageFactory === null) {
$this->messageFactory = MessageFactoryDiscovery::find();
$this->messageFactory = Psr17FactoryDiscovery::findRequestFactory();
}

return $this->messageFactory;
Expand All @@ -108,7 +117,16 @@ protected function getResponseDecoder(): ResponseDecoderInterface
return $this->responseDecoder;
}

protected function createConfiguredHttpClient(): HttpClient
protected function getStreamFactory(): StreamFactoryInterface
{
if ($this->streamFactory === null) {
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
}

return $this->streamFactory;
}

protected function createConfiguredHttpClient(): ClientInterface
{
return new PluginClient(
$this->getHttpClient(),
Expand All @@ -122,19 +140,17 @@ protected function createConfiguredHttpClient(): HttpClient

protected function createHttpRequestFromMatejRequest(Request $request): RequestInterface
{
$requestBody = json_encode($request->getData()); // TODO: use \Safe\json_encode
$requestBody = $this->getStreamFactory()->createStream(json_encode($request->getData())); // TODO: use \Safe\json_encode
$uri = $this->buildBaseUrl() . $request->getPath();

return $this->getMessageFactory()
->createRequest(
$request->getMethod(),
$uri,
[
'Content-Type' => 'application/json',
static::REQUEST_ID_HEADER => $request->getRequestId(),
],
$requestBody
);
$uri
)
->withHeader('Content-Type', 'application/json')
->withHeader(static::REQUEST_ID_HEADER, $request->getRequestId())
->withBody($requestBody);
}

protected function buildBaseUrl(): string
Expand Down
20 changes: 16 additions & 4 deletions src/Matej.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Lmc\Matej;

use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Psr\Http\Client\ClientInterface;
use Lmc\Matej\Http\RequestManager;
use Lmc\Matej\Http\ResponseDecoderInterface;
use Lmc\Matej\RequestBuilder\RequestBuilderFactory;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

class Matej
{
Expand All @@ -27,7 +28,7 @@ public function request(): RequestBuilderFactory
}

/** @return $this */
public function setHttpClient(HttpClient $client): self
public function setHttpClient(ClientInterface $client): self
{
$this->getRequestManager()->setHttpClient($client);

Expand All @@ -49,7 +50,7 @@ public function setBaseUrl(string $baseUrl): self
* @codeCoverageIgnore
* @return $this
*/
public function setHttpMessageFactory(MessageFactory $messageFactory): self
public function setHttpMessageFactory(RequestFactoryInterface $messageFactory): self
{
$this->getRequestManager()->setMessageFactory($messageFactory);

Expand All @@ -67,6 +68,17 @@ public function setHttpResponseDecoder(ResponseDecoderInterface $responseDecoder
return $this;
}

/**
* @codeCoverageIgnore
* @return $this
*/
public function setStreamFactory(StreamFactoryInterface $streamFactory): self
{
$this->getRequestManager()->setStreamFactory($streamFactory);

return $this;
}

protected function getRequestManager(): RequestManager
{
return $this->requestManager;
Expand Down

0 comments on commit 4cad1cd

Please sign in to comment.