1 | <?php |
||
2 | |||
3 | namespace NavigaAdClient; |
||
4 | |||
5 | use Illuminate\Http\Client\PendingRequest; |
||
6 | use Illuminate\Support\Facades\Http; |
||
7 | use NavigaAdClient\Requests\PaginatedRequest; |
||
8 | |||
9 | class NavigaAdApi |
||
10 | { |
||
11 | protected PendingRequest $client; |
||
12 | |||
13 | 2 | public function __construct( |
|
14 | array $credentials, |
||
15 | string $baseUrl, |
||
16 | array $options = [], |
||
17 | ) { |
||
18 | 2 | $client = Http::baseUrl(rtrim($baseUrl, '/').'/'); |
|
19 | |||
20 | 2 | if (isset($credentials['username']) && isset($credentials['password'])) { |
|
21 | 2 | $client->withBasicAuth($credentials['username'], $credentials['password']); |
|
22 | } elseif (isset($credentials['bearer'])) { |
||
23 | $client->withToken($credentials['bearer']); |
||
24 | } |
||
25 | |||
26 | 2 | if (isset($options['guzzle'])) { |
|
27 | 2 | $client->withOptions($options['guzzle']); |
|
28 | } |
||
29 | |||
30 | 2 | if (isset($options['retry'])) { |
|
31 | 2 | $client->retry(...$options['retry']); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
32 | } |
||
33 | |||
34 | 2 | if (isset($options['timeout'])) { |
|
35 | 2 | $client->timeout($options['timeout']); |
|
36 | } |
||
37 | |||
38 | 2 | if (isset($options['headers'])) { |
|
39 | 2 | $client->withHeaders($options['headers']); |
|
40 | } |
||
41 | |||
42 | 2 | $this->client = $client; |
|
43 | } |
||
44 | |||
45 | 2 | public function pendingRequest(): PendingRequest |
|
46 | { |
||
47 | 2 | return clone $this->client; |
|
48 | } |
||
49 | |||
50 | 1 | public function paginatedRequest( |
|
51 | string $endpoint, |
||
52 | int $perPage = 20, |
||
53 | int $currentPage = 1 |
||
54 | ): PaginatedRequest { |
||
55 | 1 | return new PaginatedRequest($this, $endpoint, $perPage, $currentPage); |
|
56 | } |
||
57 | } |
||
58 |