diff --git a/docs/docs-beta/docs/guides/external-systems/apis.md b/docs/docs-beta/docs/guides/external-systems/apis.md index d07e36d99bfa8..3c046bc88ab29 100644 --- a/docs/docs-beta/docs/guides/external-systems/apis.md +++ b/docs/docs-beta/docs/guides/external-systems/apis.md @@ -3,13 +3,15 @@ title: Connecting to APIs sidebar_position: 20 --- -This guide describes how to connect to and interact with APIs in dagster. +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, use configuration to customize your connections, and use difference implementations of a Resource for different environments. ## 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 source configuration values from environment variables
Prerequisites @@ -47,6 +49,13 @@ The configurable Resource written in Step 3 can be provided to an asset exactly +## Step 5: Sourcing configuration values from environment variables + +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: + + + + ## Next steps - [Authenticate to a resource](/guides/external-systems/authentication.md) diff --git a/examples/docs_beta_snippets/docs_beta_snippets/guides/external-systems/apis/env_var_configuration.py b/examples/docs_beta_snippets/docs_beta_snippets/guides/external-systems/apis/env_var_configuration.py new file mode 100644 index 0000000000000..0cff2890f7af5 --- /dev/null +++ b/examples/docs_beta_snippets/docs_beta_snippets/guides/external-systems/apis/env_var_configuration.py @@ -0,0 +1,38 @@ +import requests + +import dagster as dg + + +class SunResource(dg.ConfigurableResource): + latitude: float + longitude: float + time_zone: str + + @property + def query_string(self) -> str: + return f"https://api.sunrise-sunset.org/json?lat={self.latitude}&lng={self.longitude}&date=today&tzid={self.time_zone}" + + def sunrise(self) -> str: + data = requests.get(self.query_string, timeout=5).json() + return data["results"]["sunrise"] + + +# highlight-start +@dg.asset +def home_sunrise(context: dg.AssetExecutionContext, sun_resource: SunResource) -> None: + sunrise = sun_resource.sunrise() + context.log.info(f"Sunrise at home is at {sunrise}.") + + +defs = dg.Definitions( + assets=[home_sunrise], + resources={ + "sun_resource": SunResource( + latitude=float(dg.EnvVar("HOME_LATITUDE")), + longitude=float(dg.EnvVar("HOME_LONGITUDE")), + time_zone=dg.EnvVar("HOME_TIMEZONE"), + ) + }, +) + +# highlight-end