Skip to content

Commit

Permalink
Replace Guzzle with generic PSR18 discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Dec 14, 2023
1 parent 4634b39 commit 9f19946
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
15 changes: 12 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
],
"require": {
"php": "^7.4 || ^8.0",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"doctrine/inflector": "^1.0 || ^2.0",
"ext-json": "*",
"composer/metadata-minifier": "^1.0",
"composer/semver": "^1.0|^2.0|^3.0"
"composer/semver": "^1.0|^2.0|^3.0",
"php-http/discovery": "^1.12",
"php-http/client-common": "^2.3",
"psr/http-client-implementation": "^1.0",
"psr/http-factory-implementation": "^1.0"
},
"require-dev": {
"phpspec/phpspec": "^6.0 || ^7.0",
"squizlabs/php_codesniffer": "^3.0"
"squizlabs/php_codesniffer": "^3.0",
"guzzlehttp/guzzle": "^6.0 || ^7.0"
},
"autoload": {
"psr-4": {
Expand All @@ -41,5 +45,10 @@
"scripts": {
"lint": "vendor/bin/phpcs --standard=PSR12 src/",
"test": "vendor/bin/phpspec run -f pretty"
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
55 changes: 34 additions & 21 deletions src/Packagist/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
namespace Packagist\Api;

use Composer\Semver\Semver;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use Exception;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\PluginClientFactory;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Packagist\Api\Result\Advisory;
use Packagist\Api\Result\Factory;
use Packagist\Api\Result\Package;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\StreamInterface;

/**
* Packagist Api
Expand All @@ -19,7 +24,7 @@
*/
class Client
{
protected ClientInterface $httpClient;
protected HttpMethodsClientInterface $httpClient;

protected Factory $resultFactory;

Expand All @@ -36,14 +41,18 @@ public function __construct(
string $packagistUrl = 'https://packagist.org'
) {
if (null === $httpClient) {
$httpClient = new HttpClient();
$httpClient = (new PluginClientFactory())->createClient(Psr18ClientDiscovery::find());
}

if (null === $resultFactory) {
$resultFactory = new Factory();
}

$this->httpClient = $httpClient;
$this->httpClient = new HttpMethodsClient(
$httpClient,
Psr17FactoryDiscovery::findRequestFactory(),
Psr17FactoryDiscovery::findStreamFactory()
);
$this->resultFactory = $resultFactory;
$this->packagistUrl = $packagistUrl;
}
Expand Down Expand Up @@ -222,9 +231,11 @@ public function advisories(array $packages = [], ?int $updatedSince = null, bool
if ($updatedSince !== null) {
$query['updatedSince'] = $updatedSince;
}
$options = [
$queryString = http_build_query([
'query' => array_filter($query),
];
]);
$headers = [];
$body = null;

// Add packages if appropriate
if (count($packages) > 0) {
Expand All @@ -235,13 +246,13 @@ public function advisories(array $packages = [], ?int $updatedSince = null, bool
}
$content['packages'][] = $package;
}
$options['headers']['Content-type'] = 'application/x-www-form-urlencoded';
$options['body'] = http_build_query($content);
$headers['Content-type'] = 'application/x-www-form-urlencoded';
$body = http_build_query($content);
}

// Get advisories from API
/** @var Advisory[] $advisories */
$advisories = $this->respondPost($this->url('/api/security-advisories/'), $options);
$advisories = $this->respondPost($this->url('/api/security-advisories/?' . $queryString), $headers, $body);

// Filter advisories if necessary
if (count($advisories) > 0 && $filterByVersion) {
Expand Down Expand Up @@ -308,12 +319,13 @@ protected function respond(string $url)
* Execute the POST request and parse the response
*
* @param string $url
* @param array $option
* @param array $headers
* @param string|StreamInterface|null $body
* @return array|Package
*/
protected function respondPost(string $url, array $options)
protected function respondPost(string $url, array $headers = [], string|StreamInterface|null $body = null)
{
$response = $this->postRequest($url, $options);
$response = $this->postRequest($url, $headers, $body);
$response = $this->parse($response);

return $this->create($response);
Expand Down Expand Up @@ -352,14 +364,15 @@ protected function multiRespond(string $url1, string $url2)
* Execute the POST request
*
* @param string $url
* @param array $options
* @param array $headers
* @param string|StreamInterface|null $body
* @return string
* @throws GuzzleException
* @throws Exception
*/
protected function postRequest(string $url, array $options): string
protected function postRequest(string $url, array $headers = [], string|StreamInterface|null $body = null): string
{
return $this->httpClient
->request('POST', $url, $options)
->post($url, $headers, $body)
->getBody()
->getContents();
}
Expand All @@ -370,16 +383,16 @@ protected function postRequest(string $url, array $options): string
* @param string $url
* @return string
* @throws PackageNotFoundException
* @throws GuzzleException
* @throws Exception
*/
protected function request(string $url): string
{
try {
return $this->httpClient
->request('GET', $url)
->get($url)
->getBody()
->getContents();
} catch (GuzzleException $e) {
} catch (Exception $e) {
if ($e->getCode() === 404) {
throw new PackageNotFoundException('The requested package was not found.', 404);
}
Expand Down

0 comments on commit 9f19946

Please sign in to comment.