|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Internal\DLoad\Module\Repository\Internal\GitLab\Api; |
| 6 | + |
| 7 | +use Internal\DLoad\Module\HttpClient\Factory as HttpFactory; |
| 8 | +use Internal\DLoad\Module\HttpClient\Method; |
| 9 | +use Internal\DLoad\Module\Repository\Internal\GitLab\Api\Response\ReleaseInfo; |
| 10 | +use Internal\DLoad\Module\Repository\Internal\GitLab\Api\Response\RepositoryInfo; |
| 11 | +use Internal\DLoad\Module\Repository\Internal\GitLab\Exception\GitLabRateLimitException; |
| 12 | +use Internal\DLoad\Module\Repository\Internal\Paginator; |
| 13 | +use Psr\Http\Client\ClientExceptionInterface; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | +use Psr\Http\Message\UriInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * API client for specific GitLab repository operations. |
| 19 | + * |
| 20 | + * Bound to specific owner/repo pair and provides typed methods for GitLab API operations. |
| 21 | + * |
| 22 | + * @internal |
| 23 | + * @psalm-internal Internal\DLoad\Module\Repository\Internal\GitLab |
| 24 | + */ |
| 25 | +final class RepositoryApi |
| 26 | +{ |
| 27 | + private const URL_REPOSITORY = 'https://gitlab.com/api/v4/projects/%s'; |
| 28 | + private const URL_RELEASES = 'https://gitlab.com/api/v4/projects/%s/releases'; |
| 29 | + private const URL_RELEASE_ASSET = 'https://gitlab.com/api/v4/projects/%s/releases/%s/downloads/%s'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var non-empty-string |
| 33 | + */ |
| 34 | + public readonly string $repositoryPath; |
| 35 | + |
| 36 | + public function __construct( |
| 37 | + private readonly Client $client, |
| 38 | + private readonly HttpFactory $httpFactory, |
| 39 | + string $projectPath, |
| 40 | + ) { |
| 41 | + $this->repositoryPath = $projectPath; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param non-empty-string $repositoryPath |
| 46 | + * @param non-empty-string $releaseName |
| 47 | + * @param non-empty-string $fileName |
| 48 | + * @throws GitLabRateLimitException |
| 49 | + * @throws ClientExceptionInterface |
| 50 | + */ |
| 51 | + public function downloadArtifact(string $repositoryPath, string $releaseName, string $fileName): ResponseInterface |
| 52 | + { |
| 53 | + $url = \sprintf(self::URL_RELEASE_ASSET, \urlencode($repositoryPath), $releaseName, $fileName); |
| 54 | + return $this->client->downloadArtifact($url); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param Method|non-empty-string $method |
| 59 | + * @param array<string, string> $headers |
| 60 | + * @throws GitLabRateLimitException |
| 61 | + * @throws ClientExceptionInterface |
| 62 | + */ |
| 63 | + public function request(Method|string $method, string|UriInterface $uri, array $headers = []): ResponseInterface |
| 64 | + { |
| 65 | + return $this->client->request($method, $uri, $headers); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @throws GitLabRateLimitException |
| 70 | + * @throws ClientExceptionInterface |
| 71 | + */ |
| 72 | + public function getRepository(): RepositoryInfo |
| 73 | + { |
| 74 | + $response = $this->request(Method::Get, \sprintf(self::URL_REPOSITORY, \urlencode($this->repositoryPath))); |
| 75 | + |
| 76 | + /** @var array{ |
| 77 | + * name: string, |
| 78 | + * name_with_namespace: string, |
| 79 | + * description: string|null, |
| 80 | + * web_url: string, |
| 81 | + * visibility: bool, |
| 82 | + * created_at: string, |
| 83 | + * updated_at: string |
| 84 | + * } $data */ |
| 85 | + $data = \json_decode($response->getBody()->__toString(), true, 512, JSON_THROW_ON_ERROR); |
| 86 | + |
| 87 | + return RepositoryInfo::fromApiResponse($data); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param int<1, max> $page |
| 92 | + * @return Paginator<ReleaseInfo> |
| 93 | + * @throws GitLabRateLimitException |
| 94 | + * @throws ClientExceptionInterface |
| 95 | + */ |
| 96 | + public function getReleases(int $page = 1): Paginator |
| 97 | + { |
| 98 | + $pageLoader = function () use ($page): \Generator { |
| 99 | + $currentPage = $page; |
| 100 | + |
| 101 | + do { |
| 102 | + try { |
| 103 | + $response = $this->releasesRequest($currentPage); |
| 104 | + |
| 105 | + /** @var array<array-key, array{ |
| 106 | + * name: non-empty-string|null, |
| 107 | + * tag_name: non-empty-string, |
| 108 | + * description: null|non-empty-string, |
| 109 | + * created_at: non-empty-string, |
| 110 | + * released_at: non-empty-string, |
| 111 | + * assets: array{ |
| 112 | + * links: list<array{ |
| 113 | + * name: non-empty-string, |
| 114 | + * url: non-empty-string, |
| 115 | + * direct_asset_url?: non-empty-string, |
| 116 | + * link_type: non-empty-string, |
| 117 | + * }> |
| 118 | + * }, |
| 119 | + * upcoming_release: bool |
| 120 | + * }> $data */ |
| 121 | + $data = \json_decode($response->getBody()->__toString(), true, 512, JSON_THROW_ON_ERROR); |
| 122 | + |
| 123 | + // If empty response, no more pages |
| 124 | + if ($data === []) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + $releases = []; |
| 129 | + foreach ($data as $releaseData) { |
| 130 | + try { |
| 131 | + $releases[] = ReleaseInfo::fromApiResponse($releaseData); |
| 132 | + } catch (\Throwable) { |
| 133 | + // Skip invalid releases |
| 134 | + continue; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + yield $releases; |
| 139 | + |
| 140 | + // Check if there are more pages |
| 141 | + $hasMorePages = $this->hasNextPage($response); |
| 142 | + $currentPage++; |
| 143 | + } catch (ClientExceptionInterface) { |
| 144 | + return; |
| 145 | + } |
| 146 | + } while ($hasMorePages); |
| 147 | + }; |
| 148 | + |
| 149 | + return Paginator::createFromGenerator($pageLoader(), null); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @param positive-int $page |
| 154 | + * @throws GitLabRateLimitException |
| 155 | + * @throws ClientExceptionInterface |
| 156 | + */ |
| 157 | + private function releasesRequest(int $page): ResponseInterface |
| 158 | + { |
| 159 | + return $this->request( |
| 160 | + Method::Get, |
| 161 | + $this->httpFactory->uri( |
| 162 | + \sprintf(self::URL_RELEASES, \urlencode($this->repositoryPath)), |
| 163 | + ['page' => $page], |
| 164 | + ), |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + private function hasNextPage(ResponseInterface $response): bool |
| 169 | + { |
| 170 | + $headers = $response->getHeaders(); |
| 171 | + $link = $headers['link'] ?? []; |
| 172 | + |
| 173 | + if (!isset($link[0])) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + return \str_contains($link[0], 'rel="next"'); |
| 178 | + } |
| 179 | +} |
0 commit comments