Repository navigation
[Async stack 5/6] Functions async orchestration support #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
andystaples
wants to merge
7
commits into
andystaples-async-stack-4-async-context
from
andystaples-async-stack-5-functions-support
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
69872f1
Add Functions coroutine orchestrator support
andystaples 1a9e454
Merge andystaples-async-stack-4-async-context into andystaples-async-…
andystaples 863e1f3
Merge andystaples-async-stack-4-async-context into andystaples-async-…
andystaples b60ade0
Merge andystaples-async-stack-4-async-context into andystaples-async-…
andystaples 80356b2
Merge andystaples-async-stack-4-async-context into andystaples-async-…
andystaples 48b6c6a
Merge andystaples-async-stack-4-async-context into andystaples-async-…
andystaples 079744f
Use the native async shape for Functions orchestrators and add call_h…
andystaples File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
azure-functions-durable/azure/durable_functions/http/call.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Durable HTTP calls from durabletask orchestration contexts.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from collections.abc import Coroutine | ||
| from typing import Any, overload | ||
|
|
||
| from durabletask import task | ||
|
|
||
| from ..internal.compat.token_source import TokenSource | ||
| from .builtin import BUILTIN_HTTP_POLL_ORCHESTRATOR_NAME | ||
| from .models import DurableHttpRequest, DurableHttpResponse, HttpRetryOptions | ||
|
|
||
|
|
||
| def _build_request( | ||
| method: str, | ||
| uri: str, | ||
| content: Any, | ||
| headers: dict[str, str] | None, | ||
| token_source: TokenSource | None, | ||
| is_raw_str: bool, | ||
| retry_options: HttpRetryOptions | None) -> dict[str, Any]: | ||
| if is_raw_str and not isinstance(content, str): | ||
| raise TypeError( | ||
| "Invalid use of 'is_raw_str' parameter: 'is_raw_str' is set to " | ||
| "'True' but 'content' is not an instance of type 'str'. Either " | ||
| "set 'is_raw_str' to 'False', or ensure your 'content' is of " | ||
| "type 'str'.") | ||
|
|
||
| json_content: str | None = None | ||
| if content is not None: | ||
| json_content = content if is_raw_str else json.dumps(content) | ||
|
|
||
| return DurableHttpRequest( | ||
| method, uri, json_content, headers, token_source, retry_options).to_json() | ||
|
|
||
|
|
||
| def schedule_http_request( | ||
| ctx: task.OrchestrationContext, | ||
| method: str, | ||
| uri: str, | ||
| content: Any = None, | ||
| headers: dict[str, str] | None = None, | ||
| token_source: TokenSource | None = None, | ||
| is_raw_str: bool = False, | ||
| retry_options: HttpRetryOptions | None = None) -> task.Task[DurableHttpResponse]: | ||
| """Schedule the built-in durable HTTP sub-orchestration on an eager context.""" | ||
| request = _build_request(method, uri, content, headers, token_source, is_raw_str, retry_options) | ||
| return ctx.call_sub_orchestrator( | ||
| BUILTIN_HTTP_POLL_ORCHESTRATOR_NAME, input=request, return_type=DurableHttpResponse) | ||
|
|
||
|
|
||
| async def _call_http_async( | ||
| ctx: task.AsyncOrchestrationContext, | ||
| method: str, | ||
| uri: str, | ||
| content: Any, | ||
| headers: dict[str, str] | None, | ||
| token_source: TokenSource | None, | ||
| is_raw_str: bool, | ||
| retry_options: HttpRetryOptions | None) -> DurableHttpResponse: | ||
| request = _build_request(method, uri, content, headers, token_source, is_raw_str, retry_options) | ||
| return await ctx.call_sub_orchestrator( | ||
| BUILTIN_HTTP_POLL_ORCHESTRATOR_NAME, input=request, return_type=DurableHttpResponse) | ||
|
|
||
|
|
||
| @overload | ||
| def call_http( | ||
| ctx: task.AsyncOrchestrationContext, | ||
| method: str, | ||
| uri: str, | ||
| *, | ||
| content: Any = None, | ||
| headers: dict[str, str] | None = None, | ||
| token_source: TokenSource | None = None, | ||
| is_raw_str: bool = False, | ||
| retry_options: HttpRetryOptions | None = None, | ||
| ) -> Coroutine[Any, Any, DurableHttpResponse]: | ||
| ... | ||
|
|
||
|
|
||
| @overload | ||
| def call_http( | ||
| ctx: task.OrchestrationContext, | ||
| method: str, | ||
| uri: str, | ||
| *, | ||
| content: Any = None, | ||
| headers: dict[str, str] | None = None, | ||
| token_source: TokenSource | None = None, | ||
| is_raw_str: bool = False, | ||
| retry_options: HttpRetryOptions | None = None, | ||
| ) -> task.Task[DurableHttpResponse]: | ||
| ... | ||
|
|
||
|
|
||
| def call_http( | ||
| ctx: task.AsyncOrchestrationContext | task.OrchestrationContext, | ||
| method: str, | ||
| uri: str, | ||
| *, | ||
| content: Any = None, | ||
| headers: dict[str, str] | None = None, | ||
| token_source: TokenSource | None = None, | ||
| is_raw_str: bool = False, | ||
| retry_options: HttpRetryOptions | None = None, | ||
| ) -> Coroutine[Any, Any, DurableHttpResponse] | task.Task[DurableHttpResponse]: | ||
| """Make a durable HTTP request from an orchestrator. | ||
|
|
||
| The request is executed by a built-in activity. If the endpoint responds | ||
| with ``202 Accepted`` and a ``Location`` header, a built-in | ||
| sub-orchestration polls that location until the operation completes, | ||
| honoring ``Retry-After``. The result is a :class:`DurableHttpResponse`; | ||
| unselected non-2xx responses are returned rather than raised. | ||
|
|
||
| With an :class:`~durabletask.task.AsyncOrchestrationContext`, this returns | ||
| a single-use coroutine for ``await``. Like other async context operations, | ||
| it is cold: validation, content serialization, and scheduling happen when | ||
| the coroutine starts. With an | ||
| :class:`~durabletask.task.OrchestrationContext`, the request is scheduled | ||
| immediately and a durable task is returned for ``yield``. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ctx: AsyncOrchestrationContext | OrchestrationContext | ||
| The orchestration context supplied to the orchestrator. | ||
| method: str | ||
| The HTTP request method. | ||
| uri: str | ||
| The HTTP request URI. | ||
| content: Any | ||
| The request content. A ``str`` is sent as JSON unless ``is_raw_str`` | ||
| is set; other values are serialized to JSON. | ||
| headers: dict[str, str] | None | ||
| The request headers. | ||
| token_source: TokenSource | None | ||
| The source of an OAuth token to add to the request. | ||
| is_raw_str: bool | ||
| When ``True``, send ``str`` content as-is instead of JSON-encoding | ||
| it. Requires ``content`` to be a ``str``. | ||
| retry_options: HttpRetryOptions | None | ||
| Opt-in durable failure retries for the initial request only. | ||
| Selected HTTP statuses and activity failures fail the call after | ||
| exhaustion. Location polls do not inherit the policy. Only enable | ||
| retries when repeating the request's side effects is safe. | ||
|
|
||
| Raises | ||
| ------ | ||
| TypeError | ||
| If ``ctx`` is not a durabletask orchestration context, or if | ||
| ``is_raw_str`` is set and ``content`` is not a ``str``. | ||
| """ | ||
| if isinstance(ctx, task.AsyncOrchestrationContext): | ||
| return _call_http_async( | ||
| ctx, method, uri, content, headers, token_source, is_raw_str, retry_options) | ||
| # Annotations are not enforced at runtime; reject other contexts clearly. | ||
| if not isinstance(ctx, task.OrchestrationContext): # pyright: ignore[reportUnnecessaryIsInstance] | ||
| raise TypeError( | ||
| "call_http requires a durabletask AsyncOrchestrationContext or OrchestrationContext. " | ||
| "Orchestrators that receive a DurableOrchestrationContext should use context.call_http().") | ||
| return schedule_http_request( | ||
| ctx, method, uri, content, headers, token_source, is_raw_str, retry_options) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.