-
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.
add westeros houses example using google sheets (#33)
* add westeros houses example using google sheets * skip cache when fetching google token via rest API * use cache-group when reading token from cache * update westeros houses block name * use HttpQueryContext instead of QueryContext * use snake-case for rowId * add new Google Sheets variable to .wp-env.override.example.json * move google-auth logic to integrations dir * add doc comments --------- Co-authored-by: Max Schmeling <[email protected]> Co-authored-by: Max Schmeling <[email protected]> Co-authored-by: Chris Zarate <[email protected]> Co-authored-by: Hew <[email protected]>
- Loading branch information
1 parent
8526923
commit 070cd3a
Showing
11 changed files
with
334 additions
and
11 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
81 changes: 81 additions & 0 deletions
81
example/google-sheets/westeros-houses/inc/queries/class-get-westeros-houses-query.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,81 @@ | ||
<?php | ||
|
||
namespace RemoteDataBlocks\Example\GoogleSheets\WesterosHouses; | ||
|
||
use RemoteDataBlocks\Config\HttpQueryContext; | ||
|
||
class GetWesterosHousesQuery extends HttpQueryContext { | ||
const COLUMNS = [ | ||
'House', | ||
'Seat', | ||
'Region', | ||
'Words', | ||
'Sigil', | ||
]; | ||
|
||
public array $input_variables = [ | ||
'row_id' => [ | ||
'name' => 'Row ID', | ||
'overrides' => [ | ||
[ | ||
'target' => 'utm_content', | ||
'type' => 'query_var', | ||
], | ||
], | ||
'type' => 'id', | ||
], | ||
]; | ||
|
||
public array $output_variables = [ | ||
'is_collection' => false, | ||
'mappings' => [ | ||
'row_id' => [ | ||
'name' => 'Row ID', | ||
'path' => '$.RowId', | ||
'type' => 'id', | ||
], | ||
'house' => [ | ||
'name' => 'House', | ||
'path' => '$.House', | ||
'type' => 'string', | ||
], | ||
'seat' => [ | ||
'name' => 'Seat', | ||
'path' => '$.Seat', | ||
'type' => 'string', | ||
], | ||
'region' => [ | ||
'name' => 'Region', | ||
'path' => '$.Region', | ||
'type' => 'string', | ||
], | ||
'words' => [ | ||
'name' => 'Words', | ||
'path' => '$.Words', | ||
'type' => 'string', | ||
], | ||
'image_url' => [ | ||
'name' => 'Sigil', | ||
'path' => '$.Sigil', | ||
'type' => 'image_url', | ||
], | ||
], | ||
]; | ||
|
||
public function process_response( string $raw_response_data, array $input_variables ): string|array|object|null { | ||
$parsed_response_data = json_decode( $raw_response_data, true ); | ||
$selected_row = null; | ||
$row_id = $input_variables['row_id']; | ||
|
||
if ( isset( $parsed_response_data['values'] ) && is_array( $parsed_response_data['values'] ) ) { | ||
$raw_selected_row = $parsed_response_data['values'][ $row_id ]; | ||
if ( is_array( $raw_selected_row ) ) { | ||
$selected_row = array_combine( self::COLUMNS, $raw_selected_row ); | ||
$selected_row = array_combine( self::COLUMNS, $selected_row ); | ||
$selected_row['RowId'] = $row_id; | ||
} | ||
} | ||
|
||
return $selected_row; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
example/google-sheets/westeros-houses/inc/queries/class-list-westeros-houses-query.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,75 @@ | ||
<?php | ||
|
||
namespace RemoteDataBlocks\Example\GoogleSheets\WesterosHouses; | ||
|
||
use RemoteDataBlocks\Config\HttpQueryContext; | ||
|
||
class ListWesterosHousesQuery extends HttpQueryContext { | ||
const COLUMNS = [ | ||
'House', | ||
'Seat', | ||
'Region', | ||
'Words', | ||
'Sigil', | ||
]; | ||
|
||
public array $input_variables = []; | ||
|
||
public array $output_variables = [ | ||
'root_path' => '$.values[*]', | ||
'is_collection' => true, | ||
'mappings' => [ | ||
'row_id' => [ | ||
'name' => 'Row ID', | ||
'path' => '$.RowId', | ||
'type' => 'id', | ||
], | ||
'house' => [ | ||
'name' => 'House', | ||
'path' => '$.House', | ||
'type' => 'string', | ||
], | ||
'seat' => [ | ||
'name' => 'Seat', | ||
'path' => '$.Seat', | ||
'type' => 'string', | ||
], | ||
'region' => [ | ||
'name' => 'Region', | ||
'path' => '$.Region', | ||
'type' => 'string', | ||
], | ||
'words' => [ | ||
'name' => 'Words', | ||
'path' => '$.Words', | ||
'type' => 'string', | ||
], | ||
'image_url' => [ | ||
'name' => 'Sigil', | ||
'path' => '$.Sigil', | ||
'type' => 'image_url', | ||
], | ||
], | ||
]; | ||
|
||
public function process_response( string $raw_response_data, array $input_variables ): string|array|object|null { | ||
$parsed_response_data = json_decode( $raw_response_data, true ); | ||
|
||
if ( isset( $parsed_response_data['values'] ) && is_array( $parsed_response_data['values'] ) ) { | ||
$values = $parsed_response_data['values']; | ||
array_shift( $values ); // Drop the first row | ||
|
||
$parsed_response_data['values'] = array_map( | ||
function ( $row, $index ) { | ||
$combined = array_combine( self::COLUMNS, $row ); | ||
$combined['RowId'] = $index + 1; // Add row_id field, starting from 1 | ||
return $combined; | ||
}, | ||
$values, | ||
array_keys( $values ) | ||
); | ||
} | ||
|
||
return $parsed_response_data; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
example/google-sheets/westeros-houses/inc/queries/class-westeros-houses-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,39 @@ | ||
<?php | ||
|
||
namespace RemoteDataBlocks\Example\GoogleSheets\WesterosHouses; | ||
|
||
use RemoteDataBlocks\Config\HttpDatasource; | ||
use RemoteDataBlocks\Integrations\Google\Auth\GoogleAuth; | ||
|
||
class WesterosHousesDatasource extends HttpDatasource { | ||
private array $credentials; | ||
|
||
public function __construct( string $credentials ) { | ||
/** | ||
* Decodes Base64 encoded JSON string into an array | ||
* and assigns it to the $credentials property. | ||
*/ | ||
$this->credentials = json_decode( base64_decode( $credentials ), true ); | ||
} | ||
|
||
public function get_endpoint(): string { | ||
return 'https://sheets.googleapis.com/v4/spreadsheets/' . | ||
'1EHdQg53Doz0B-ImrGz_hTleYeSvkVIk_NSJCOM1FQk0/values/Houses'; | ||
} | ||
|
||
public function get_display_name(): string { | ||
return 'Westeros Houses'; | ||
} | ||
|
||
public function get_request_headers(): array { | ||
$access_token = GoogleAuth::generate_token_from_service_account_key( | ||
$this->credentials, | ||
GoogleAuth::GOOGLE_SHEETS_SCOPES | ||
); | ||
|
||
return [ | ||
'Authorization' => sprintf( 'Bearer %s', $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace RemoteDataBlocks\Example\GoogleSheets\WesterosHouses; | ||
|
||
use RemoteDataBlocks\Editor\ConfigurationLoader; | ||
use RemoteDataBlocks\Logging\LoggerManager; | ||
use function add_action; | ||
|
||
require_once __DIR__ . '/inc/queries/class-westeros-houses-datasource.php'; | ||
require_once __DIR__ . '/inc/queries/class-list-westeros-houses-query.php'; | ||
require_once __DIR__ . '/inc/queries/class-get-westeros-houses-query.php'; | ||
|
||
function register_westeros_houses_block() { | ||
$block_name = 'Westeros House'; | ||
$access_token = \RemoteDataBlocks\Example\get_access_token( 'google_sheets_westeros_houses' ); | ||
|
||
if ( empty( $access_token ) ) { | ||
$logger = LoggerManager::instance(); | ||
$logger->warning( | ||
sprintf( | ||
'%s is not defined, cannot register %s block', | ||
'EXAMPLE_GOOGLE_SHEETS_WESTEROS_HOUSES_ACCESS_TOKEN', | ||
$block_name | ||
) | ||
); | ||
return; | ||
} | ||
|
||
$westeros_houses_datasource = new WesterosHousesDatasource( $access_token ); | ||
$list_westeros_houses_query = new ListWesterosHousesQuery( $westeros_houses_datasource ); | ||
$get_westeros_houses_query = new GetWesterosHousesQuery( $westeros_houses_datasource ); | ||
|
||
ConfigurationLoader::register_block( $block_name, $get_westeros_houses_query ); | ||
ConfigurationLoader::register_list_query( $block_name, $list_westeros_houses_query ); | ||
ConfigurationLoader::register_loop_block( 'Westeros Houses List', $list_westeros_houses_query ); | ||
ConfigurationLoader::register_page( $block_name, 'westeros-houses' ); | ||
} | ||
|
||
add_action( 'register_remote_data_blocks', __NAMESPACE__ . '\\register_westeros_houses_block' ); |
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
Oops, something went wrong.