Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data source doc #79

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/concepts/data-source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Data source

A data source defines basic reusable properties of an API and is required to define a [query](query.md).

## DatasourceInterface

At its simplest, a data source implements `DatasourceInterface` and describes itself with the following methods:

- `get_display_name(): string`: Return the display name of the data source.
- `get_image_url(): string|null`: Optionally, return an image URL that can represent the data source in UI.

## HttpDatasource

`HttpDatasource` implements `DatasourceInterface` and provides additional common reusable properties of an HTTP API:

- `get_endpoint(): string`: Returns the base URL of the API endpoint. This can be overridden by a query.
- `get_request_headers(): array`: Returns an associative array of HTTP headers to be sent with each request. This is a common place to set authentication headers such as `Authorization`. This array can be extended or overridden by a query.

## Example

Most HTTP-powered APIs can be represented by extending `HttpDatasource`. Here's an example of a data source for US ZIP code data:

```php
class ZipCodeDatasource extends HttpDatasource {
public function get_display_name(): string {
return 'US ZIP codes';
}

public function get_endpoint(): string {
return 'https://api.zippopotam.us/us/';
}

public function get_request_headers(): array {
return [
'Content-Type' => 'application/json',
];
}
}
```

## Custom data source

APIs that do not use HTTP as transport may require a custom data source. Implement `DatasourceInterface` and provide additional methods that define reusable properties of your API. The actual implementation of your transport will likely be provided extending `QueryRunner`.

```php
class WebDavDatasource implements DatasourceInterface {
public function get_display_name(): string {
return 'WebDAV Files';
}

public function get_image_url(): null {
return null;
}
chriszarate marked this conversation as resolved.
Show resolved Hide resolved

public function get_webdav_root(): string {
return 'webdavs://webdav.example.com/';
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace RemoteDataBlocks\Config\Datasource;

/**
* HttpDatasource class
* CodedHttpDatasource class
*
* Implements the HttpDatasourceInterface to define a generic HTTP datasource.
* Extends HttpDatasource to define a HTTP datasource that can be displayed in
* the plugin settings.
*
* @package remote-data-blocks
* @since 0.1.0
*/
abstract class CompatibleHttpDatasource extends HttpDatasource {
abstract class CodedHttpDatasource extends HttpDatasource {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/**
* Provide object representations of the data source for display in plugin
* settings. This allows the data sources defined in code to be viewed without
Expand Down
4 changes: 2 additions & 2 deletions inc/Editor/BlockManagement/ConfigStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use RemoteDataBlocks\Logging\LoggerManager;
use Psr\Log\LoggerInterface;
use RemoteDataBlocks\Config\Datasource\CompatibleHttpDatasource;
use RemoteDataBlocks\Config\Datasource\CodedHttpDatasource;
use RemoteDataBlocks\Config\QueryContext\HttpQueryContext;

use function sanitize_title;
Expand Down Expand Up @@ -96,7 +96,7 @@ public static function get_compatible_data_sources(): array {

$data_source = $query->get_datasource();

if ( ! $data_source instanceof CompatibleHttpDatasource ) {
if ( ! $data_source instanceof CodedHttpDatasource ) {
continue;
}

Expand Down
15 changes: 4 additions & 11 deletions inc/ExampleApi/Queries/ExampleApiDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace RemoteDataBlocks\ExampleApi\Queries;

use RemoteDataBlocks\Config\Datasource\HttpDatasource;
use RemoteDataBlocks\Config\Datasource\DatasourceInterface;

/**
* This is a placeholder datasource used only to represent the data source in the
* settings UI. The actual data loading is implemented by ExampleApiQueryRunner.
*/
class ExampleApiDataSource extends HttpDatasource {
class ExampleApiDataSource implements DatasourceInterface {
Copy link
Contributor

@mhsdef mhsdef Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

Yeah, that's the gist of it. Adds flexibility in aligning permutations of abstractions closer to their realities.

/**
* @inheritDoc
*/
Expand All @@ -19,14 +19,7 @@ public function get_display_name(): string {
/**
* @inheritDoc
*/
public function get_endpoint(): string {
return 'https://example.com/api/v1';
}

/**
* @inheritDoc
*/
public function get_request_headers(): array {
return [];
public function get_image_url(): null {
return null;
}
}
5 changes: 2 additions & 3 deletions inc/Integrations/Airtable/AirtableDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace RemoteDataBlocks\Integrations\Airtable;

use RemoteDataBlocks\Config\Datasource\CompatibleHttpDatasource;
use function sanitize_title;
use RemoteDataBlocks\Config\Datasource\CodedHttpDatasource;

class AirtableDatasource extends CompatibleHttpDatasource {
class AirtableDatasource extends CodedHttpDatasource {
private $tables;

public function __construct( private string $access_token, private string $base, mixed $tables ) {
Expand Down
6 changes: 3 additions & 3 deletions inc/Integrations/Shopify/ShopifyDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace RemoteDataBlocks\Integrations\Shopify;

use RemoteDataBlocks\Config\Datasource\CompatibleHttpDatasource;
use RemoteDataBlocks\Config\Datasource\CodedHttpDatasource;

use function plugins_url;

defined( 'ABSPATH' ) || exit();

class ShopifyDatasource extends CompatibleHttpDatasource {
class ShopifyDatasource extends CodedHttpDatasource {
public function __construct( private string $access_token, private string $store_name ) {}

public function get_store_name(): string {
Expand All @@ -18,7 +18,7 @@ public function get_store_name(): string {
public function get_display_name(): string {
return 'Shopify (' . $this->store_name . ')';
}

public function get_endpoint(): string {
return 'https://' . $this->store_name . '.myshopify.com/api/2024-04/graphql.json';
}
Expand Down