Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Api/AbstractApi.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now so much more readable! Thanks :)

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

namespace PrivatePackagist\ApiClient\Api;

use Composer\Pcre\Preg;
use PrivatePackagist\ApiClient\Client;
use PrivatePackagist\ApiClient\HttpClient\Message\ResponseMediator;

abstract class AbstractApi
{
const DEFAULT_LIMIT = 500;

/** @var Client */
protected $client;
/** @var ResponseMediator */
Expand Down Expand Up @@ -47,6 +50,47 @@ protected function get($path, array $parameters = [], array $headers = [])
return $this->responseMediator->getContent($response);
}

/**
* @param string $path
* @param array $parameters
* @param array $headers
* @return array
*/
protected function getCollection($path, array $parameters = [], array $headers = [])
{
if (count($parameters) > 0) {
$path .= '?'.http_build_query($parameters);
}

$content = [];
$nextUrl = $path;

do {
$response = $this->client->getHttpClient()->get(
$nextUrl,
array_merge($headers, [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
])
);

$pageContent = $this->responseMediator->getContent($response);

if ($response->getStatusCode() !== 200) {
return $pageContent;
}

$content = array_merge($content, $pageContent);

$nextUrl = null;
if ($response->hasHeader('Link')) {
$nextUrl = $this->parseLinkHeader($response->getHeaderLine('Link'), 'next');
}
} while ($nextUrl !== null);

return $content;
}

