Skip to content

Commit

Permalink
Airtable Integration: Extract AirtableDatasource & leverage it for bu…
Browse files Browse the repository at this point in the history
…ilt-in & dynamic datasources (#17)

Co-authored-by: Hew <[email protected]>
  • Loading branch information
jblz and mhsdef authored Sep 13, 2024
1 parent 070cd3a commit 4d592df
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ class AirtableEldenRingListLocationsQuery extends HttpQueryContext {
];

public function get_endpoint( array $input_variables ): string {
return $this->get_datasource()->get_endpoint() . '/' . AirtableEldenRingMapDatasource::LOCATIONS_TABLE . '?filterByFormula=FIND%28%27' . $input_variables['map_name'] . '%27%2C%20%7BMap%7D%29%3E0';
return $this->get_datasource()->get_endpoint( 'locations' ) . '?filterByFormula=FIND%28%27' . $input_variables['map_name'] . '%27%2C%20%7BMap%7D%29%3E0';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AirtableEldenRingListMapsQuery extends HttpQueryContext {
];

public function get_endpoint( $input_variables ): string {
return $this->get_datasource()->get_endpoint() . '/' . AirtableEldenRingMapDatasource::MAPS_TABLE;
return $this->get_datasource()->get_endpoint( 'maps' );
}

public function get_query_name(): string {
Expand Down

This file was deleted.

9 changes: 7 additions & 2 deletions example/airtable/elden-ring-map/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@

namespace RemoteDataBlocks\Example\Airtable\EldenRingMap;

use RemoteDataBlocks\Config\AirtableDatasource;
use RemoteDataBlocks\Editor\ConfigurationLoader;
use RemoteDataBlocks\Logging\LoggerManager;
use function register_block_type;
use function wp_register_script;
use function wp_register_style;

require_once __DIR__ . '/inc/queries/class-airtable-elden-ring-map-datasource.php';
require_once __DIR__ . '/inc/queries/class-airtable-elden-ring-list-locations-query.php';
require_once __DIR__ . '/inc/queries/class-airtable-elden-ring-list-maps-query.php';

function register_airtable_elden_ring_map_block() {
$block_name = 'Elden Ring Location';
$access_token = \RemoteDataBlocks\Example\get_access_token( 'airtable_elden_ring' );
$base = 'appqI3sJ9R2NcML8Y';
$tables = [
'maps' => 'tblS3OYo8tZOg04CP',
'locations' => 'tblc82R9msH4Yh6ZX',
];

if ( empty( $access_token ) ) {
$logger = LoggerManager::instance();
$logger->warning( sprintf( '%s is not defined, cannot register %s block', 'EXAMPLE_AIRTABLE_ELDEN_RING_ACCESS_TOKEN', $block_name ) );
return;
}

$elden_ring_datasource = new AirtableEldenRingMapDatasource( $access_token );
$elden_ring_datasource = new AirtableDatasource( $access_token, $base, $tables );
$list_locations_query = new AirtableEldenRingListLocationsQuery( $elden_ring_datasource );
$list_maps_query = new AirtableEldenRingListMapsQuery( $elden_ring_datasource );

Expand Down

This file was deleted.

6 changes: 4 additions & 2 deletions example/airtable/events/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@

namespace RemoteDataBlocks\Example\Airtable\Events;

use RemoteDataBlocks\Config\AirtableDatasource;
use RemoteDataBlocks\Editor\ConfigurationLoader;
use RemoteDataBlocks\Logging\LoggerManager;
use function add_action;

require_once __DIR__ . '/inc/queries/class-airtable-events-datasource.php';
require_once __DIR__ . '/inc/queries/class-airtable-get-event-query.php';
require_once __DIR__ . '/inc/queries/class-airtable-list-events-query.php';

function register_airtable_events_block() {
$block_name = 'Airtable Event';
$access_token = \RemoteDataBlocks\Example\get_access_token( 'airtable_events' );
$base = 'appVQ2PAl95wQSo9S';
$table = 'tblyGtuxblLtmoqMI';

if ( empty( $access_token ) ) {
$logger = LoggerManager::instance();
$logger->warning( sprintf( '%s is not defined, cannot register %s block', 'EXAMPLE_AIRTABLE_EVENTS_ACCESS_TOKEN', $block_name ) );
return;
}

$airtable_datasource = new AirtableEventsDatasource( $access_token );
$airtable_datasource = new AirtableDatasource( $access_token, $base, $table );
$airtable_get_event_query = new AirtableGetEventQuery( $airtable_datasource );
$airtable_list_events_query = new AirtableListEventsQuery( $airtable_datasource );

Expand Down
44 changes: 44 additions & 0 deletions inc/integrations/airtable-integration/airtable-integration.php
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
}
}
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',
];
}
}
1 change: 1 addition & 0 deletions remote-data-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
PluginSettings::init();

// Integrations
Integrations\AirtableIntegration::init();
Integrations\ShopifyIntegration::init();
Integrations\VipBlockDataApi::init();

Expand Down

0 comments on commit 4d592df

Please sign in to comment.