Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiedemaria committed Aug 26, 2024
1 parent dba99e3 commit b25a4f2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
57 changes: 32 additions & 25 deletions docs/docs-beta/docs/guides/external-systems/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ title: Connecting to APIs
sidebar_position: 20
---

This guide describes how to connect to and interact with APIs in dagster. In this guide you will use dagster Resources to connect to an external API. Using a Resource allows you to standardize how you connect to an external API across your project and use configuration to customize your connections.
When building a data pipeline, you'll likely need to connect to a number of external APIs, each with its own specific configuration and behavior. This guide demonstrates how to standardize your API connections and customize their configuration using Dagster resources.

Check warning on line 6 in docs/docs-beta/docs/guides/external-systems/apis.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Dagster.wordiness] Specify the number or remove the phrase. Raw Output: {"message": "[Dagster.wordiness] Specify the number or remove the phrase.", "location": {"path": "docs/docs-beta/docs/guides/external-systems/apis.md", "range": {"start": {"line": 6, "column": 65}}}, "severity": "INFO"}


## What you'll learn

- How to write a dagster Resource to connect to an API
- How to use that Resource in an asset
- How to configure a Resource
- How to connect to an API using a Dagster resource
- How to use that resource in an asset
- How to configure a resource
- How to source configuration values from environment variables

<details>
Expand All @@ -19,53 +19,60 @@ This guide describes how to connect to and interact with APIs in dagster. In thi
To follow the steps in this guide, you'll need:

- Familiarity with [Asset definitions](/concepts/assets)
- Familiarity with [Resources](/concepts/resources)
- Install the `requests` library: `pip install requests`
- Familiarity with [resources](/concepts/resources)
- Install the `requests` library:
```bash
pip install requests
```

</details>

## Step 1: Write a Resource to connect to an API
## Step 1: Write a resource to connect to an API

Here is a minimal Resource for connecting to the an API that returns the sunrise time. In this example, the request URL has been hardcoded to query for data at San Francisco International Airport.
Here is a minimal resource for connecting to an API that returns the sunrise time.

<CodeExample filePath="guides/external-systems/apis/minimal_resource.py" language="python" title="Resource to connect to Sun API" />
In this example, the request URL has been hardcoded to query for data at San Francisco International Airport.

Check failure on line 34 in docs/docs-beta/docs/guides/external-systems/apis.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Dagster.spelling] Is 'hardcoded' spelled correctly? Raw Output: {"message": "[Dagster.spelling] Is 'hardcoded' spelled correctly?", "location": {"path": "docs/docs-beta/docs/guides/external-systems/apis.md", "range": {"start": {"line": 34, "column": 43}}}, "severity": "ERROR"}

Check failure on line 34 in docs/docs-beta/docs/guides/external-systems/apis.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'hardcoded'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'hardcoded'?", "location": {"path": "docs/docs-beta/docs/guides/external-systems/apis.md", "range": {"start": {"line": 34, "column": 43}}}, "severity": "ERROR"}

<CodeExample filePath="guides/external-systems/apis/minimal_resource.py" language="python" title="Resource to connect to the Sun API" />

## Step 2: Use the Resource in an asset

To use the Resource written in Step 1, you can provide it to an asset like this:
## Step 2: Use the resource in an asset

<CodeExample filePath="guides/external-systems/apis/use_minimal_resource_in_asset.py" language="python" title="Use the SFOSunResource in an asset" />
To use the resource written in Step 1, you can provide it to an asset like this:

When you materialize `sfo_sunrise`, dagster will provide an initialized `SFOSunResource` to the `sun_resource` parameter.
<CodeExample filePath="guides/external-systems/apis/use_minimal_resource_in_asset.py" language="python" title="Use the SunResource in an asset" />

When you materialize `sfo_sunrise`, Dagster will provide an initialized `SunResource` to the `sun_resource` parameter.

## Step 3: Configure your Resource
Many APIs have configuration you can set to customize your usage. Here is an updated version of the Resource from Step 1 with configuration to allow for setting the query location:

<CodeExample filePath="guides/external-systems/apis/configurable_resource.py" language="python" title="Configurable Resource to connect to Sun API" />
## Step 3: Configure your resource
Many APIs have configuration you can set to customize your usage. Here is an updated version of the resource from Step 1 with configuration to allow for setting the query location:

## Step 4: Use the configurable Resource in an asset
<CodeExample filePath="guides/external-systems/apis/configurable_resource.py" language="python" title="Configurable Resource to connect to the Sun API" />

The configurable Resource written in Step 3 can be provided to an asset exactly as before. When the Resource is initialized, you can pass values for each of the configuration options.
## Step 3.1: Use the configurable resource in an asset

The configurable resource written in Step 3 can be provided to an asset exactly as before. When the resource is initialized, you can pass values for each of the configuration options.

<CodeExample filePath="guides/external-systems/apis/use_configurable_resource_in_asset.py" language="python" title="Use the configurable SunResource in an asset" />

