-
Notifications
You must be signed in to change notification settings - Fork 35
/
InvokeOutputBinding.java
67 lines (61 loc) · 2.69 KB
/
InvokeOutputBinding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package main.java.com.function;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.dapr.annotation.DaprInvokeOutput;
import com.microsoft.azure.functions.dapr.annotation.DaprStateOutput;
import com.microsoft.azure.functions.OutputBinding;
import java.util.Optional;
/**
* This function uses Dapr Invoke Output Binding to invoke another Dapr enabled function.
* This function can be invoked by http trigger.
*/
public class InvokeOutputBinding {
/**
* This function listens at endpoint "/api/invoke/{appId}/{methodName}". Curl command to invoke it:
* curl -X POST \ ─╯
http://localhost:7071/api/invoke/functionapp/CreateNewOrder \
-H 'Content-Type: application/json' \
-d '{
"body":{
"value": {
"orderId": "45"
}
}
}'
*/
@FunctionName("InvokeOutputBinding")
public String run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS,
route = "invoke/{appId}/{methodName}")
HttpRequestMessage<Optional<String>> request,
@DaprInvokeOutput(
appId = "{appId}",
methodName = "{methodName}",
httpVerb = "post")
OutputBinding<String> payload,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
// Parse query parameter
final String query = request.getQueryParameters().get("name");
final String body = request.getBody().orElse(query);
//request body must be passed this way "{\"body\":{\"value\":{\"key\":\"some value\"}}}" to use the InvokeOutputBinding, all the data must be enclosed in body property.
String jsoString = String.format("{\"body\":%s}", body);
context.getLogger().info(jsoString);
payload.setValue(jsoString);
return jsoString;
}
}