55namespace Packagist \Api ;
66
77use Composer \Semver \Semver ;
8- use GuzzleHttp \Client as HttpClient ;
9- use GuzzleHttp \ClientInterface ;
10- use GuzzleHttp \Exception \GuzzleException ;
8+ use Exception ;
9+ use Http \Client \Common \HttpMethodsClient ;
10+ use Http \Client \Common \HttpMethodsClientInterface ;
11+ use Http \Client \Common \PluginClientFactory ;
12+ use Http \Discovery \Psr17FactoryDiscovery ;
13+ use Http \Discovery \Psr18ClientDiscovery ;
1114use Packagist \Api \Result \Advisory ;
1215use Packagist \Api \Result \Factory ;
1316use Packagist \Api \Result \Package ;
17+ use Psr \Http \Client \ClientInterface ;
18+ use Psr \Http \Message \StreamInterface ;
1419
1520/**
1621 * Packagist Api
1924 */
2025class Client
2126{
22- protected ClientInterface $ httpClient ;
27+ protected HttpMethodsClientInterface $ httpClient ;
2328
2429 protected Factory $ resultFactory ;
2530
@@ -36,14 +41,18 @@ public function __construct(
3641 string $ packagistUrl = 'https://packagist.org '
3742 ) {
3843 if (null === $ httpClient ) {
39- $ httpClient = new HttpClient ( );
44+ $ httpClient = ( new PluginClientFactory ())-> createClient (Psr18ClientDiscovery:: find () );
4045 }
4146
4247 if (null === $ resultFactory ) {
4348 $ resultFactory = new Factory ();
4449 }
4550
46- $ this ->httpClient = $ httpClient ;
51+ $ this ->httpClient = new HttpMethodsClient (
52+ $ httpClient ,
53+ Psr17FactoryDiscovery::findRequestFactory (),
54+ Psr17FactoryDiscovery::findStreamFactory ()
55+ );
4756 $ this ->resultFactory = $ resultFactory ;
4857 $ this ->packagistUrl = $ packagistUrl ;
4958 }
@@ -222,9 +231,11 @@ public function advisories(array $packages = [], ?int $updatedSince = null, bool
222231 if ($ updatedSince !== null ) {
223232 $ query ['updatedSince ' ] = $ updatedSince ;
224233 }
225- $ options = [
234+ $ queryString = http_build_query ( [
226235 'query ' => array_filter ($ query ),
227- ];
236+ ]);
237+ $ headers = [];
238+ $ body = null ;
228239
229240 // Add packages if appropriate
230241 if (count ($ packages ) > 0 ) {
@@ -235,13 +246,13 @@ public function advisories(array $packages = [], ?int $updatedSince = null, bool
235246 }
236247 $ content ['packages ' ][] = $ package ;
237248 }
238- $ options [ ' headers ' ] ['Content-type ' ] = 'application/x-www-form-urlencoded ' ;
239- $ options [ ' body ' ] = http_build_query ($ content );
249+ $ headers ['Content-type ' ] = 'application/x-www-form-urlencoded ' ;
250+ $ body = http_build_query ($ content );
240251 }
241252
242253 // Get advisories from API
243254 /** @var Advisory[] $advisories */
244- $ advisories = $ this ->respondPost ($ this ->url ('/api/security-advisories/ ' ), $ options );
255+ $ advisories = $ this ->respondPost ($ this ->url ('/api/security-advisories/? ' . $ queryString ), $ headers , $ body );
245256
246257 // Filter advisories if necessary
247258 if (count ($ advisories ) > 0 && $ filterByVersion ) {
@@ -308,12 +319,13 @@ protected function respond(string $url)
308319 * Execute the POST request and parse the response
309320 *
310321 * @param string $url
311- * @param array $option
322+ * @param array $headers
323+ * @param string|StreamInterface|null $body
312324 * @return array|Package
313325 */
314- protected function respondPost (string $ url , array $ options )
326+ protected function respondPost (string $ url , array $ headers = [], string | StreamInterface | null $ body = null )
315327 {
316- $ response = $ this ->postRequest ($ url , $ options );
328+ $ response = $ this ->postRequest ($ url , $ headers , $ body );
317329 $ response = $ this ->parse ($ response );
318330
319331 return $ this ->create ($ response );
@@ -352,14 +364,15 @@ protected function multiRespond(string $url1, string $url2)
352364 * Execute the POST request
353365 *
354366 * @param string $url
355- * @param array $options
367+ * @param array $headers
368+ * @param string|StreamInterface|null $body
356369 * @return string
357- * @throws GuzzleException
370+ * @throws Exception
358371 */
359- protected function postRequest (string $ url , array $ options ): string
372+ protected function postRequest (string $ url , array $ headers = [], string | StreamInterface | null $ body = null ): string
360373 {
361374 return $ this ->httpClient
362- ->request ( ' POST ' , $ url , $ options )
375+ ->post ( $ url , $ headers , $ body )
363376 ->getBody ()
364377 ->getContents ();
365378 }
@@ -370,16 +383,16 @@ protected function postRequest(string $url, array $options): string
370383 * @param string $url
371384 * @return string
372385 * @throws PackageNotFoundException
373- * @throws GuzzleException
386+ * @throws Exception
374387 */
375388 protected function request (string $ url ): string
376389 {
377390 try {
378391 return $ this ->httpClient
379- ->request ( ' GET ' , $ url )
392+ ->get ( $ url )
380393 ->getBody ()
381394 ->getContents ();
382- } catch (GuzzleException $ e ) {
395+ } catch (Exception $ e ) {
383396 if ($ e ->getCode () === 404 ) {
384397 throw new PackageNotFoundException ('The requested package was not found. ' , 404 );
385398 }
0 commit comments