Skip to content

Commit b7ea190

Browse files
authored
Http: add support for PSR17/PSR18 and drop usage of deprecated client discovery (#363)
1 parent 4bd51e3 commit b7ea190

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@
4343
"psr/http-message": "^1.0"
4444
},
4545
"require-dev": {
46-
"guzzlehttp/psr7": "^1.7.0",
46+
"nyholm/psr7": "^1.6.1",
4747
"php-http/mock-client": "^1.4",
4848
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.3",
4949
"squizlabs/php_codesniffer": "^3.1"
5050
},
5151
"config": {
52-
"sort-packages": true
52+
"sort-packages": true,
53+
"allow-plugins": {
54+
"php-http/discovery": false
55+
}
5356
}
5457
}

src/IntercomClient.php

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
use Http\Client\Common\Plugin\ErrorPlugin;
66
use Http\Client\Common\PluginClient;
7-
use Http\Client\HttpClient;
8-
use Http\Discovery\HttpClientDiscovery;
9-
use Http\Discovery\MessageFactoryDiscovery;
10-
use Http\Discovery\UriFactoryDiscovery;
7+
use Http\Discovery\Psr17FactoryDiscovery;
8+
use Http\Discovery\Psr18ClientDiscovery;
119
use Http\Message\Authentication;
1210
use Http\Message\Authentication\BasicAuth;
1311
use Http\Message\Authentication\Bearer;
14-
use Http\Message\RequestFactory;
15-
use Http\Message\UriFactory;
1612
use Psr\Http\Client\ClientExceptionInterface;
13+
use Psr\Http\Client\ClientInterface;
14+
use Psr\Http\Message\RequestFactoryInterface;
1715
use Psr\Http\Message\RequestInterface;
1816
use Psr\Http\Message\ResponseInterface;
17+
use Psr\Http\Message\StreamFactoryInterface;
18+
use Psr\Http\Message\UriFactoryInterface;
1919
use Psr\Http\Message\UriInterface;
2020
use stdClass;
2121

@@ -24,20 +24,25 @@ class IntercomClient
2424
const SDK_VERSION = '4.4.0';
2525

2626
/**
27-
* @var HttpClient $httpClient
27+
* @var ClientInterface $httpClient
2828
*/
2929
private $httpClient;
3030

3131
/**
32-
* @var RequestFactory $requestFactory
32+
* @var RequestFactoryInterface $requestFactory
3333
*/
3434
private $requestFactory;
3535

3636
/**
37-
* @var UriFactory $uriFactory
37+
* @var UriFactoryInterface $uriFactory
3838
*/
3939
private $uriFactory;
4040

41+
/**
42+
* @var StreamFactoryInterface $streamFactory
43+
*/
44+
private $streamFactory;
45+
4146
/**
4247
* @var string API user authentication
4348
*/
@@ -163,40 +168,51 @@ public function __construct(string $appIdOrToken, string $password = null, array
163168
$this->extraRequestHeaders = $extraRequestHeaders;
164169

165170
$this->httpClient = $this->getDefaultHttpClient();
166-
$this->requestFactory = MessageFactoryDiscovery::find();
167-
$this->uriFactory = UriFactoryDiscovery::find();
171+
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
172+
$this->uriFactory = Psr17FactoryDiscovery::findUriFactory();
173+
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
168174
}
169175

170176
/**
171177
* Sets the HTTP client.
172178
*
173-
* @param HttpClient $httpClient
179+
* @param ClientInterface $httpClient
174180
*/
175-
public function setHttpClient(HttpClient $httpClient)
181+
public function setHttpClient(ClientInterface $httpClient)
176182
{
177183
$this->httpClient = $httpClient;
178184
}
179185

180186
/**
181187
* Sets the request factory.
182188
*
183-
* @param RequestFactory $requestFactory
189+
* @param RequestFactoryInterface $requestFactory
184190
*/
185-
public function setRequestFactory(RequestFactory $requestFactory)
191+
public function setRequestFactory(RequestFactoryInterface $requestFactory)
186192
{
187193
$this->requestFactory = $requestFactory;
188194
}
189195

190196
/**
191197
* Sets the URI factory.
192198
*
193-
* @param UriFactory $uriFactory
199+
* @param UriFactoryInterface $uriFactory
194200
*/
195-
public function setUriFactory(UriFactory $uriFactory)
201+
public function setUriFactory(UriFactoryInterface $uriFactory)
196202
{
197203
$this->uriFactory = $uriFactory;
198204
}
199205

206+
/**
207+
* Sets the stream factory.
208+
*
209+
* @param StreamFactoryInterface $streamFactory
210+
*/
211+
public function setStreamFactory(StreamFactoryInterface $streamFactory)
212+
{
213+
$this->streamFactory = $streamFactory;
214+
}
215+
200216
/**
201217
* Sends POST request to Intercom API.
202218
*
@@ -310,12 +326,12 @@ public function getRateLimitDetails()
310326
}
311327

312328
/**
313-
* @return HttpClient
329+
* @return ClientInterface
314330
*/
315331
private function getDefaultHttpClient()
316332
{
317333
return new PluginClient(
318-
HttpClientDiscovery::find(),
334+
Psr18ClientDiscovery::find(),
319335
[new ErrorPlugin()]
320336
);
321337
}
@@ -372,11 +388,21 @@ private function authenticateRequest(RequestInterface $request)
372388
*/
373389
private function sendRequest($method, $uri, $body = null)
374390
{
375-
$headers = $this->getRequestHeaders();
376391
$body = is_array($body) ? json_encode($body) : $body;
377-
$request = $this->authenticateRequest(
378-
$this->requestFactory->createRequest($method, $uri, $headers, $body)
379-
);
392+
$request = $this->requestFactory
393+
->createRequest($method, $uri);
394+
395+
if ($body !== null) {
396+
$request = $request
397+
->withBody($this->streamFactory->createStream($body));
398+
}
399+
400+
foreach ($this->getRequestHeaders() as $name => $value) {
401+
$request = $request
402+
->withHeader($name, $value);
403+
}
404+
405+
$request = $this->authenticateRequest($request);
380406

381407
return $this->httpClient->sendRequest($request);
382408
}

tests/IntercomClientTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
namespace Intercom\Test;
44

55
use DateTimeImmutable;
6-
use GuzzleHttp\Psr7\Response;
76
use Http\Client\Common\Plugin\ErrorPlugin;
87
use Http\Client\Common\PluginClient;
98
use Http\Client\Exception;
10-
use Http\Discovery\HttpClientDiscovery;
9+
use Http\Discovery\Psr18ClientDiscovery;
1110
use Http\Discovery\Strategy\MockClientStrategy;
1211
use Http\Mock\Client;
1312
use Intercom\IntercomClient;
13+
use Nyholm\Psr7\Response;
1414
use stdClass;
1515

1616
class IntercomClientTest extends TestCase
1717
{
1818
protected function setUp(): void
1919
{
20-
HttpClientDiscovery::prependStrategy(MockClientStrategy::class);
20+
Psr18ClientDiscovery::prependStrategy(MockClientStrategy::class);
2121
}
2222

2323
public function testBasicClient()

0 commit comments

Comments
 (0)