Moritz Rocholl
11/29/2023, 1:31 PMMarvin
11/29/2023, 1:33 PMcache_key_fn
— a function that returns a cache key — on your task. The cache key function should take into account the inputs to the task that might affect the output. In this case, it might be the client ID and secret, or any other parameters that are used in the OAuth2 flow.
3. Use the token in other tasks. You can do this by having other tasks depend on the token task. The token will be passed as an argument to these tasks.
4. When the flow runs, Prefect will first check if a cached result exists for the token task. If it does, and the inputs to the task have not changed, Prefect will use the cached result instead of running the task again.
Please note that the token will be saved in an encrypted Prefect Secret block if you are using Prefect Cloud.
Here is a code snippet to illustrate this:
from prefect import task, Flow
@task(cache_for=timedelta(minutes=10), cache_validator='all_parameters')
def get_token(client_id, client_secret):
# Call the API and get the token
# This is just a placeholder, replace it with your actual code
token = call_api(client_id, client_secret)
return token
@task
def use_token(token):
# Use the token
# This is just a placeholder, replace it with your actual code
do_something(token)
with Flow('My Flow') as flow:
token = get_token('my_client_id', 'my_client_secret')
use_token(token)
flow.run()
In this example, the get_token
task is cached for 10 minutes. The cache_validator='all_parameters'
means that the cache will be used if all parameters to the task are the same as in a previous run.
Please replace the placeholder code with your actual code for calling the API and using the token.
For more details, you can refer to the Prefect documentation on task caching.
As for the OAuth2 token, if the repository is private, you can enter a token to access your private repository. This token will be saved in an encrypted Prefect Secret block. Creating access tokens differs for each provider. We recommend using HTTPS with fine-grained Personal Access Tokens so that you can limit access by repository. For more details, you can refer to the Prefect documentation on deployment storage guide.