hey <@ULVA73B9P>; what's the easiest way to set HT...
# ask-marvin
p
hey @Marvin; what's the easiest way to set HTTP header parameters for the prefect client when I'm calling a flow as if I were calling a function? We're running a prefect deployment on prem, so in order to schedule any flow runs to that deplyoment, all API requests need the HTTP headers to have a special field set, and I don't see any easy way to do that in the documentation
m
To set custom HTTP header parameters for the Prefect client when calling a flow as if you were calling a function, especially for an on-premise deployment, it's typically done through environment variables or configuring the
httpx
Python library used by Prefect under the hood. However, the official documentation does not provide specific examples for setting custom headers directly. Instead, standard settings like
PREFECT_API_URL
and
PREFECT_API_KEY
can be set using the CLI or environment variables. For communication via proxies, you can set the standard
HTTPS_PROXY
and
SSL_CERT_FILE
variables to configure the
httpx
client. If you need to add a special custom header field to all API requests, here's a general approach that you can take, assuming you're comfortable modifying the underlying HTTP client: 1. Customize the
httpx
client instance used by the Prefect client to include your custom headers. 2. Define the custom headers as a dictionary:
custom_headers = {"Your-Header-Name": "Your-Header-Value"}
3. Attach these headers to the client instance. 4. Use this custom client when initiating actions with your Prefect on-premises deployment. You might have to review the Prefect client library's source code or advanced configuration settings (if any) for specifics on implementing custom headers because this is not standard documented behavior. If this does not suffice, and you still encounter difficulties or need a specific usage example, let me know, and I can help you look further into this, potentially by checking Prefect's GitHub repository for similar issues or code snippets.
p
how do I trigger a flow run using a custom client instance?
n
what do you mean by
custom client instance
?
p
I want to run my flows, using a client with custom HTTP headers
we have a deployment of prefect server that's behind a cloudflare layer thingy (sorry, I don't know what the proper terminology here is)
n
Copy code
import os

import httpx

api_url = os.getenv('PREFECT_API_URL')
headers = {
  "Authorization": f"Bearer {os.getenv('PREFECT_API_KEY')}"
}
payload = {
  "name": "my-new-flow-run-name", # not required
  #"parameters": {} only required if your flow needs params
}
deployment_id = "<my-deployment-uuid>"

async with httpx.AsyncClient() as client:
  response = await <http://client.post|client.post>(
    f"{api_url}/deployments/{deployment_id}/create_flow_run",
    headers=headers,
    json=payload
  )
  response.raise_for_status()
something like that?
p
er, I guess the use case right now is I want to run the flow locally
like, basically calling the flow like a function locally
n
ohh i see
when I'm calling a flow as if I were calling a function?
now, my bad
p
yeah sorry
let me try drawing a picture explaining our setup
n
hmm you could definitely patch this with your own subclass that uses specific headers, but i suspect there must be a better way
p
yeah that's essentially what I'm doing right now
I basically took the
get_client
function in
orchestration.py
and swapped it out with a function that adds custom headers
but that doesn't seem to fully work; it doesn't seem to create any new flow runs ata ll
crummy picture
but basically any interaction that goes from left to right needs to go through that cloudflare layer
following this: https://developers.cloudflare.com/cloudflare-one/tutorials/cli/ we need to set a header called
cf-access-token