diff --git a/README.md b/README.md index c8c571e..368fe78 100644 --- a/README.md +++ b/README.md @@ -31,29 +31,38 @@ require 'vendor/autoload.php'; ## Basic Usage -LibrariesIO splits the different endpoints of the API into several "components": +LibrariesIO splits the different endpoints based on their "component": - * Esi\LibrariesIO\Platform\Platform + * Esi\LibrariesIO\Platform + * Platform::makeRequest() does not require an $endpoint, though you can pass 'platforms'. * Esi\LibrariesIO\Project - * Esi\LibrariesIO\Project\Contributors - * Esi\LibrariesIO\Project\Dependencies - * Esi\LibrariesIO\Project\Dependents - * Esi\LibrariesIO\Project\DependentRepositories - * Esi\LibrariesIO\Project\Project - * Esi\LibrariesIO\Project\Search - * Esi\LibrariesIO\Project\SourceRank + * Project::makeRequest() takes an 'endpoint' parameter to specify which subset you are looking for. + * Current endpoints are: + * contributors + * dependencies + * dependents + * dependent_repositories + * project + * search + * sourceRank * Esi\LibrariesIO\Repository - * Esi\LibrariesIO\Repository\Dependencies - * Esi\LibrariesIO\Repository\Projects - * Esi\LibrariesIO\Repository\Repository + * Repository::makeRequest() takes an 'endpoint' parameter to specify which subset you are looking for. + * Current endpoints are: + * dependencies + * projects + * repository * Esi\LibrariesIO\User - * Esi\LibrariesIO\User\Dependencies - * Esi\LibrariesIO\User\Packages - * Esi\LibrariesIO\User\PackageContributions - * Esi\LibrariesIO\User\Repositories - * Esi\LibrariesIO\User\RepositoryContributions - * Esi\LibrariesIO\User\Subscriptions - * Esi\LibrariesIO\User\User + * User::makeRequest() takes an 'endpoint' parameter to specify which subset you are looking for. + * Current endpoints are: + * dependencies + * packages + * package_contributions + * repositories + * repository_contributions + * subscriptions + * user + +Each 'subset' has their own required options. Check the documentation (currently WIP) for more information. As an example, let's say you want to get a list of the available platforms. To do so: diff --git a/src/AbstractBase.php b/src/AbstractBase.php index fc43d0d..7620cd7 100644 --- a/src/AbstractBase.php +++ b/src/AbstractBase.php @@ -15,22 +15,27 @@ namespace Esi\LibrariesIO; // Exceptions and Attributes -use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\Exception\{ + GuzzleException, + ClientException +}; +use InvalidArgumentException, JsonException; use SensitiveParameter; -use InvalidArgumentException; -use JsonException; -// Guzzle -use GuzzleHttp\Client; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Exception\ClientException; +// HTTP +use GuzzleHttp\{ + Client, + HandlerStack +}; use Psr\Http\Message\ResponseInterface; // Cache use Symfony\Component\Cache\Adapter\FilesystemAdapter; -use Kevinrob\GuzzleCache\CacheMiddleware; -use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; -use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage; +use Kevinrob\GuzzleCache\{ + CacheMiddleware, + Strategy\PrivateCacheStrategy, + Storage\Psr6CacheStorage +}; // Functions and constants use function is_dir, is_writable, json_decode, preg_match; @@ -73,14 +78,14 @@ abstract class AbstractBase * * @var Client */ - protected Client $client; + public Client $client; /** * Base API endpoint. * * @var string */ - protected const API_URL = 'https://libraries.io/api/'; + public const API_URL = 'https://libraries.io/api/'; /** * Libraries.io API key. @@ -88,14 +93,14 @@ abstract class AbstractBase * @see https://libraries.io/account * @var ?string */ - protected ?string $apiKey = null; + public ?string $apiKey = null; /** * Path to your cache folder on the file system. * * @var ?string */ - protected ?string $cachePath = null; + public ?string $cachePath = null; /** * Constructor. @@ -127,7 +132,7 @@ public function __construct( * @param array $query * @return Client */ - protected function makeClient(?array $query = null): Client + public function makeClient(?array $query = null): Client { // Some endpoints do not require any query parameters if ($query === null) { @@ -174,11 +179,68 @@ protected function makeClient(?array $query = null): Client /** * Performs the actual request to the API. * + * @param string $endpoint * @param array $options * @return ResponseInterface * @throws ClientException|GuzzleException */ - abstract function makeRequest(array $options): ResponseInterface; + public abstract function makeRequest(string $endpoint, array $options): ResponseInterface; + + /** + * Processes the available parameters for a given endpoint. + * + * @param string $endpoint + * @return array|string> + */ + public abstract function endpointParameters(string $endpoint): array; + + /** + * Each endpoint class will have a 'subset' of endpoints that fall under it. This + * function handles returning a formatted endpoint for the Client. + * + * @param string $format + * @param array $options + * @return string + */ + public function processEndpointFormat(string $format, array $options): string + { + if (\str_contains($format, ':') === false) { + return $format; + } + + foreach ($options AS $key => $val) { + if ($key === 'page' || $key === 'per_page') { + continue; + } + $format = str_replace(":$key", $val, $format); + } + return $format; + } + + /** + * Helper function to make sure that the $options passed to the child class' makeRequest() + * contains the required options listed in the endpoints options. + * + * @param array|string> $endpointOptions + * @param array $options + * @return bool + */ + public function verifyEndpointOptions(array $endpointOptions, array $options): bool + { + if ($endpointOptions === []) { + return true; + } + + $noError = true; + + foreach ($endpointOptions AS $endpointOption) { + if (!isset($options[$endpointOption])) { + $noError = false; + break; + } + } + return $noError; + } /** * Decodes the jSON returned from the API. Returns as an associative array. diff --git a/src/Exception/RateLimitExceededException.php b/src/Exception/RateLimitExceededException.php index 7261f97..d8ea2f5 100644 --- a/src/Exception/RateLimitExceededException.php +++ b/src/Exception/RateLimitExceededException.php @@ -46,6 +46,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + +/** + * @psalm-api + */ final class RateLimitExceededException extends InvalidArgumentException { // diff --git a/src/Platform/Platform.php b/src/Platform.php similarity index 71% rename from src/Platform/Platform.php rename to src/Platform.php index d6a5481..852a33e 100644 --- a/src/Platform/Platform.php +++ b/src/Platform.php @@ -12,10 +12,12 @@ * @copyright (C) 2023 Eric Sizemore * @license The MIT License (MIT) */ -namespace Esi\LibrariesIO\Platform; +namespace Esi\LibrariesIO; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; +use Esi\LibrariesIO\{ + Exception\RateLimitExceededException, + AbstractBase +}; use GuzzleHttp\Exception\ClientException; use Psr\Http\Message\ResponseInterface; @@ -55,16 +57,23 @@ final class Platform extends AbstractBase /** * {@inheritdoc} */ - public function makeRequest(?array $options = null): ResponseInterface + public function makeRequest(string $endpoint = 'platforms', ?array $options = null): ResponseInterface { - // We actually do not need any $options for this call + // We actually do not need any $options for this call, and the only valid endpoint is 'platforms' currently + $endpointParameters = $this->endpointParameters($endpoint); + + if ($endpointParameters === []) { + throw new InvalidArgumentException( + 'Invalid endpoint specified. Must be one of: platforms' + ); + } // Build query parent::makeClient(); // Attempt the request try { - return $this->client->get('platforms'); + return $this->client->get($endpointParameters['format']); } catch (ClientException $e) { if ($e->getResponse()->getStatusCode() === 429) { throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); @@ -73,4 +82,15 @@ public function makeRequest(?array $options = null): ResponseInterface } } } + + /** + * {@inheritdoc} + */ + public function endpointParameters(string $endpoint): array + { + return match($endpoint) { + 'platforms' => ['format' => 'platforms', 'options' => []], + default => [] + }; + } } diff --git a/src/Project/Search.php b/src/Project.php similarity index 52% rename from src/Project/Search.php rename to src/Project.php index ed7c8c9..8e022e1 100644 --- a/src/Project/Search.php +++ b/src/Project.php @@ -12,14 +12,19 @@ * @copyright (C) 2023 Eric Sizemore * @license The MIT License (MIT) */ -namespace Esi\LibrariesIO\Project; +namespace Esi\LibrariesIO; use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; + +use Esi\LibrariesIO\{ + Exception\RateLimitExceededException, + AbstractBase +}; + use GuzzleHttp\Exception\ClientException; use Psr\Http\Message\ResponseInterface; -use function in_array; + +use function in_array, implode; /** * LibrariesIO - A simple API wrapper/client for the Libraries.io API. @@ -51,15 +56,92 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -final class Search extends AbstractBase +final class Project extends AbstractBase { + /** + * {@inheritdoc} + */ + public function makeRequest(string $endpoint, array $options): ResponseInterface + { + // Make sure we have the format and options for $endpoint + $endpointParameters = $this->endpointParameters($endpoint); + + if ($endpointParameters === []) { + throw new InvalidArgumentException( + 'Invalid endpoint specified. Must be one of: contributors, dependencies, dependent_repositories, dependents, search, sourcerank, or project' + ); + } + + $endpointOptions = $endpointParameters['options']; + + if (!parent::verifyEndpointOptions($endpointOptions, $options)) { + throw new InvalidArgumentException( + '$options has not specified all required parameters. Paremeters needed: ' . implode(', ', $endpointOptions) + ); + } + + // Build query + $query = [ + 'page' => $options['page'] ?? 1, + 'per_page' => $options['per_page'] ?? 30 + ]; + + // If on the 'search' endpoint, we have to provide the query and sort parameters. + if ($endpoint === 'search') { + $query += [ + 'q' => $options['query'], + 'sort' => $this->searchVerifySortOption(/** @phpstan-ignore-line **/$options['sort']), + ]; + + // Search can also have: 'languages', 'licenses', 'keywords', 'platforms' as additional paremeters + $additionalParams = $this->searchAdditionalParams($options); + + if ($additionalParams !== []) { + $query += $additionalParams; + } + } + + // Build the client + parent::makeClient($query); + + // Attempt the request + $endpointParameters['format'] = parent::processEndpointFormat($endpointParameters['format'], $options); + + try { + return $this->client->get($endpointParameters['format']); + } catch (ClientException $e) { + if ($e->getResponse()->getStatusCode() === 429) { + throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); + } else { + throw $e; + } + } + } + + /** + * {@inheritdoc} + */ + public function endpointParameters(string $endpoint): array + { + return match($endpoint) { + 'contributors' => ['format' => ':platform/:name/contributors' , 'options' => ['platform', 'name']], + 'dependencies' => ['format' => ':platform/:name/:version/dependencies' , 'options' => ['platform', 'name', 'version']], + 'dependent_repositories' => ['format' => ':platform/:name/dependent_repositories', 'options' => ['platform', 'name']], + 'dependents' => ['format' => ':platform/:name/dependents' , 'options' => ['platform', 'name']], + 'search' => ['format' => 'search' , 'options' => ['query', 'sort']], + 'sourcerank' => ['format' => ':platform/:name/sourcerank' , 'options' => ['platform', 'name']], + 'project' => ['format' => ':platform/:name' , 'options' => ['platform', 'name']], + default => [] + }; + } + /** * Processes the additional parameters that can be used by the search endpoint. * * @param array $options * @return array */ - protected function processAdditionalParams(array $options): array + protected function searchAdditionalParams(array $options): array { $additionalParams = []; @@ -77,7 +159,7 @@ protected function processAdditionalParams(array $options): array * @param string $sort * @return string */ - protected function verifySortOption(string $sort): string + protected function searchVerifySortOption(string $sort): string { static $sortOptions = [ 'rank', 'stars', 'dependents_count', @@ -90,43 +172,4 @@ protected function verifySortOption(string $sort): string } return $sort; } - - /** - * {@inheritdoc} - */ - public function makeRequest(array $options): ResponseInterface - { - // We need two options - if (!isset($options['query'], $options['sort'])) { - throw new InvalidArgumentException('$options requires 2 parameters (query, sort)'); - } - - // Build query - $query = [ - 'q' => $options['query'], - 'sort' => $this->verifySortOption(/** @phpstan-ignore-line **/$options['sort']), - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]; - - $additionalParams = $this->processAdditionalParams($options); - - if ($additionalParams !== []) { - $query += $additionalParams; - } - - parent::makeClient($query); - - // Attempt the request - try { - return $this->client->get('search'); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } } diff --git a/src/Project/Dependencies.php b/src/Project/Dependencies.php deleted file mode 100644 index 0ebc70d..0000000 --- a/src/Project/Dependencies.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\Project; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Dependencies extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(array $options): ResponseInterface - { - // We need two options - if (!isset($options['platform'], $options['name'], $options['version'])) { - throw new InvalidArgumentException('$options requires 3 parameters (platform, name, version)'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('%s/%s/%s/dependencies', $options['platform'], $options['name'], $options['version'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/Project/DependentRepositories.php b/src/Project/DependentRepositories.php deleted file mode 100644 index ada5023..0000000 --- a/src/Project/DependentRepositories.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\Project; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class DependentRepositories extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(array $options): ResponseInterface - { - // We need two options - if (!isset($options['platform'], $options['name'])) { - throw new InvalidArgumentException('$options requires 2 parameters (platform, name)'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('%s/%s/dependent_repositories', $options['platform'], $options['name'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/Project/Dependents.php b/src/Project/Dependents.php deleted file mode 100644 index c629b6f..0000000 --- a/src/Project/Dependents.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\Project; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Dependents extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(array $options): ResponseInterface - { - // We need two options - if (!isset($options['platform'], $options['name'])) { - throw new InvalidArgumentException('$options requires 2 parameters (platform, name)'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('%s/%s/dependents', $options['platform'], $options['name'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/Project/Project.php b/src/Project/Project.php deleted file mode 100644 index c7f117e..0000000 --- a/src/Project/Project.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\Project; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Project extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(array $options): ResponseInterface - { - // We need two options - if (!isset($options['platform'], $options['name'])) { - throw new InvalidArgumentException('$options requires 2 parameters (platform, name)'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('%s/%s', $options['platform'], $options['name'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/Project/SourceRank.php b/src/Project/SourceRank.php deleted file mode 100644 index f10f39e..0000000 --- a/src/Project/SourceRank.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\Project; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class SourceRank extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(array $options): ResponseInterface - { - // We need two options - if (!isset($options['platform'], $options['name'])) { - throw new InvalidArgumentException('$options requires 2 parameters (platform, name)'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('%s/%s/sourcerank', $options['platform'], $options['name'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/Repository/Repository.php b/src/Repository.php similarity index 60% rename from src/Repository/Repository.php rename to src/Repository.php index 10d168a..82df200 100644 --- a/src/Repository/Repository.php +++ b/src/Repository.php @@ -12,14 +12,17 @@ * @copyright (C) 2023 Eric Sizemore * @license The MIT License (MIT) */ -namespace Esi\LibrariesIO\Repository; +namespace Esi\LibrariesIO; use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; +use Esi\LibrariesIO\{ + Exception\RateLimitExceededException, + AbstractBase +}; use GuzzleHttp\Exception\ClientException; use Psr\Http\Message\ResponseInterface; -use function sprintf; + +use function implode; /** * LibrariesIO - A simple API wrapper/client for the Libraries.io API. @@ -51,16 +54,31 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ +/** + * @psalm-api + */ final class Repository extends AbstractBase { /** * {@inheritdoc} */ - public function makeRequest(?array $options = null): ResponseInterface + public function makeRequest(string $endpoint, ?array $options = null): ResponseInterface { - // We need two options - if (!isset($options['owner'], $options['name'])) { - throw new InvalidArgumentException('$options requires 2 parameters (owner, name)'); + // Make sure we have the format and options for $endpoint + $endpointParameters = $this->endpointParameters($endpoint); + + if ($endpointParameters === []) { + throw new InvalidArgumentException( + 'Invalid endpoint specified. Must be one of: dependencies, projects, or repository' + ); + } + + $endpointOptions = $endpointParameters['options']; + + if (!parent::verifyEndpointOptions($endpointOptions, $options)) { + throw new InvalidArgumentException( + '$options has not specified all required parameters. Paremeters needed: ' . implode(', ', $endpointOptions) + ); } // Build query @@ -71,8 +89,10 @@ public function makeRequest(?array $options = null): ResponseInterface ]); // Attempt the request + $endpointParameters['format'] = parent::processEndpointFormat($endpointParameters['format'], $options); + try { - return $this->client->get(sprintf('github/%s/%s', $options['owner'], $options['name'])); + return $this->client->get($endpointParameters['format']); } catch (ClientException $e) { if ($e->getResponse()->getStatusCode() === 429) { throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); @@ -81,4 +101,17 @@ public function makeRequest(?array $options = null): ResponseInterface } } } + + /** + * {@inheritdoc} + */ + public function endpointParameters(string $endpoint): array + { + return match($endpoint) { + 'dependencies' => ['format' => 'github/:owner/:name/dependencies', 'options' => ['owner', 'name']], + 'projects' => ['format' => 'github/:owner/:name/projects' , 'options' => ['owner', 'name']], + 'repository' => ['format' => 'github/:owner/:name' , 'options' => ['owner', 'name']], + default => [] + }; + } } diff --git a/src/Repository/Dependencies.php b/src/Repository/Dependencies.php deleted file mode 100644 index 094fb5c..0000000 --- a/src/Repository/Dependencies.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\Repository; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Dependencies extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // We need two options - if (!isset($options['owner'], $options['name'])) { - throw new InvalidArgumentException('$options requires 2 parameters (owner, name)'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('github/%s/%s/dependencies', $options['owner'], $options['name'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/Repository/Projects.php b/src/Repository/Projects.php deleted file mode 100644 index 45e6e0f..0000000 --- a/src/Repository/Projects.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\Repository; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Projects extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // We need two options - if (!isset($options['owner'], $options['name'])) { - throw new InvalidArgumentException('$options requires 2 parameters (owner, name)'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('github/%s/%s/projects', $options['owner'], $options['name'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/Project/Contributors.php b/src/User.php similarity index 51% rename from src/Project/Contributors.php rename to src/User.php index 94cbb51..a0abce1 100644 --- a/src/Project/Contributors.php +++ b/src/User.php @@ -12,15 +12,19 @@ * @copyright (C) 2023 Eric Sizemore * @license The MIT License (MIT) */ -namespace Esi\LibrariesIO\Project; +namespace Esi\LibrariesIO; use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; + +use Esi\LibrariesIO\{ + Exception\RateLimitExceededException, + AbstractBase +}; use GuzzleHttp\Exception\ClientException; use Psr\Http\Message\ResponseInterface; -use function sprintf; + +use function implode; /** * LibrariesIO - A simple API wrapper/client for the Libraries.io API. @@ -52,28 +56,46 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -final class Contributors extends AbstractBase +/** + * @psalm-api + */ +final class User extends AbstractBase { /** * {@inheritdoc} */ - public function makeRequest(array $options): ResponseInterface + public function makeRequest(string $endpoint, ?array $options = null): ResponseInterface { - // We need two options - if (!isset($options['platform'], $options['name'])) { - throw new InvalidArgumentException('$options requires 2 parameters (platform, name)'); + // Make sure we have the format and options for $endpoint + $endpointParameters = $this->endpointParameters($endpoint); + + if ($endpointParameters === []) { + throw new InvalidArgumentException( + 'Invalid endpoint specified. Must be one of: dependencies, package_contributions, packages, repositories, repository_constributions, or subscriptions' + ); + } + + $endpointOptions = $endpointParameters['options']; + + if (!parent::verifyEndpointOptions($endpointOptions, $options)) { + throw new InvalidArgumentException( + '$options has not specified all required parameters. Paremeters needed: ' . implode(', ', $endpointOptions) + . '. (login can be: username or username/repo) depending on the endpoint)' + ); } // Build query parent::makeClient([ - // Using pagination? 'page' => $options['page'] ?? 1, 'per_page' => $options['per_page'] ?? 30 ]); + // Attempt the request + $endpointParameters['format'] = parent::processEndpointFormat($endpointParameters['format'], $options); + // Attempt the request try { - return $this->client->get(sprintf('%s/%s/contributors', $options['platform'], $options['name'])); + return $this->client->get($endpointParameters['format']); } catch (ClientException $e) { if ($e->getResponse()->getStatusCode() === 429) { throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); @@ -82,4 +104,20 @@ public function makeRequest(array $options): ResponseInterface } } } + + /** + * {@inheritdoc} + */ + public function endpointParameters(string $endpoint): array + { + return match($endpoint) { + 'dependencies' => ['format' => 'github/:login/dependencies' , 'options' => ['login']], + 'package_contributions' => ['format' => 'github/:login/project-contributions' , 'options' => ['login']], + 'packages' => ['format' => 'github/:login/projects' , 'options' => ['login']], + 'repositories' => ['format' => 'github/:login/repositories' , 'options' => ['login']], + 'repository_contributions' => ['format' => 'github/:login/repository-contributions', 'options' => ['login']], + 'subscriptions' => ['format' => 'subscriptions' , 'options' => []], + default => [] + }; + } } diff --git a/src/User/Dependencies.php b/src/User/Dependencies.php deleted file mode 100644 index 1a2604a..0000000 --- a/src/User/Dependencies.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\User; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Dependencies extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // We need one option - if (!isset($options['login'])) { - throw new InvalidArgumentException('$options requires 1 parameter (login). Either: username or username/repo'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('github/%s/dependencies', $options['login'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/User/PackageContributions.php b/src/User/PackageContributions.php deleted file mode 100644 index 30fbc79..0000000 --- a/src/User/PackageContributions.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\User; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class PackageContributions extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // We need one option - if (!isset($options['login'])) { - throw new InvalidArgumentException('$options requires 1 parameter (login). Either: username or username/repo'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('github/%s/project-contributions', $options['login'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/User/Packages.php b/src/User/Packages.php deleted file mode 100644 index 83e683a..0000000 --- a/src/User/Packages.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\User; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Packages extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // We need one option - if (!isset($options['login'])) { - throw new InvalidArgumentException('$options requires 1 parameter (login). Either: username or username/repo'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('github/%s/projects', $options['login'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/User/Repositories.php b/src/User/Repositories.php deleted file mode 100644 index fe38217..0000000 --- a/src/User/Repositories.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\User; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Repositories extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // We need one option - if (!isset($options['login'])) { - throw new InvalidArgumentException('$options requires 1 parameter (login). Either: username or username/repo'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('github/%s/repositories', $options['login'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/User/RepositoryContributions.php b/src/User/RepositoryContributions.php deleted file mode 100644 index 023e930..0000000 --- a/src/User/RepositoryContributions.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\User; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class RepositoryContributions extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // We need one option - if (!isset($options['login'])) { - throw new InvalidArgumentException('$options requires 1 parameter (login). Either: username or username/repo'); - } - - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get(sprintf('github/%s/repository-contributions', $options['login'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/User/Subscriptions.php b/src/User/Subscriptions.php deleted file mode 100644 index 7302880..0000000 --- a/src/User/Subscriptions.php +++ /dev/null @@ -1,77 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\User; - -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class Subscriptions extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // Build query - parent::makeClient([ - // Using pagination? - 'page' => $options['page'] ?? 1, - 'per_page' => $options['per_page'] ?? 30 - ]); - - // Attempt the request - try { - return $this->client->get('subscriptions'); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -} diff --git a/src/User/User.php b/src/User/User.php deleted file mode 100644 index 192ceeb..0000000 --- a/src/User/User.php +++ /dev/null @@ -1,80 +0,0 @@ - - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - */ -namespace Esi\LibrariesIO\User; - -use InvalidArgumentException; -use Esi\LibrariesIO\Exception\RateLimitExceededException; -use Esi\LibrariesIO\AbstractBase; -use GuzzleHttp\Exception\ClientException; -use Psr\Http\Message\ResponseInterface; -use function sprintf; - -/** - * LibrariesIO - A simple API wrapper/client for the Libraries.io API. - * - * @author Eric Sizemore - * @package LibrariesIO - * @link https://www.secondversion.com/ - * @version 1.0.0 - * @copyright (C) 2023 Eric Sizemore - * @license The MIT License (MIT) - * - * Copyright (C) 2023 Eric Sizemore. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -final class User extends AbstractBase -{ - /** - * {@inheritdoc} - */ - public function makeRequest(?array $options = null): ResponseInterface - { - // We need one option - if (!isset($options['login'])) { - throw new InvalidArgumentException('$options requires 1 parameter (login)'); - } - - // Build query - parent::makeClient(); - - // Attempt the request - try { - return $this->client->get(sprintf('github/%s', $options['login'])); - } catch (ClientException $e) { - if ($e->getResponse()->getStatusCode() === 429) { - throw new RateLimitExceededException('Libraries.io API rate limit exceeded.', previous: $e); - } else { - throw $e; - } - } - } -}