-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Airtable Integration: Extract AirtableDatasource & leverage it for bu…
…ilt-in & dynamic datasources (#17) Co-authored-by: Hew <[email protected]>
- Loading branch information
Showing
9 changed files
with
106 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
example/airtable/elden-ring-map/inc/queries/class-airtable-elden-ring-map-datasource.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
example/airtable/events/inc/queries/class-airtable-events-datasource.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
inc/integrations/airtable-integration/airtable-integration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace RemoteDataBlocks\Integrations; | ||
|
||
use RemoteDataBlocks\Config\AirtableDatasource; | ||
use RemoteDataBlocks\Logging\LoggerManager; | ||
use RemoteDataBlocks\REST\DatasourceCRUD; | ||
|
||
require_once __DIR__ . '/datasources/airtable-datasource.php'; | ||
|
||
class AirtableIntegration { | ||
public static function init(): void { | ||
self::register_dynamic_data_source_blocks(); | ||
} | ||
|
||
private static function register_dynamic_data_source_blocks(): void { | ||
$data_sources = DatasourceCRUD::get_data_sources( REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE ); | ||
|
||
foreach ( $data_sources as $config ) { | ||
// Transform data to our experimental format, which is all array based | ||
$config = array_map( | ||
function ( $value ) { | ||
return is_object( $value ) ? (array) $value : $value; | ||
}, | ||
(array) $config | ||
); | ||
self::register_blocks_for_airtable_data_source( $config ); | ||
} | ||
} | ||
|
||
private static function register_blocks_for_airtable_data_source( array $config ): void { | ||
$logger = LoggerManager::instance(); | ||
$logger->info( 'Registering Airtable block for: ' . wp_json_encode( $config ) ); // TODO: Remove this or make it debug level, etc. | ||
|
||
$base_id = $config['base']['id'] ?? ''; | ||
$table_id = $config['table']['id'] ?? ''; | ||
if ( empty( $base_id ) ) { | ||
$logger->error( 'Airtable block is missing base ID' ); | ||
} | ||
|
||
$airtable_datasource = new AirtableDatasource( $config['token'], $base_id, $table_id ); | ||
// TODO: Block registration & all the rest... This will only work if there is some mapping configured | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
inc/integrations/airtable-integration/datasources/airtable-datasource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace RemoteDataBlocks\Config; | ||
|
||
use RemoteDataBlocks\Config\HttpDatasource; | ||
|
||
class AirtableDatasource extends HttpDatasource { | ||
private $tables; | ||
|
||
public function __construct( private string $access_token, private string $base, mixed $tables ) { | ||
if ( ! is_array( $tables ) ) { | ||
$tables = [ '' => $tables ]; | ||
} | ||
$this->tables = $tables; | ||
} | ||
|
||
public function get_access_token(): string { | ||
return $this->access_token; | ||
} | ||
|
||
public function get_base(): string { | ||
return $this->base; | ||
} | ||
|
||
public function get_display_name(): string { | ||
return 'Airtable: ' . $this->get_base() . ' (' . implode( ', ', array_keys( $this->tables ) ) . ')'; | ||
} | ||
|
||
public function get_table( string $variation = '' ): string { | ||
return $this->tables[ $variation ] ?? ''; | ||
} | ||
|
||
public function get_endpoint( string $variation = '' ): string { | ||
$url = 'https://api.airtable.com/v0/' . $this->get_base(); | ||
$table = $this->get_table( $variation ); | ||
if ( $table ) { | ||
$url .= '/' . $table; | ||
} | ||
return $url; | ||
} | ||
|
||
public function get_request_headers(): array { | ||
return [ | ||
'Authorization' => sprintf( 'Bearer %s', $this->get_access_token() ), | ||
'Content-Type' => 'application/json', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters