Skip to content

Commit 6ccb073

Browse files
Added createHttpManagementPayload API (#107)
* Added createHttpManagementPayload API * Moved HttpManagementPayload to a seperate class * Updated CHANGELOG.md * Adding Javadoc for constructor
1 parent a444d95 commit 6ccb073

File tree

3 files changed

+111
-31
lines changed

3 files changed

+111
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
### New
1212

1313
* Add CHANGELOG.md file to track changes across versions
14+
* Added createHttpManagementPayload API ([#63](https://github.com/microsoft/durabletask-java/issues/63))
1415

1516
### Updates
1617

azurefunctions/src/main/java/com/microsoft/durabletask/azurefunctions/DurableClientContext.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -111,44 +111,42 @@ public HttpResponseMessage createCheckStatusResponse(HttpRequestMessage<?> reque
111111
// request headers into consideration and generate the base URL accordingly.
112112
// More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded.
113113
// One potential workaround is to set ASPNETCORE_FORWARDEDHEADERS_ENABLED to true.
114+
115+
// Construct the response as an HTTP 202 with a JSON object payload
116+
return request.createResponseBuilder(HttpStatus.ACCEPTED)
117+
.header("Location", this.getInstanceStatusURL(request, instanceId) + "?" + this.requiredQueryStringParameters)
118+
.header("Content-Type", "application/json")
119+
.body(this.getClientResponseLinks(request, instanceId))
120+
.build();
121+
}
122+
123+
/**
124+
* Creates a {@link HttpManagementPayload} that is useful for checking the status of the specified instance.
125+
*
126+
* @param request The HTTP request that triggered the current orchestration instance.
127+
* @param instanceId The ID of the orchestration instance to check.
128+
* @return The {@link HttpManagementPayload} with URLs that can be used to query the status of the orchestration,
129+
* raise events to the orchestration, or terminate the orchestration.
130+
*/
131+
public HttpManagementPayload createHttpManagementPayload(HttpRequestMessage<?> request, String instanceId) {
132+
return this.getClientResponseLinks(request, instanceId);
133+
}
134+
135+
private HttpManagementPayload getClientResponseLinks(HttpRequestMessage<?> request, String instanceId) {
136+
String instanceStatusURL = this.getInstanceStatusURL(request, instanceId);
137+
return new HttpManagementPayload(instanceId, instanceStatusURL, this.requiredQueryStringParameters);
138+
}
139+
140+
private String getInstanceStatusURL(HttpRequestMessage<?> request, String instanceId) {
114141
String baseUrl = request.getUri().getScheme() + "://" + request.getUri().getAuthority();
115142
String encodedInstanceId;
143+
116144
try {
117145
encodedInstanceId = URLEncoder.encode(instanceId, StandardCharsets.UTF_8.toString());
118146
} catch (UnsupportedEncodingException ex) {
119147
throw new IllegalArgumentException("Failed to encode the instance ID: " + instanceId, ex);
120148
}
121149

122-
String instanceStatusURL = baseUrl + "/runtime/webhooks/durabletask/instances/" + encodedInstanceId;
123-
124-
// Construct the response as an HTTP 202 with a JSON object payload
125-
return request.createResponseBuilder(HttpStatus.ACCEPTED)
126-
.header("Location", instanceStatusURL + "?" + this.requiredQueryStringParameters)
127-
.header("Content-Type", "application/json")
128-
.body(new HttpCreateCheckStatusResponse(
129-
instanceId,
130-
instanceStatusURL,
131-
this.requiredQueryStringParameters))
132-
.build();
133-
}
134-
135-
private static class HttpCreateCheckStatusResponse {
136-
// These fields are serialized to JSON
137-
public final String id;
138-
public final String purgeHistoryDeleteUri;
139-
public final String sendEventPostUri;
140-
public final String statusQueryGetUri;
141-
public final String terminatePostUri;
142-
143-
public HttpCreateCheckStatusResponse(
144-
String instanceId,
145-
String instanceStatusURL,
146-
String requiredQueryStringParameters) {
147-
this.id = instanceId;
148-
this.purgeHistoryDeleteUri = instanceStatusURL + "?" + requiredQueryStringParameters;
149-
this.sendEventPostUri = instanceStatusURL + "/raiseEvent/{eventName}?" + requiredQueryStringParameters;
150-
this.statusQueryGetUri = instanceStatusURL + "?" + requiredQueryStringParameters;
151-
this.terminatePostUri = instanceStatusURL + "/terminate?reason={text}&" + requiredQueryStringParameters;
152-
}
150+
return baseUrl + "/runtime/webhooks/durabletask/instances/" + encodedInstanceId;
153151
}
154152
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for
4+
* license information.
5+
*/
6+
7+
package com.microsoft.durabletask.azurefunctions;
8+
9+
/**
10+
* The class that holds the webhook URLs to manage orchestration instances.
11+
*/
12+
public class HttpManagementPayload {
13+
private final String id;
14+
private final String purgeHistoryDeleteUri;
15+
private final String sendEventPostUri;
16+
private final String statusQueryGetUri;
17+
private final String terminatePostUri;
18+
19+
/**
20+
* Creates a {@link HttpManagementPayload} to manage orchestration instances
21+
*
22+
* @param instanceId The ID of the orchestration instance
23+
* @param instanceStatusURL The base webhook url of the orchestration instance
24+
* @param requiredQueryStringParameters The query parameters to include with the http request
25+
*/
26+
public HttpManagementPayload(
27+
String instanceId,
28+
String instanceStatusURL,
29+
String requiredQueryStringParameters) {
30+
this.id = instanceId;
31+
this.purgeHistoryDeleteUri = instanceStatusURL + "?" + requiredQueryStringParameters;
32+
this.sendEventPostUri = instanceStatusURL + "/raiseEvent/{eventName}?" + requiredQueryStringParameters;
33+
this.statusQueryGetUri = instanceStatusURL + "?" + requiredQueryStringParameters;
34+
this.terminatePostUri = instanceStatusURL + "/terminate?reason={text}&" + requiredQueryStringParameters;
35+
}
36+
37+
/**
38+
* Gets the ID of the orchestration instance.
39+
*
40+
* @return The ID of the orchestration instance.
41+
*/
42+
public String getId() {
43+
return this.id;
44+
}
45+
46+
/**
47+
* Gets the HTTP GET status query endpoint URL.
48+
*
49+
* @return The HTTP URL for fetching the instance status.
50+
*/
51+
public String getStatusQueryGetUri() {
52+
return this.statusQueryGetUri;
53+
}
54+
55+
/**
56+
* Gets the HTTP POST external event sending endpoint URL.
57+
*
58+
* @return The HTTP URL for posting external event notifications.
59+
*/
60+
public String getSendEventPostUri() {
61+
return this.sendEventPostUri;
62+
}
63+
64+
/**
65+
* Gets the HTTP POST instance termination endpoint.
66+
*
67+
* @return The HTTP URL for posting instance termination commands.
68+
*/
69+
public String getTerminatePostUri() {
70+
return this.terminatePostUri;
71+
}
72+
73+
/**
74+
* Gets the HTTP DELETE purge instance history by instance ID endpoint.
75+
*
76+
* @return The HTTP URL for purging instance history by instance ID.
77+
*/
78+
public String getPurgeHistoryDeleteUri() {
79+
return this.purgeHistoryDeleteUri;
80+
}
81+
}

0 commit comments

Comments
 (0)