When you materialize `sfo_sunrise`, dagster will provide a `SunResource` initialized with the provided configuration values to the `sun_resource` parameter.
When you materialize `sfo_sunrise`, Dagster will provide a `SunResource` initialized with the configuration values to the `sun_resource` parameter.


## Step 5: Sourcing configuration values from environment variables
## Step 4: Sourcing configuration values from environment variables
Resources can also be configured with environment variables, specifically values referenced using the `EnvVar` class.

You can configure your Resource using values that are stored in environment variables using the `EnvVar` class. In this example, there is a new `home_sunrise` asset. Rather than hardcoding the location of your home, you can set it in environment variables, and configure the `SunResource` by reading those values:
In this example, there is a new `home_sunrise` asset. Rather than hardcoding the location of your home, you can set it in environment variables, and configure the `SunResource` by reading those values:

Check failure on line 65 in docs/docs-beta/docs/guides/external-systems/apis.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'hardcoding'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'hardcoding'?", "location": {"path": "docs/docs-beta/docs/guides/external-systems/apis.md", "range": {"start": {"line": 65, "column": 67}}}, "severity": "ERROR"}

Check failure on line 65 in docs/docs-beta/docs/guides/external-systems/apis.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Dagster.spelling] Is 'hardcoding' spelled correctly? Raw Output: {"message": "[Dagster.spelling] Is 'hardcoding' spelled correctly?", "location": {"path": "docs/docs-beta/docs/guides/external-systems/apis.md", "range": {"start": {"line": 65, "column": 67}}}, "severity": "ERROR"}

<CodeExample filePath="guides/external-systems/apis/env_var_configuration.py" language="python" title="Configure the Resource with values from environment variables" />
<CodeExample filePath="guides/external-systems/apis/env_var_configuration.py" language="python" title="Configure the resource with values from environment variables" />

When you materialize `home_sunrise`, dagster will read the values set for the `HOME_LATITUDE`, `HOME_LONGITUDE, and `HOME_TIMZONE` environment variables and initialize a `SunResource` with those values. That initialized `SunResource` will be provided to the `sun_resource` parameter. Note that the values are read **at materialization time**, so the environment variables must be set where the code is executing.
When you materialize `home_sunrise`, Dagster will read the values set for the `HOME_LATITUDE`, `HOME_LONGITUDE, and `HOME_TIMZONE` environment variables and initialize a `SunResource` with those values. That initialized `SunResource` will be provided to the `sun_resource` parameter. Note that the values are read **at materialization time**, so the environment variables must be set where the code is executing.

Check warning on line 69 in docs/docs-beta/docs/guides/external-systems/apis.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Dagster.wordiness] Replace with **Note**: [sentence]. Raw Output: {"message": "[Dagster.wordiness] Replace with **Note**: [sentence].", "location": {"path": "docs/docs-beta/docs/guides/external-systems/apis.md", "range": {"start": {"line": 69, "column": 285}}}, "severity": "INFO"}
## Next steps
- [Authenticate to a resource](/guides/external-systems/authentication.md)
- [Use different Resources in different execution environments](/todo)
- Learn what [dagster-provided Resources](/todo) are available to use
- [Use different resources in different execution environments](/todo)
- [Set environment variables in Dagster+](/todo)
- Learn what [Dagster-provided resources](/todo) are available to use
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class SunResource(dg.ConfigurableResource):
# highlight-start
latitude: str
longitude: str
time_zone: str
Expand All @@ -12,6 +13,8 @@ class SunResource(dg.ConfigurableResource):
def query_string(self) -> str:
return f"https://api.sunrise-sunset.org/json?lat={self.latittude}&lng={self.longitude}&date=today&tzid={self.time_zone}"

# highlight-end

def sunrise(self) -> str:
data = requests.get(self.query_string, timeout=5).json()
return data["results"]["sunrise"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dagster as dg


class SFOSunResource(dg.ConfigurableResource):
class SunResource(dg.ConfigurableResource):
@property
def query_string(self) -> str:
latittude = "37.615223"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dagster as dg


class SFOSunResource(dg.ConfigurableResource):
class SunResource(dg.ConfigurableResource):
@property
def query_string(self) -> str:
latittude = "37.615223"
Expand All @@ -18,15 +18,11 @@ def sunrise(self) -> str:

# highlight-start
@dg.asset
def sfo_sunrise(
context: dg.AssetExecutionContext, sun_resource: SFOSunResource
) -> None:
def sfo_sunrise(context: dg.AssetExecutionContext, sun_resource: SunResource) -> None:
sunrise = sun_resource.sunrise()
context.log.info(f"Sunrise in San Francisco is at {sunrise}.")


defs = dg.Definitions(
assets=[sfo_sunrise], resources={"sun_resource": SFOSunResource()}
)
defs = dg.Definitions(assets=[sfo_sunrise], resources={"sun_resource": SunResource()})

# highlight-end

0 comments on commit b25a4f2

Please sign in to comment.