Completed
Pull Request — master (#5)
by
unknown
09:29
created

AirtableApiClient::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Tapp\Airtable\Api;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Str;
7
8
class AirtableApiClient implements ApiClient
9
{
10
    private $client;
11
12
    private $base;
13
    private $table;
14
15
    private $filters = [];
16
    private $pageSize = 100;
17
    private $maxRecords = 100;
18
19
    public function __construct($base, $table, $access_token)
20
    {
21
        $this->base = $base;
22
        $this->table = $table;
23
        $this->client = $this->buildClient($access_token);
24
    }
25
26
    private function buildClient($access_token)
27
    {
28
        return new Client([
29
            'base_uri' => 'https://api.airtable.com',
30
            'headers' => [
31
                'Authorization' => "Bearer {$access_token}",
32
                'content-type' => 'application/json',
33
            ],
34
        ]);
35
    }
36
37
    public function where($column, $value)
38
    {
39
        $this->filters []= "{{$column}}=\"{$value}\"";
40
41
        return $this;
42
    }
43
44
    public function get(?string $id = null)
45
    {
46
        $url = $this->getEndpointUrl($id);
47
48
        return $this->jsonToObject($this->client->get($url));
49
    }
50
51
    public function getAllPages()
52
    {
53
        $url = $this->getEndpointUrl();
54
55
        $response = $this->client->get($url, [
56
            'query' => [
57
                'pageSize' => $this->pageSize,
58
                'maxRecords' => $this->maxRecords,
59
            ],
60
        ]);
61
62
        //TODO: loop through offset to get more than one page when more than 100 records exist
63
64
        return $this->jsonToObject($response);
65
    }
66
67
    public function post($contents = null)
68
    {
69
        $url = $this->getEndpointUrl();
70
71
        $params = ['json' => ['fields' => (object) $contents]];
72
73
        return $this->jsonToObject($this->client->post($url, $params));
74
    }
75
76
    public function put(string $id, $contents = null)
77
    {
78
        $url = $this->getEndpointUrl($id);
79
80
        $params = ['json' => ['fields' => (object) $contents]];
81
82
        return $this->jsonToObject($this->client->put($url, $params));
83
    }
84
85
    public function patch(string $id, $contents = null)
86
    {
87
        $url = $this->getEndpointUrl($id);
88
89
        $params = ['json' => ['fields' => (object) $contents]];
90
91
        return $this->jsonToObject($this->client->patch($url, $params));
92
    }
93
94
    public function delete(string $id)
95
    {
96
        $url = $this->getEndpointUrl($id);
97
98
        return $this->jsonToObject($this->client->delete($url));
99
    }
100
101
    public function responseToJson($response)
102
    {
103
        $body = (string) $response->getBody();
104
105
        return $body;
106
    }
107
108
    public function jsonToObject($response)
109
    {
110
        $body = (string) $response->getBody();
111
112
        if ($body === '') {
113
            return collect([]);
114
        }
115
116
        $object = json_decode($body);
117
118
        return isset($object->records) ? collect($object->records) : $object;
119
    }
120
121
    public function jsonToArray($response)
122
    {
123
        $body = (string) $response->getBody();
124
125
        if ($body === '') {
126
            return [];
127
        }
128
129
        return json_decode($body, true);
130
    }
131
132
    protected function getEndpointUrl(?string $id = null): string
133
    {
134
        if ($id) {
135
            $url = '/v0/~/~/~';
136
137
            return Str::replaceArray('~', [
138
                $this->base,
139
                $this->table,
140
                $id,
141
            ], $url);
142
        }
143
144
        $parameters = http_build_query([
145
            'filterByFormula' => implode('&', $this->filters),
146
        ]);
147
148
        $url = '/v0/~/~?~';
149
150
        return Str::replaceArray('~', [
151
            $this->base,
152
            $this->table,
153
            $parameters
154
        ], $url);
155
    }
156
}
157