Skip to content

Commit ab01fb5

Browse files
committed
initial commit
1 parent 55b597f commit ab01fb5

File tree

17 files changed

+2505
-0
lines changed

17 files changed

+2505
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.microsoft.durabletask.azurefunctions;
4+
5+
import com.microsoft.durabletask.Task;
6+
import com.microsoft.durabletask.TaskOptions;
7+
import com.microsoft.durabletask.TaskOrchestrationContext;
8+
9+
import javax.annotation.Nullable;
10+
import java.net.URI;
11+
import java.util.Map;
12+
13+
/**
14+
* Provides static helper methods for making durable HTTP calls from Azure Functions orchestrator functions.
15+
* <p>
16+
* The {@code callHttp} methods schedule a built-in HTTP activity that is executed by the Durable Functions host.
17+
* The HTTP request and response are serialized and persisted in the orchestration history, making them safe
18+
* for replay.
19+
* <p>
20+
* Example usage:
21+
* <pre>
22+
* &#64;FunctionName("MyOrchestrator")
23+
* public String run(
24+
* &#64;DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) {
25+
* DurableHttpRequest request = new DurableHttpRequest("GET", new URI("https://example.com/api/data"));
26+
* DurableHttpResponse response = DurableHttp.callHttp(ctx, request).await();
27+
* return response.getContent();
28+
* }
29+
* </pre>
30+
* <p>
31+
* <b>Asynchronous HTTP 202 Polling:</b> When {@link DurableHttpRequest#isAsynchronousPatternEnabled()} is
32+
* {@code true} (the default) and the target endpoint returns an HTTP 202 response with a {@code Location}
33+
* header, the framework will automatically poll the location URL until a non-202 response is received.
34+
* <p>
35+
* <b>Managed Identity Authentication:</b> For authenticated calls using Azure Managed Identity, configure
36+
* a {@link ManagedIdentityTokenSource} on the request:
37+
* <pre>{@code
38+
* TokenSource tokenSource = new ManagedIdentityTokenSource("https://management.core.windows.net/.default");
39+
* DurableHttpRequest request = new DurableHttpRequest(
40+
* "POST",
41+
* new URI("https://management.azure.com/subscriptions/.../restart?api-version=2019-03-01"),
42+
* null, null, tokenSource);
43+
* DurableHttpResponse response = DurableHttp.callHttp(ctx, request).await();
44+
* }</pre>
45+
*
46+
* @see DurableHttpRequest
47+
* @see DurableHttpResponse
48+
* @see TaskOrchestrationContext
49+
*/
50+
public final class DurableHttp {
51+
52+
/**
53+
* The well-known built-in activity name recognized by the Durable Functions host for HTTP calls.
54+
*/
55+
static final String BUILT_IN_HTTP_ACTIVITY_NAME = "BuiltIn::HttpActivity";
56+
57+
private DurableHttp() {
58+
// Static utility class — not instantiable
59+
}
60+
61+
/**
62+
* Makes a durable HTTP request using the specified {@link DurableHttpRequest} and returns a {@link Task}
63+
* that completes when the HTTP call completes. The returned {@code Task}'s value will be a
64+
* {@link DurableHttpResponse} containing the status code, headers, and content of the HTTP response.
65+
* <p>
66+
* If the HTTP call results in a failure (e.g., a connection error), the returned {@code Task} will complete
67+
* exceptionally with a {@link com.microsoft.durabletask.TaskFailedException}. Note that HTTP error status
68+
* codes (4xx, 5xx) are <em>not</em> treated as failures — the response will be returned normally with the
69+
* corresponding status code.
70+
*
71+
* @param ctx the orchestration context
72+
* @param request the {@link DurableHttpRequest} describing the HTTP request to make
73+
* @return a new {@link Task} that completes when the HTTP call completes
74+
*/
75+
public static Task<DurableHttpResponse> callHttp(TaskOrchestrationContext ctx, DurableHttpRequest request) {
76+
return callHttp(ctx, request, null);
77+
}
78+
79+
/**
80+
* Makes a durable HTTP request using the specified {@link DurableHttpRequest} with additional
81+
* options (e.g., retry policies) and returns a {@link Task} that completes when the HTTP call completes.
82+
*
83+
* @param ctx the orchestration context
84+
* @param request the {@link DurableHttpRequest} describing the HTTP request to make
85+
* @param options additional options that control the execution and processing of the HTTP call
86+
* (e.g., retry policies), or {@code null} for default behavior
87+
* @return a new {@link Task} that completes when the HTTP call completes
88+
*/
89+
public static Task<DurableHttpResponse> callHttp(TaskOrchestrationContext ctx, DurableHttpRequest request,
90+
@Nullable TaskOptions options) {
91+
if (ctx == null) {
92+
throw new IllegalArgumentException("ctx must not be null");
93+
}
94+
if (request == null) {
95+
throw new IllegalArgumentException("request must not be null");
96+
}
97+
if (options != null) {
98+
return ctx.callActivity(BUILT_IN_HTTP_ACTIVITY_NAME, request, options, DurableHttpResponse.class);
99+
}
100+
return ctx.callActivity(BUILT_IN_HTTP_ACTIVITY_NAME, request, DurableHttpResponse.class);
101+
}
102+
103+
/**
104+
* Makes a simple durable HTTP request to the specified URI.
105+
* <p>
106+
* This is a convenience method equivalent to:
107+
* <pre>{@code
108+
* DurableHttp.callHttp(ctx, new DurableHttpRequest(method, uri))
109+
* }</pre>
110+
*
111+
* @param ctx the orchestration context
112+
* @param method the HTTP method (e.g., "GET", "POST", "PUT", "DELETE")
113+
* @param uri the target URI for the HTTP request
114+
* @return a new {@link Task} that completes when the HTTP call completes
115+
*/
116+
public static Task<DurableHttpResponse> callHttp(TaskOrchestrationContext ctx, String method, URI uri) {
117+
return callHttp(ctx, new DurableHttpRequest(method, uri));
118+
}
119+
120+
/**
121+
* Makes a durable HTTP request with headers and content to the specified URI.
122+
*
123+
* @param ctx the orchestration context
124+
* @param method the HTTP method (e.g., "GET", "POST", "PUT", "DELETE")
125+
* @param uri the target URI for the HTTP request
126+
* @param headers the HTTP headers to include in the request, or {@code null} for no headers
127+
* @param content the body content of the HTTP request, or {@code null} for no body
128+
* @return a new {@link Task} that completes when the HTTP call completes
129+
*/
130+
public static Task<DurableHttpResponse> callHttp(TaskOrchestrationContext ctx, String method, URI uri,
131+
@Nullable Map<String, String> headers,
132+
@Nullable String content) {
133+
return callHttp(ctx, new DurableHttpRequest(method, uri, headers, content));
134+
}
135+
}

0 commit comments

Comments
 (0)