1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Beachcasts\Airtable; |
||
6 | |||
7 | use Assert\Assert; |
||
8 | use Beachcasts\Airtable\Middleware\BearerTokenMiddleware; |
||
9 | use GuzzleHttp\Client; |
||
10 | use GuzzleHttp\Handler\CurlHandler; |
||
11 | use GuzzleHttp\HandlerStack; |
||
12 | use GuzzleHttp\Middleware; |
||
13 | |||
14 | /** |
||
15 | * Class AirtableClient |
||
16 | * @package Beachcasts\Airtable |
||
17 | */ |
||
18 | class AirtableClient |
||
19 | { |
||
20 | /** |
||
21 | * Guzzle client object |
||
22 | * |
||
23 | * @var Client|null |
||
24 | */ |
||
25 | protected $client; |
||
26 | |||
27 | /** |
||
28 | * Airtable constructor. Create a new Airtable Instance |
||
29 | * |
||
30 | * @param Config $config |
||
31 | * @param string $baseId |
||
32 | */ |
||
33 | 4 | public function __construct(Config $config, string $baseId) |
|
34 | { |
||
35 | 4 | Assert::that($baseId) |
|
36 | 4 | ->notBlank('AirTable requires a non-empty $baseId'); |
|
37 | |||
38 | 4 | $this->client = new Client( |
|
39 | [ |
||
40 | 4 | 'base_uri' => $config->getBaseUrl() . '/' . $config->getVersion() . '/' . $baseId . '/', |
|
41 | 4 | 'handler' => $this->getBearerTokenStack($config->getApiKey()) |
|
42 | ] |
||
43 | ); |
||
44 | 4 | } |
|
45 | |||
46 | 4 | private function getBearerTokenStack(string $apiKey): HandlerStack |
|
47 | { |
||
48 | 4 | $stack = new HandlerStack(new CurlHandler()); |
|
49 | 4 | $stack->push(Middleware::mapRequest(new BearerTokenMiddleware($apiKey)), BearerTokenMiddleware::class); |
|
50 | |||
51 | 4 | return $stack; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return Client|null |
||
56 | */ |
||
57 | 2 | public function getClient(): Client |
|
58 | { |
||
59 | 2 | return $this->client; |
|
60 | } |
||
61 | |||
62 | 1 | public function getTable(string $tableName): Table |
|
63 | { |
||
64 | 1 | $table = new Table($tableName); |
|
65 | 1 | $table->setClient($this->client); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
66 | |||
67 | 1 | return $table; |
|
68 | } |
||
69 | } |
||
70 |