Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions projects/fal_client/src/fal_client/_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,17 @@ def add_priority_header(priority: Priority, headers: dict[str, str]) -> None:
f"Priority must be one of {valid_priorities}, got '{priority}'"
)
headers[QUEUE_PRIORITY_HEADER] = priority


def add_forwarded_headers(request_headers, _headers: dict[str, str]) -> None:
if cdn_token := request_headers.get("x-fal-cdn-token"):
_headers["x-fal-forwarded-cdn-token"] = cdn_token

forwarded_request_ids = []
if request_ids := request_headers.get("x-fal-forwarded-request-id"):
forwarded_request_ids.extend(request_ids.split(","))
if request_id := request_headers.get("x-fal-request-id"):
forwarded_request_ids.append(request_id)

if forwarded_request_ids:
_headers["x-fal-forwarded-request-id"] = ",".join(forwarded_request_ids)
21 changes: 21 additions & 0 deletions projects/fal_client/src/fal_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
add_hint_header,
REQUEST_TIMEOUT_TYPE_HEADER,
REQUEST_TIMEOUT_HEADER,
add_forwarded_headers,
)

if TYPE_CHECKING:
Expand All @@ -62,6 +63,14 @@
if TYPE_CHECKING:
from PIL import Image

try:
from fal.ref import get_current_app
except ImportError:

def get_current_app() -> Optional[Any]:
return None


AnyJSON = Dict[str, Any]
UploadRepositoryId = Literal["fal_v3", "cdn", "fal"]

Expand Down Expand Up @@ -1568,6 +1577,9 @@ async def run(
if start_timeout is not None:
add_timeout_header(start_timeout, _headers)

if (app := get_current_app()) is not None and app.current_request is not None:
add_forwarded_headers(app.current_request.headers, _headers)

response = await _async_maybe_retry_request(
self._client,
"POST",
Expand Down Expand Up @@ -1621,6 +1633,9 @@ async def submit(
if start_timeout is not None:
add_timeout_header(start_timeout, _headers)

if (app := get_current_app()) is not None and app.current_request is not None:
add_forwarded_headers(app.current_request.headers, _headers)

response = await _async_maybe_retry_request(
self._client,
"POST",
Expand Down Expand Up @@ -2064,6 +2079,9 @@ def run(
if start_timeout is not None:
add_timeout_header(start_timeout, _headers)

if (app := get_current_app()) is not None and app.current_request is not None:
add_forwarded_headers(app.current_request.headers, _headers)

response = _maybe_retry_request(
self._client,
"POST",
Expand Down Expand Up @@ -2113,6 +2131,9 @@ def submit(
if start_timeout is not None:
add_timeout_header(start_timeout, _headers)

if (app := get_current_app()) is not None and app.current_request is not None:
add_forwarded_headers(app.current_request.headers, _headers)

response = _maybe_retry_request(
self._client,
"POST",
Expand Down