<@ULVA73B9P> I have a JSON in the following format...
# ask-marvin
k
@Marvin I have a JSON in the following format:
Copy code
{
  "client_id": "<my-client-id>",
  "client_secret": "<my-client-secret>"
}
Can I use a single storage block to recall the values and pass them to a function like this:
Copy code
get_data(
    client_id=<my-client-id>,
    client_secret=<my-client-secret>
)
m
Yes, you can use a Prefect block to store and retrieve your JSON configuration, allowing you to pass the values to a function. Here's how you can approach this: 1. Create a Custom Block: Prefect allows you to define custom blocks that can store configurations like your JSON. You can create a block by subclassing
Block
from Prefect, which builds on Pydantic's
BaseModel
. This way, each field in your JSON can be declared as a field in the block. 2. Store the JSON in a Block Document: Once you have defined the block type, you can create a block document to store your JSON values. This document can be created and updated via Prefect's UI or API. 3. Retrieve and Use the Values: You can then retrieve the block document in your flow or task, and use the stored values to call your
get_data
function. Here's a basic outline of how you might define a custom block and use it:
Copy code
python
from prefect.blocks.core import Block
from pydantic import BaseModel

class MyCredentialsBlock(Block):
    client_id: str
    client_secret: str

# Save the block document
my_creds = MyCredentialsBlock(client_id="<my-client-id>", client_secret="<my-client-secret>")
my_creds.save(name="my_credentials")

# Later, retrieve and use the block
retrieved_creds = MyCredentialsBlock.load("my_credentials")
get_data(client_id=retrieved_creds.client_id, client_secret=retrieved_creds.client_secret)
For more detailed information on blocks, you can refer to the Prefect Blocks Documentation.
k
@Marvin Can I use the existing Secret block with a JSON value?
m
Yes, you can use Prefect's
SecretDict
field type to store a JSON object within a Secret block. The
SecretDict
allows you to add a dictionary field to your block that automatically obfuscates values at all levels in the UI or in logs. This is particularly useful for storing sensitive information like credentials. Here's a basic example of how you might define and use a Secret block with a JSON payload:
Copy code
python
from prefect.blocks.system import Secret
from prefect.blocks.core import Block
from pydantic import BaseModel, SecretDict

class MySecretBlock(Block):
    secrets: SecretDict

# Save the block document with your JSON
my_secret_block = MySecretBlock(secrets={"client_id": "<my-client-id>", "client_secret": "<my-client-secret>"})
my_secret_block.save(name="my_json_secrets")

# Retrieve and use the block
retrieved_secrets = MySecretBlock.load("my_json_secrets")
get_data(client_id=retrieved_secrets.secrets["client_id"], client_secret=retrieved_secrets.secrets["client_secret"])
This approach ensures that your JSON values are stored securely and can be easily retrieved for use in your flows or tasks. For more details, you can refer to the Prefect Blocks Documentation.