Skip to content

Commit

Permalink
add westeros houses example using google sheets (#33)
Browse files Browse the repository at this point in the history
* 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
5 people authored Sep 13, 2024
1 parent 8526923 commit 070cd3a
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .wp-env.override.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"config": {
"REMOTE_DATA_BLOCKS_EXAMPLE_AIRTABLE_ELDEN_RING_ACCESS_TOKEN": "",
"REMOTE_DATA_BLOCKS_EXAMPLE_AIRTABLE_EVENTS_ACCESS_TOKEN": "",
"REMOTE_DATA_BLOCKS_EXAMPLE_SHOPIFY_ACCESS_TOKEN": ""
"REMOTE_DATA_BLOCKS_EXAMPLE_SHOPIFY_ACCESS_TOKEN": "",
"REMOTE_DATA_BLOCKS_EXAMPLE_GOOGLE_SHEETS_WESTEROS_HOUSES_ACCESS_TOKEN": ""
}
}
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;
}
}
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;
}
}
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',
];
}
}
39 changes: 39 additions & 0 deletions example/google-sheets/westeros-houses/register.php
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' );
2 changes: 2 additions & 0 deletions example/remote-data-blocks-example-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function load_only_if_parent_plugin_is_active() {
require_once __DIR__ . '/rest-api/art-institute/register.php';
require_once __DIR__ . '/rest-api/zip-code/register.php';
require_once __DIR__ . '/shopify/register.php';
require_once __DIR__ . '/google-sheets/westeros-houses/register.php';
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_only_if_parent_plugin_is_active', 10, 0 );

Expand All @@ -54,6 +55,7 @@ function get_access_token( string $example_name ): string {
'airtable_elden_ring',
'airtable_events',
'shopify',
'google_sheets_westeros_houses',
];

if ( ! in_array( $example_name, $supported_tokens, true ) ) {
Expand Down
Loading

0 comments on commit 070cd3a

Please sign in to comment.