-
Notifications
You must be signed in to change notification settings - Fork 45
feat: add methods to fetch package info from new composer 2 api #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| require __DIR__.'/../vendor/autoload.php'; | ||
|
|
||
| $client = new Packagist\Api\Client(); | ||
| $package = $client->getComposer('sylius/sylius'); | ||
|
|
||
| var_export($package); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| require __DIR__.'/../vendor/autoload.php'; | ||
|
|
||
| $client = new Packagist\Api\Client(); | ||
| $package = $client->getComposerLite('sylius/sylius'); | ||
|
|
||
| var_export($package); |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
|
|
||
| use GuzzleHttp\Client as HttpClient; | ||
| use GuzzleHttp\ClientInterface; | ||
| use GuzzleHttp\Exception\GuzzleException; | ||
| use Packagist\Api\Result\Factory; | ||
| use Packagist\Api\Result\Package; | ||
| use Psr\Http\Message\StreamInterface; | ||
|
|
@@ -55,9 +56,9 @@ public function __construct( | |
| * * type: type of package (type in composer.json) | ||
| * * tags: tags of package (keywords in composer.json) | ||
| * | ||
| * @param string $query Name of package | ||
| * @param array $filters An array of filters | ||
| * @param int $limit Pages to limit results (0 = all pages) | ||
| * @param string $query Name of package | ||
| * @param array $filters An array of filters | ||
| * @param int $limit Pages to limit results (0 = all pages) | ||
| * | ||
| * @return array The results | ||
| */ | ||
|
|
@@ -98,23 +99,25 @@ public function get(string $package) | |
| } | ||
|
|
||
| /** | ||
| * Similar to {@link get()}, but uses composer metadata which is Packagist's preferred | ||
| * way of retrieving details, since responses are cached efficiently as static files | ||
| * by the Packagist service. The response lacks some metadata that is provided | ||
| * by {@link get()}, see https://packagist.org/apidoc for details. | ||
| * | ||
| * Caution: Returns an array of packages, you need to select the correct one | ||
| * from the indexed array. | ||
| * | ||
| * @since 1.6 | ||
| * | ||
| * @see https://packagist.org/apidoc#get-package-data | ||
| * Contains tagged releases and dev versions | ||
| * @param string $package Full qualified name ex : myname/mypackage | ||
| * | ||
| * @return \Packagist\Api\Result\Package[] An array of packages, including the requested one. | ||
| * @return Package[] An array of packages, including the requested one. | ||
| */ | ||
| public function getComposer(string $package): array | ||
| { | ||
| return $this->respond(sprintf($this->url('/p2/%s~dev.json'), $package)); | ||
| } | ||
|
|
||
| /** | ||
| * @see https://packagist.org/apidoc#get-package-data | ||
| * Contains only tagged releases | ||
| * @param string $package Full qualified name ex : myname/mypackage | ||
| * @return Package[] An array of packages, including the requested one. | ||
| */ | ||
| public function getComposer($package) | ||
| public function getComposerLite(string $package): array | ||
| { | ||
| return $this->respond(sprintf($this->url('/p/%s.json'), $package)); | ||
| return $this->respond(sprintf($this->url('/p2/%s.json'), $package)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just confirming that the change of the API we consume in this method was deliberate? I have no problem with changing the underlying API we consume from v1 to v2. Is the intention of adding
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! I confirm! |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -197,9 +200,14 @@ protected function respond(string $url) | |
| */ | ||
| protected function request(string $url): string | ||
| { | ||
| return (string) $this->httpClient | ||
| ->request('GET', $url) | ||
| ->getBody(); | ||
| try { | ||
| return $this->httpClient | ||
| ->request('GET', $url) | ||
| ->getBody() | ||
| ->getContents(); | ||
| } catch (GuzzleException $e) { | ||
| return json_encode([]); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be careful. The
~dev.jsonfiles is a not about getting all versions. It is about getting dev versions. None of the releases (be them stable releases or beta releases) are in that file. If you want to get all versions, you need to load both files.