1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Esi\LibrariesIO. |
7
|
|
|
* |
8
|
|
|
* (c) 2023-2024 Eric Sizemore <https://github.com/ericsizemore> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view |
11
|
|
|
* the LICENSE file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Esi\LibrariesIO; |
15
|
|
|
|
16
|
|
|
use Esi\LibrariesIO\Exception\InvalidApiKeyException; |
17
|
|
|
use Esi\LibrariesIO\Exception\InvalidEndpointException; |
18
|
|
|
use Esi\LibrariesIO\Exception\RateLimitExceededException; |
19
|
|
|
use GuzzleHttp\Exception\{ |
20
|
|
|
ClientException, |
21
|
|
|
GuzzleException |
22
|
|
|
}; |
23
|
|
|
use InvalidArgumentException; |
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
25
|
|
|
use SensitiveParameter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @see \Esi\LibrariesIO\Tests\LibrariesIOTest |
29
|
|
|
*/ |
30
|
|
|
class LibrariesIO extends AbstractClient |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @param null|array<string, mixed> $clientOptions |
34
|
|
|
* |
35
|
|
|
* @throws InvalidApiKeyException |
36
|
|
|
*/ |
37
|
40 |
|
public function __construct(#[SensitiveParameter] string $apiKey, ?string $cachePath = null, ?array $clientOptions = null) |
38
|
|
|
{ |
39
|
40 |
|
parent::__construct($apiKey, $cachePath, $clientOptions); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws ClientException |
44
|
|
|
* @throws GuzzleException |
45
|
|
|
* @throws RateLimitExceededException |
46
|
|
|
*/ |
47
|
4 |
|
public function platform(): ResponseInterface |
48
|
|
|
{ |
49
|
|
|
// The only valid endpoint is 'platforms' currently |
50
|
4 |
|
return $this->request('GET', 'platforms'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Performs a request to the 'project' endpoint and a subset endpoint, which can be: |
55
|
|
|
* contributors, dependencies, dependent_repositories, dependents, search, sourcerank, or project |
56
|
|
|
* |
57
|
|
|
* @param array<string>|array<string, int> $options |
58
|
|
|
* |
59
|
|
|
* @throws InvalidEndpointException |
60
|
|
|
* @throws ClientException |
61
|
|
|
* @throws GuzzleException |
62
|
|
|
* @throws RateLimitExceededException |
63
|
|
|
*/ |
64
|
10 |
|
public function project(string $endpoint, array $options): ResponseInterface |
65
|
|
|
{ |
66
|
10 |
|
[$endpointFormat, $endpointOptions, $requestMethod] = Utils::endpointParameters('project', $endpoint, $options); |
67
|
|
|
|
68
|
8 |
|
$query = [ |
69
|
8 |
|
'page' => $options['page'] ?? 1, |
70
|
8 |
|
'per_page' => $options['per_page'] ?? 30, |
71
|
8 |
|
]; |
72
|
|
|
|
73
|
|
|
// If on the 'search' endpoint, we have to provide the query and sort parameters. |
74
|
8 |
|
if ($endpoint === 'search') { |
75
|
2 |
|
$query += [ |
76
|
2 |
|
'q' => $options['query'], |
77
|
2 |
|
'sort' => Utils::searchVerifySortOption((string) $options['sort']), |
78
|
2 |
|
]; |
79
|
|
|
|
80
|
2 |
|
$additionalParams = Utils::searchAdditionalParams($options); |
81
|
|
|
|
82
|
2 |
|
if ($additionalParams !== []) { |
83
|
2 |
|
$query += $additionalParams; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
8 |
|
$query = ['query' => $query]; |
88
|
|
|
|
89
|
8 |
|
return $this->request($requestMethod, $endpointFormat, $query); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Performs a request to the 'repository' endpoint and a subset endpoint, which can be: |
94
|
|
|
* dependencies, projects, or repository |
95
|
|
|
* |
96
|
|
|
* @param array<string>|array<string, int> $options |
97
|
|
|
* |
98
|
|
|
* @throws InvalidEndpointException |
99
|
|
|
* @throws ClientException |
100
|
|
|
* @throws GuzzleException |
101
|
|
|
* @throws RateLimitExceededException |
102
|
|
|
*/ |
103
|
8 |
|
public function repository(string $endpoint, array $options): ResponseInterface |
104
|
|
|
{ |
105
|
8 |
|
[$endpointFormat, $endpointOptions, $requestMethod] = Utils::endpointParameters('repository', $endpoint, $options); |
106
|
|
|
|
107
|
6 |
|
$query = [ |
108
|
6 |
|
'query' => [ |
109
|
6 |
|
'page' => $options['page'] ?? 1, |
110
|
6 |
|
'per_page' => $options['per_page'] ?? 30, |
111
|
6 |
|
], |
112
|
6 |
|
]; |
113
|
|
|
|
114
|
6 |
|
return $this->request($requestMethod, $endpointFormat, $query); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Performs a request to the 'subscription' endpoint and a subset endpoint, which can be: |
119
|
|
|
* subscribe, check, update, unsubscribe |
120
|
|
|
* |
121
|
|
|
* @param array<string> $options |
122
|
|
|
* |
123
|
|
|
* @throws InvalidEndpointException |
124
|
|
|
* @throws ClientException |
125
|
|
|
* @throws GuzzleException |
126
|
|
|
* @throws RateLimitExceededException |
127
|
|
|
*/ |
128
|
6 |
|
public function subscription(string $endpoint, array $options): ResponseInterface |
129
|
|
|
{ |
130
|
6 |
|
[$endpointFormat, $endpointOptions, $requestMethod] = Utils::endpointParameters('subscription', $endpoint, $options); |
131
|
|
|
|
132
|
4 |
|
if (isset($options['include_prerelease'])) { |
133
|
2 |
|
$query = ['query' => ['include_prerelease' => $options['include_prerelease']]]; |
134
|
|
|
} |
135
|
|
|
|
136
|
4 |
|
return $this->request($requestMethod, $endpointFormat, $query ?? []); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Performs a request to the 'user' endpoint and a subset endpoint, which can be: |
141
|
|
|
* dependencies, package_contributions, packages, repositories, repository_contributions, or subscriptions |
142
|
|
|
* |
143
|
|
|
* @param array<string>|array<string, int> $options |
144
|
|
|
* |
145
|
|
|
* @throws InvalidEndpointException |
146
|
|
|
* @throws ClientException |
147
|
|
|
* @throws GuzzleException |
148
|
|
|
* @throws RateLimitExceededException |
149
|
|
|
*/ |
150
|
11 |
|
public function user(string $endpoint, array $options): ResponseInterface |
151
|
|
|
{ |
152
|
11 |
|
[$endpointFormat, $endpointOptions, $requestMethod] = Utils::endpointParameters('user', $endpoint, $options); |
153
|
|
|
|
154
|
9 |
|
$query = [ |
155
|
9 |
|
'query' => [ |
156
|
9 |
|
'page' => $options['page'] ?? 1, |
157
|
9 |
|
'per_page' => $options['per_page'] ?? 30, |
158
|
9 |
|
], |
159
|
9 |
|
]; |
160
|
|
|
|
161
|
9 |
|
return $this->request($requestMethod, $endpointFormat, $query); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|