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

Create a custom configured resource for Strava API #5

Merged
merged 4 commits into from
Dec 18, 2024

Conversation

jairus-m
Copy link
Owner

@jairus-m jairus-m commented Dec 18, 2024

Summary

Currently, the Strava API configuration is hard-coded and baked into the assets/activities.py as a function. This works if you are only developing a single pipeline for testing, but does not scale since the configured API is not modular and/or reusable.

This PR aims to create a reusable resource for the Strava API that can be used as a Dagster resource just like the dbt or duckdb resources.

Details

Utilized the ConfigurableResource class to be inherited by a new StravaAPIResource class in order to import and reuse the API across my project and to follow best practices. This object is in the resources/ directory and is used just like any other Dagster resource and inherits the same benefits.

from dagster import ConfigurableResource, asset, Definitions
import requests
from typing import Dict, Any

class StravaAPIResource(ConfigurableResource):
    client_id: str
    client_secret: str
    refresh_token: str
    _access_token: str = None

    def get_access_token(self) -> str:
        if not self._access_token:
            self._access_token = self._refresh_access_token()
        return self._access_token

    def _refresh_access_token(self) -> str:
        auth_url = "https://www.strava.com/oauth/token"
        payload = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'refresh_token': self.refresh_token,
            'grant_type': 'refresh_token'
        }
        response = requests.post(auth_url, data=payload, timeout=100)
        response.raise_for_status() 
        return response.json()['access_token']

@jairus-m jairus-m merged commit 8c624f8 into main Dec 18, 2024
3 checks passed
@jairus-m jairus-m changed the title Feat/strava resource v2 Create a custom configureed resource for Strava API Dec 21, 2024
@jairus-m jairus-m changed the title Create a custom configureed resource for Strava API Create a custom configured resource for Strava API Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant