Skip to content

Commit

Permalink
Http: add support for PSR17/PSR18 and drop usage of deprecated client…
Browse files Browse the repository at this point in the history
… discovery (#363)
  • Loading branch information
glaubinix authored Jan 18, 2024
1 parent 4bd51e3 commit b7ea190
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@
"psr/http-message": "^1.0"
},
"require-dev": {
"guzzlehttp/psr7": "^1.7.0",
"nyholm/psr7": "^1.6.1",
"php-http/mock-client": "^1.4",
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.3",
"squizlabs/php_codesniffer": "^3.1"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": false
}
}
}
72 changes: 49 additions & 23 deletions src/IntercomClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Message\Authentication;
use Http\Message\Authentication\BasicAuth;
use Http\Message\Authentication\Bearer;
use Http\Message\RequestFactory;
use Http\Message\UriFactory;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use stdClass;

Expand All @@ -24,20 +24,25 @@ class IntercomClient
const SDK_VERSION = '4.4.0';

/**
* @var HttpClient $httpClient
* @var ClientInterface $httpClient
*/
private $httpClient;

/**
* @var RequestFactory $requestFactory
* @var RequestFactoryInterface $requestFactory
*/
private $requestFactory;

/**
* @var UriFactory $uriFactory
* @var UriFactoryInterface $uriFactory
*/
private $uriFactory;

/**
* @var StreamFactoryInterface $streamFactory
*/
private $streamFactory;

/**
* @var string API user authentication
*/
Expand Down Expand Up @@ -163,40 +168,51 @@ public function __construct(string $appIdOrToken, string $password = null, array
$this->extraRequestHeaders = $extraRequestHeaders;

$this->httpClient = $this->getDefaultHttpClient();
$this->requestFactory = MessageFactoryDiscovery::find();
$this->uriFactory = UriFactoryDiscovery::find();
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
$this->uriFactory = Psr17FactoryDiscovery::findUriFactory();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
}

/**
* Sets the HTTP client.
*
* @param HttpClient $httpClient
* @param ClientInterface $httpClient
*/
public function setHttpClient(HttpClient $httpClient)
public function setHttpClient(ClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}

/**
* Sets the request factory.
*
* @param RequestFactory $requestFactory
* @param RequestFactoryInterface $requestFactory
*/
public function setRequestFactory(RequestFactory $requestFactory)
public function setRequestFactory(RequestFactoryInterface $requestFactory)
{
$this->requestFactory = $requestFactory;
}

/**
* Sets the URI factory.
*
* @param UriFactory $uriFactory
* @param UriFactoryInterface $uriFactory
*/
public function setUriFactory(UriFactory $uriFactory)
public function setUriFactory(UriFactoryInterface $uriFactory)
{
$this->uriFactory = $uriFactory;
}

/**
* Sets the stream factory.
*
* @param StreamFactoryInterface $streamFactory
*/
public function setStreamFactory(StreamFactoryInterface $streamFactory)
{
$this->streamFactory = $streamFactory;
}

/**
* Sends POST request to Intercom API.
*
Expand Down Expand Up @@ -310,12 +326,12 @@ public function getRateLimitDetails()
}

/**
* @return HttpClient
* @return ClientInterface
*/
private function getDefaultHttpClient()
{
return new PluginClient(
HttpClientDiscovery::find(),
Psr18ClientDiscovery::find(),
[new ErrorPlugin()]
);
}
Expand Down Expand Up @@ -372,11 +388,21 @@ private function authenticateRequest(RequestInterface $request)
*/
private function sendRequest($method, $uri, $body = null)
{
$headers = $this->getRequestHeaders();
$body = is_array($body) ? json_encode($body) : $body;
$request = $this->authenticateRequest(
$this->requestFactory->createRequest($method, $uri, $headers, $body)
);
$request = $this->requestFactory
->createRequest($method, $uri);

if ($body !== null) {
$request = $request
->withBody($this->streamFactory->createStream($body));
}

foreach ($this->getRequestHeaders() as $name => $value) {
$request = $request
->withHeader($name, $value);
}

$request = $this->authenticateRequest($request);

return $this->httpClient->sendRequest($request);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/IntercomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
namespace Intercom\Test;

use DateTimeImmutable;
use GuzzleHttp\Psr7\Response;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\Exception;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Discovery\Strategy\MockClientStrategy;
use Http\Mock\Client;
use Intercom\IntercomClient;
use Nyholm\Psr7\Response;
use stdClass;

class IntercomClientTest extends TestCase
{
protected function setUp(): void
{
HttpClientDiscovery::prependStrategy(MockClientStrategy::class);
Psr18ClientDiscovery::prependStrategy(MockClientStrategy::class);
}

public function testBasicClient()
Expand Down

0 comments on commit b7ea190

Please sign in to comment.