/**
* @param string $path
* @param array $parameters
Expand Down Expand Up @@ -127,4 +171,23 @@ protected function createJsonBody(array $parameters)
{
return (count($parameters) === 0) ? null : json_encode($parameters);
}

/**
* @param string $header
* @param string $type
* @return string|null
*/
private function parseLinkHeader(string $header, string $type): ?string
{
foreach (explode(',', $header) as $relation) {
if (Preg::isMatch('/<(.*)>; rel="(.*)"/i', \trim($relation, ','), $match)) {
/** @var string[] $match */
if (3 === count($match) && $match[2] === $type) {
return $match[1];
}
}
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/Api/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Credentials extends AbstractApi

public function all()
{
return $this->get('/credentials/');
return $this->get('/credentials/', ['limit' => self::DEFAULT_LIMIT]);
}

public function show($credentialId)
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Customers extends AbstractApi
{
public function all()
{
return $this->get('/customers/');
return $this->get('/customers/', ['limit' => self::DEFAULT_LIMIT]);
}

public function show($customerIdOrUrlName)
Expand Down Expand Up @@ -70,7 +70,7 @@ public function disable($customerIdOrUrlName)

public function listPackages($customerIdOrUrlName)
{
return $this->get(sprintf('/customers/%s/packages/', $customerIdOrUrlName));
return $this->get(sprintf('/customers/%s/packages/', $customerIdOrUrlName), ['limit' => self::DEFAULT_LIMIT]);
}

public function showPackage($customerIdOrUrlName, $packageIdOrName)
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Customers/MagentoLegacyKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MagentoLegacyKeys extends AbstractApi
{
public function all($customerIdOrUrlName)
{
return $this->get(sprintf('/api/customers/%s/magento-legacy-keys/', $customerIdOrUrlName));
return $this->get(sprintf('/api/customers/%s/magento-legacy-keys/', $customerIdOrUrlName), ['limit' => self::DEFAULT_LIMIT]);
}

public function create($customerIdOrUrlName, $publicKey, $privateKey)
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Customers/VendorBundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class VendorBundles extends AbstractApi
{
public function listVendorBundles($customerIdOrUrlName)
{
return $this->get(sprintf('/customers/%s/vendor-bundles/', $customerIdOrUrlName));
return $this->get(sprintf('/customers/%s/vendor-bundles/', $customerIdOrUrlName), ['limit' => self::DEFAULT_LIMIT]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Api/MirroredRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MirroredRepositories extends AbstractApi
{
public function all()
{
return $this->get('/mirrored-repositories/');
return $this->get('/mirrored-repositories/', ['limit' => self::DEFAULT_LIMIT]);
}

public function create($name, $url, $mirroringBehavior, $credentials = null)
Expand Down Expand Up @@ -48,7 +48,7 @@ public function remove($mirroredRepositoryId)

public function listPackages($mirroredRepositoryId)
{
return $this->get(sprintf('/mirrored-repositories/%s/packages/', $mirroredRepositoryId));
return $this->get(sprintf('/mirrored-repositories/%s/packages/', $mirroredRepositoryId), ['limit' => self::DEFAULT_LIMIT]);
}

public function addPackages($mirroredRepositoryId, array $packages)
Expand Down
10 changes: 7 additions & 3 deletions src/Api/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public function all(array $filters = [])
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', self::AVAILABLE_ORIGINS) . '".');
}

return $this->get('/packages/', $filters);
$filters = array_merge(['limit' => self::DEFAULT_LIMIT], $filters);

return $this->getCollection('/packages/', $filters);
}

public function show($packageIdOrName)
Expand Down Expand Up @@ -127,16 +129,18 @@ public function remove($packageIdOrName)

public function listCustomers($packageIdOrName)
{
return $this->get(sprintf('/packages/%s/customers/', $packageIdOrName));
return $this->get(sprintf('/packages/%s/customers/', $packageIdOrName), ['limit' => self::DEFAULT_LIMIT]);
}

public function listDependents($packageName)
{
return $this->get(sprintf('/packages/%s/dependents/', $packageName));
return $this->get(sprintf('/packages/%s/dependents/', $packageName), ['limit' => self::DEFAULT_LIMIT]);
}

public function listSecurityIssues($packageIdOrName, array $filters = [])
{
$filters = array_merge(['limit' => self::DEFAULT_LIMIT], $filters);

return $this->get(sprintf('/packages/%s/security-issues/', $packageIdOrName), $filters);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Api/Packages/Artifacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public function show($artifactId)

public function showPackageArtifacts($packageIdOrName)
{
return $this->get(sprintf('/packages/%s/artifacts/', $packageIdOrName));
return $this->get(sprintf('/packages/%s/artifacts/', $packageIdOrName), ['limit' => self::DEFAULT_LIMIT]);
}
}
2 changes: 2 additions & 0 deletions src/Api/SecurityIssues.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class SecurityIssues extends AbstractApi

public function all(array $filters = [])
{
$filters = array_merge(['limit' => self::DEFAULT_LIMIT], $filters);

return $this->get('/security-issues/', $filters);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Api/Suborganizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Suborganizations extends AbstractApi
{
public function all()
{
return $this->get('/suborganizations/');
return $this->get('/suborganizations/', ['limit' => self::DEFAULT_LIMIT]);
}

public function show($suborganizationName)
Expand All @@ -35,7 +35,7 @@ public function remove($suborganizationName)

public function listTeams($suborganizationName)
{
return $this->get(sprintf('/suborganizations/%s/teams/', $suborganizationName));
return $this->get(sprintf('/suborganizations/%s/teams/', $suborganizationName), ['limit' => self::DEFAULT_LIMIT]);
}

public function addOrEditTeams($suborganizationName, array $teams)
Expand All @@ -60,7 +60,7 @@ public function removeTeam($suborganizationName, $teamId)

public function listTokens($suborganizationName)
{
return $this->get(sprintf('/suborganizations/%s/tokens/', $suborganizationName));
return $this->get(sprintf('/suborganizations/%s/tokens/', $suborganizationName), ['limit' => self::DEFAULT_LIMIT]);
}

public function createToken($suborganizationName, array $tokenData)
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Suborganizations/MirroredRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MirroredRepositories extends AbstractApi
{
public function all($suborganizationName)
{
return $this->get(sprintf('/suborganizations/%s/mirrored-repositories/', $suborganizationName));
return $this->get(sprintf('/suborganizations/%s/mirrored-repositories/', $suborganizationName), ['limit' => self::DEFAULT_LIMIT]);
}

public function add($suborganizationName, array $mirroredRepositories)
Expand Down Expand Up @@ -49,7 +49,7 @@ public function remove($suborganizationName, $mirroredRepositoryId)

public function listPackages($suborganizationName, $mirroredRepositoryId)
{
return $this->get(sprintf('/suborganizations/%s/mirrored-repositories/%s/packages/', $suborganizationName, $mirroredRepositoryId));
return $this->get(sprintf('/suborganizations/%s/mirrored-repositories/%s/packages/', $suborganizationName, $mirroredRepositoryId), ['limit' => self::DEFAULT_LIMIT]);
}

public function addPackages($suborganizationName, $mirroredRepositoryId, array $packages)
Expand Down
4 changes: 3 additions & 1 deletion src/Api/Suborganizations/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function all($suborganizationName, array $filters = [])
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', \PrivatePackagist\ApiClient\Api\Packages::AVAILABLE_ORIGINS) . '".');
}

$filters = array_merge(['limit' => self::DEFAULT_LIMIT], $filters);

return $this->get(sprintf('/suborganizations/%s/packages/', $suborganizationName), $filters);
}

Expand Down Expand Up @@ -65,6 +67,6 @@ public function remove($suborganizationName, $packageIdOrName)

public function listDependents($suborganizationName, $packageIdOrName)
{
return $this->get(sprintf('/suborganizations/%s/packages/%s/dependents/', $suborganizationName, $packageIdOrName));
return $this->get(sprintf('/suborganizations/%s/packages/%s/dependents/', $suborganizationName, $packageIdOrName), ['limit' => self::DEFAULT_LIMIT]);
}
}
8 changes: 4 additions & 4 deletions src/Api/Subrepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Subrepositories extends AbstractApi
{
public function all()
{
return $this->get('/subrepositories/');
return $this->get('/subrepositories/', ['limit' => self::DEFAULT_LIMIT]);
}

public function show($subrepositoryName)
Expand All @@ -38,7 +38,7 @@ public function remove($subrepositoryName)

public function listTeams($subrepositoryName)
{
return $this->get(sprintf('/subrepositories/%s/teams/', $subrepositoryName));
return $this->get(sprintf('/subrepositories/%s/teams/', $subrepositoryName), ['limit' => self::DEFAULT_LIMIT]);
}

/**
Expand Down Expand Up @@ -76,12 +76,12 @@ public function removeTeam($subrepositoryName, $teamId)
#[\Deprecated('Use Subrepositories::packages()->all() instead', '1.16.1')]
public function listPackages($subrepositoryName)
{
return $this->packages()->all($subrepositoryName);
return $this->packages()->all($subrepositoryName, ['limit' => self::DEFAULT_LIMIT]);
}

public function listTokens($subrepositoryName)
{
return $this->get(sprintf('/subrepositories/%s/tokens/', $subrepositoryName));
return $this->get(sprintf('/subrepositories/%s/tokens/', $subrepositoryName), ['limit' => self::DEFAULT_LIMIT]);
}

public function createToken($subrepositoryName, array $tokenData)
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Subrepositories/MirroredRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MirroredRepositories extends AbstractApi
{
public function all($subrepositoryName)
{
return $this->get(sprintf('/subrepositories/%s/mirrored-repositories/', $subrepositoryName));
return $this->get(sprintf('/subrepositories/%s/mirrored-repositories/', $subrepositoryName), ['limit' => self::DEFAULT_LIMIT]);
}

public function add($subrepositoryName, array $mirroredRepositories)
Expand Down Expand Up @@ -52,7 +52,7 @@ public function remove($subrepositoryName, $mirroredRepositoryId)

public function listPackages($subrepositoryName, $mirroredRepositoryId)
{
return $this->get(sprintf('/subrepositories/%s/mirrored-repositories/%s/packages/', $subrepositoryName, $mirroredRepositoryId));
return $this->get(sprintf('/subrepositories/%s/mirrored-repositories/%s/packages/', $subrepositoryName, $mirroredRepositoryId), ['limit' => self::DEFAULT_LIMIT]);
}

public function addPackages($subrepositoryName, $mirroredRepositoryId, array $packages)
Expand Down
4 changes: 3 additions & 1 deletion src/Api/Subrepositories/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function all($subrepositoryName, array $filters = [])
throw new InvalidArgumentException('Filter "origin" has to be one of: "' . implode('", "', \PrivatePackagist\ApiClient\Api\Packages::AVAILABLE_ORIGINS) . '".');
}

$filters = array_merge(['limit' => self::DEFAULT_LIMIT], $filters);

return $this->get(sprintf('/subrepositories/%s/packages/', $subrepositoryName), $filters);
}

Expand Down Expand Up @@ -68,6 +70,6 @@ public function remove($subrepositoryName, $packageIdOrName)

public function listDependents($subrepositoryName, $packageIdOrName)
{
return $this->get(sprintf('/subrepositories/%s/packages/%s/dependents/', $subrepositoryName, $packageIdOrName));
return $this->get(sprintf('/subrepositories/%s/packages/%s/dependents/', $subrepositoryName, $packageIdOrName), ['limit' => self::DEFAULT_LIMIT]);
}
}
2 changes: 1 addition & 1 deletion src/Api/Synchronizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Synchronizations extends AbstractApi
{
public function all()
{
return $this->get('/synchronizations/');
return $this->get('/synchronizations/', ['limit' => self::DEFAULT_LIMIT]);
}
}
4 changes: 2 additions & 2 deletions src/Api/Teams.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Teams extends AbstractApi
{
public function all()
{
return $this->get('/teams/');
return $this->get('/teams/', ['limit' => self::DEFAULT_LIMIT]);
}

public function create(string $name, TeamPermissions $permissions): array
Expand Down Expand Up @@ -82,7 +82,7 @@ public function removeMember($teamId, $userId): array

public function packages($teamId)
{
return $this->get(sprintf('/teams/%s/packages/', $teamId));
return $this->get(sprintf('/teams/%s/packages/', $teamId), ['limit' => self::DEFAULT_LIMIT]);
}

public function addPackages($teamId, array $packages)
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Tokens extends AbstractApi
{
public function all()
{
return $this->get('/tokens/');
return $this->get('/tokens/', ['limit' => self::DEFAULT_LIMIT]);
}

public function create(array $tokenData)
Expand Down
2 changes: 1 addition & 1 deletion src/Api/VendorBundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class VendorBundles extends AbstractApi
*/
public function all()
{
return $this->get('/vendor-bundles/');
return $this->get('/vendor-bundles/', ['limit' => self::DEFAULT_LIMIT]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Api/VendorBundles/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Packages extends AbstractApi
*/
public function listPackages($vendorBundleIds)
{
return $this->get(sprintf('/vendor-bundles/%s/packages/', $vendorBundleIds));
return $this->get(sprintf('/vendor-bundles/%s/packages/', $vendorBundleIds), ['limit' => self::DEFAULT_LIMIT]);
}

/**
Expand Down
Loading
Loading