Azure / azure-functions-durable-python

Python library for using the Durable Functions bindings.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: How to pass data between Azure http_trigger to FastAPI endpoint

AndreRicardo-Zoetis opened this issue · comments

How to pass a string starter from Azure http_trigger to the FastAPI endpoint?

Currently I'm hacking this by setting starter in context.trace_context.attributes

# "Client side"
def get_client(request: Request) -> df.DurableOrchestrationClient:
    starter = request.scope["azure_functions.trace_context"].attributes["starter"]
    client = df.DurableOrchestrationClient(starter)
    return client

# This is the FastAPI version of the `route="startOrchestrator"`
# `async def start_orchestrator(req: func.HttpRequest, client):`
@fastapi_app.get(path="/fast_orchestrator")
async def fast_orchestrator(
    client: df.DurableOrchestrationClient = Depends(get_client)
):
    instance_id = await client.start_new("my_orchestrator")

    reply = f"Started orchestration with ID = '{instance_id}'."
    logging.info(reply)
    # Don't know how to convert this back to Azure func.HttpRequest
    # return client.create_check_status_response(req, instance_id)

    # Here we should build the HTTP 202 with location header to the /status/{instance_id}

    return reply

# Sort of "middleware" side
@df_app.route(route="{*route}", auth_level=func.AuthLevel.ANONYMOUS)
@df_app.generic_input_binding(arg_name="starter", type="durableClient")
async def http_trigger(
    req: func.HttpRequest, context: func.Context, starter: str
) -> func.HttpResponse:
    context.trace_context.attributes["starter"] = starter
    response: func.HttpResponse = await func.AsgiMiddleware(fastapi_app).handle_async(
        req, context
    )
    # Do activity or orchestration based on response
    return response