Skip to content

Commit

Permalink
Apply clean code, include create function in readme.md and fix http p…
Browse files Browse the repository at this point in the history
…ost parameters (#55)

* Include .idea folder in gitigore

* Insert into readme.md how to create record

* Apply clean code

* Apply StyleCI
  • Loading branch information
gustavosobrinho01 committed Nov 18, 2021
1 parent e7dbd56 commit eb6aac5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage
.ac-php-conf.json
.phpunit.result.cache
.DS_Store
.idea
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ You can sort by multiple fields by calling `orderBy` more than once (a single ca
Airtable::orderBy('id')->orderBy('created_at', 'desc')->get();
```

#### Create
- Insert a record

``` php
Airtable::create(['name' => 'myName']);
```

#### First or Create
- First argument will be used for finding existing
- 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)
Expand Down
31 changes: 14 additions & 17 deletions src/Api/AirtableApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tapp\Airtable\Api;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

Expand All @@ -18,10 +19,8 @@ class AirtableApiClient implements ApiClient
private $fields = [];
private $sorts = [];
private $offset = false;
private $pageSize = 100;
private $maxRecords = 100;

public function __construct($base, $table, $access_token, $httpLogFormat = null, Http $client = null, $typecast = false, $delayBetweenRequests = 200000)
public function __construct($base, $table, $access_token, Http $client = null, $typecast = false, $delayBetweenRequests = 200000)
{
$this->base = $base;
$this->table = $table;
Expand All @@ -42,14 +41,14 @@ private function buildClient($access_token)
]);
}

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

return $this;
}

public function addSort(string $column, string $direction = 'asc')
public function addSort(string $column, string $direction = 'asc'): AirtableApiClient
{
if ($direction === 'desc') {
$this->sorts[] = ['field' => $column, 'direction' => $direction];
Expand All @@ -60,7 +59,7 @@ public function addSort(string $column, string $direction = 'asc')
return $this;
}

public function setTable($table)
public function setTable($table): AirtableApiClient
{
$this->table = $table;

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

public function getAllPages($delayBetweenRequestsInMicroseconds)
public function getAllPages($delayBetweenRequestsInMicroseconds): Collection
{
$records = [];

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

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

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

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

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

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

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

public function massUpdate(string $method, array $data)
public function massUpdate(string $method, array $data): array
{
$url = $this->getEndpointUrl();
$records = [];

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

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

public function responseToJson($response)
public function responseToJson($response): string
{
$body = (string) $response->getBody();

return $body;
return (string) $response->getBody();
}

public function responseToCollection($response)
Expand All @@ -182,7 +179,7 @@ public function decodeResponse($response)
return collect(json_decode($body, true));
}

public function setFields(?array $fields)
public function setFields(?array $fields): AirtableApiClient
{
$this->fields = $fields;

Expand Down

0 comments on commit eb6aac5

Please sign in to comment.