Skip to content

Commit

Permalink
Replace HTTP Factory class
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubpilaralmamedia committed Jan 4, 2024
1 parent 0b55739 commit 110f083
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<!-- There is always Unreleased section on the top. Subsections (Added, Changed, Fixed, Removed) should be added as needed. -->

## Unreleased
### Added
- Fix: Package php-http/message-factory is abandoned, you should avoid using it. Use psr/http-factory instead.
- Add support for lmc/coding-standard ^3.0

## 4.1.0 - 2021-08-19
### Added
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
"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"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.4",
"lmc/coding-standard": "^1.3 || ^2.0",
"http-interop/http-factory-guzzle": "^1.2",
"lmc/coding-standard": "^1.3 || ^2.0 || ^3.0",
"php-coveralls/php-coveralls": "^2.4",
"php-http/guzzle6-adapter": "^1.1.1 || ^2.0",
"php-http/mock-client": "^1.0",
Expand Down Expand Up @@ -57,6 +58,7 @@
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"ergebnis/composer-normalize": true,
"php-http/discovery": true,
"phpstan/extension-installer": true
},
"sort-packages": true
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 110f083

Please sign in to comment.