Skip to content

Commit eb6aac5

Browse files
Apply clean code, include create function in readme.md and fix http post parameters (#55)
* Include .idea folder in gitigore * Insert into readme.md how to create record * Apply clean code * Apply StyleCI
1 parent e7dbd56 commit eb6aac5

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ coverage
77
.ac-php-conf.json
88
.phpunit.result.cache
99
.DS_Store
10+
.idea

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ You can sort by multiple fields by calling `orderBy` more than once (a single ca
108108
Airtable::orderBy('id')->orderBy('created_at', 'desc')->get();
109109
```
110110

111+
#### Create
112+
- Insert a record
113+
114+
``` php
115+
Airtable::create(['name' => 'myName']);
116+
```
117+
111118
#### First or Create
112119
- First argument will be used for finding existing
113120
- Second argument is additional data to save if no results are found and we are creating (will not be saved used if item already exists)

src/Api/AirtableApiClient.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tapp\Airtable\Api;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Facades\Http;
67
use Illuminate\Support\Str;
78

@@ -18,10 +19,8 @@ class AirtableApiClient implements ApiClient
1819
private $fields = [];
1920
private $sorts = [];
2021
private $offset = false;
21-
private $pageSize = 100;
22-
private $maxRecords = 100;
2322

24-
public function __construct($base, $table, $access_token, $httpLogFormat = null, Http $client = null, $typecast = false, $delayBetweenRequests = 200000)
23+
public function __construct($base, $table, $access_token, Http $client = null, $typecast = false, $delayBetweenRequests = 200000)
2524
{
2625
$this->base = $base;
2726
$this->table = $table;
@@ -42,14 +41,14 @@ private function buildClient($access_token)
4241
]);
4342
}
4443

45-
public function addFilter($column, $operation, $value)
44+
public function addFilter($column, $operation, $value): AirtableApiClient
4645
{
4746
$this->filters[] = "{{$column}}{$operation}\"{$value}\"";
4847

4948
return $this;
5049
}
5150

52-
public function addSort(string $column, string $direction = 'asc')
51+
public function addSort(string $column, string $direction = 'asc'): AirtableApiClient
5352
{
5453
if ($direction === 'desc') {
5554
$this->sorts[] = ['field' => $column, 'direction' => $direction];
@@ -60,7 +59,7 @@ public function addSort(string $column, string $direction = 'asc')
6059
return $this;
6160
}
6261

63-
public function setTable($table)
62+
public function setTable($table): AirtableApiClient
6463
{
6564
$this->table = $table;
6665

@@ -74,7 +73,7 @@ public function get(?string $id = null)
7473
return $this->decodeResponse($this->client->get($url));
7574
}
7675

77-
public function getAllPages($delayBetweenRequestsInMicroseconds)
76+
public function getAllPages($delayBetweenRequestsInMicroseconds): Collection
7877
{
7978
$records = [];
8079

@@ -100,7 +99,7 @@ public function post($contents = null)
10099
{
101100
$url = $this->getEndpointUrl();
102101

103-
$params = ['json' => ['fields' => (object) $contents, 'typecast' => $this->typecast]];
102+
$params = ['fields' => (object) $contents, 'typecast' => $this->typecast];
104103

105104
return $this->decodeResponse($this->client->post($url, $params));
106105
}
@@ -109,7 +108,7 @@ public function put(string $id, $contents = null)
109108
{
110109
$url = $this->getEndpointUrl($id);
111110

112-
$params = ['json' => ['fields' => (object) $contents, 'typecast' => $this->typecast]];
111+
$params = ['fields' => (object) $contents, 'typecast' => $this->typecast];
113112

114113
return $this->decodeResponse($this->client->put($url, $params));
115114
}
@@ -118,20 +117,20 @@ public function patch(string $id, $contents = null)
118117
{
119118
$url = $this->getEndpointUrl($id);
120119

121-
$params = ['json' => ['fields' => (object) $contents, 'typecast' => $this->typecast]];
120+
$params = ['fields' => (object) $contents, 'typecast' => $this->typecast];
122121

123122
return $this->decodeResponse($this->client->patch($url, $params));
124123
}
125124

126-
public function massUpdate(string $method, array $data)
125+
public function massUpdate(string $method, array $data): array
127126
{
128127
$url = $this->getEndpointUrl();
129128
$records = [];
130129

131130
// Update & Patch request body can include an array of up to 10 record objects
132131
$chunks = array_chunk($data, 10);
133132
foreach ($chunks as $key => $data_chunk) {
134-
$params = ['json' => ['records' => $data_chunk, 'typecast' => $this->typecast]];
133+
$params = ['records' => $data_chunk, 'typecast' => $this->typecast];
135134

136135
$response = $this->decodeResponse($this->client->$method($url, $params));
137136
$records += $response['records'];
@@ -151,11 +150,9 @@ public function delete(string $id)
151150
return $this->decodeResponse($this->client->delete($url));
152151
}
153152

154-
public function responseToJson($response)
153+
public function responseToJson($response): string
155154
{
156-
$body = (string) $response->getBody();
157-
158-
return $body;
155+
return (string) $response->getBody();
159156
}
160157

161158
public function responseToCollection($response)
@@ -182,7 +179,7 @@ public function decodeResponse($response)
182179
return collect(json_decode($body, true));
183180
}
184181

185-
public function setFields(?array $fields)
182+
public function setFields(?array $fields): AirtableApiClient
186183
{
187184
$this->fields = $fields;
188185

0 commit comments

Comments
 (0)