-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathHttpTrigger.java
More file actions
197 lines (189 loc) · 7.73 KB
/
HttpTrigger.java
File metadata and controls
197 lines (189 loc) · 7.73 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
package com.microsoft.azure.functions.annotation;
import com.microsoft.azure.functions.HttpMethod;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>
* The HttpTrigger annotation is applied to Azure functions that will be triggered by a call to the
* HTTP endpoint that the function is located at. The HttpTrigger annotation should be applied to a
* method parameter of one of the following types:
* </p>
*
* <ul>
* <li>{@link com.microsoft.azure.functions.HttpRequestMessage HttpRequestMessage<T>}</li>
* <li>Any native Java types such as int, String, byte[]</li>
* <li>Nullable values using Optional<T></li>
* <li>Any POJO type</li>
* </ul>
*
* <p>
* For example:
* </p>
*
* <pre>
* {@literal @}FunctionName("hello")
* public HttpResponseMessage<String> helloFunction(
* {@literal @}HttpTrigger(name = "req",
* methods = {HttpMethod.GET},
* authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request
* ) {
* ....
* }
* </pre>
*
* <p>
* In this code snippet you will observe that we have a function annotated with
* {@code @FunctionName("hello")}, which indicates that this function will be available at the
* endpoint /api/hello. The name of the method itself, in this case {@code helloFunction} is
* irrelevant for all intents and purposes related to Azure Functions. Note however that the method
* return type is {@link com.microsoft.azure.functions.HttpResponseMessage}, and that the first
* argument into the function is an {@link com.microsoft.azure.functions.HttpRequestMessage} with
* generic type {@code Optional<String>}. This indicates that the body of the request will
* potentially contain a String value.
* </p>
*
* <p>
* Most important of all however is the {@code @HttpTrigger} annotation that has been applied to
* this argument. In this annotation you'll note that it has been given a name, as well as told what
* type of requests it supports (in this case, only HTTP GET requests), and that the
* {@link AuthorizationLevel} is anonymous, allowing access to anyone who can call the endpoint.
* </p>
*
* <p>
* The {@code HttpTrigger} can be further customised by providing a custom {@link #route()}, which
* allows for custom endpoints to be specified, and for these endpoints to be parameterized with
* arguments being bound to arguments provided to the function at runtime.
* </p>
*
* <p>
* The following example shows a Java function that looks for a name parameter either in the query
* string (HTTP GET) or the body (HTTP POST) of the HTTP request. Notice that the return value is
* used for the output binding, but a return value attribute isn't required.
* </p>
*
* <pre>
* {@literal @}FunctionName("readHttpName")
* public String readName(
* {@literal @}HttpTrigger(name = "req",
* methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
* final HttpRequestMessage<Optional<String>> request) {
* String name = request.getBody().orElseGet(() -> request.getQueryParameters().get("name"));
* return name == null ?
* "Please pass a name on the query string or in the request body" :
* "Hello " + name;
* }
* </pre>
*
* @see com.microsoft.azure.functions.HttpRequestMessage
* @see com.microsoft.azure.functions.HttpResponseMessage
* @since 1.0.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface HttpTrigger {
/**
* The variable name used in function code for the request or request body.
*
* @return The variable name used in function code for the request or request body.
*/
String name();
/**
* <p>
* Defines how Functions runtime should treat the parameter value. Possible values are:
* </p>
* <ul>
* <li>"": get the value as a string, and try to deserialize to actual parameter type like
* POJO</li>
* <li>string: always get the value as a string</li>
* <li>binary: get the value as a binary data, and try to deserialize to actual parameter type
* byte[]</li>
* </ul>
*
* @return The dataType which will be used by the Functions runtime.
*/
String dataType() default "";
/**
* <p>
* Defines the route template, controlling which request URLs your function will respond to. The
* default value if no route is provided is the function name specified in the
* {@link FunctionName} annotation, applied to each Azure Function.
* </p>
*
* <p>
* By default when you create a function for an HTTP trigger, or WebHook, the function is
* addressable with a route of the form
* {@code http://<yourapp>.azurewebsites.net/api/<funcname>}. You can customize this
* route using this route property. For example, a route of
* {@code "products/{category:alpha}/{id:int}"} would mean that the function is now addressable
* with the following route instead of the original route:
* {@code http://<yourapp>.azurewebsites.net/api/products/electronics/357}, which allows the
* function code to support two parameters in the address: category and id. By specifying the
* route in this way, developers can then add the additional route arguments as arguments into the
* function by using the {@link BindingName} annotation. For example:
* </p>
*
* <pre>
* {@literal @}FunctionName("routeTest")
* public HttpResponseMessage<String> routeTest(
* {@literal @}HttpTrigger(name = "req",
* methods = {HttpMethod.GET},
* authLevel = AuthorizationLevel.ANONYMOUS,
* route = "products/{category:alpha}/{id:int}")
* HttpRequestMessage<Optional<String>> request,
* {@literal @}BindingName("category") String category,
* {@literal @}BindingName("id") int id,
* final ExecutionContext context
* ) {
* ....
* context.getLogger().info("We have " + category + " with id " + id);
* ....
* }
* </pre>
*
* <p>
* For more details on the route syntax, refer to the <a href=
* "https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions">
* online documentation</a>.
* </p>
*
* @return The route template to use for the annotated function.
*/
String route() default "";
/**
* An array of the HTTP methods to which the function responds. If not specified, the function
* responds to all HTTP methods.
*
* @return An array containing all valid HTTP methods.
*/
HttpMethod[] methods() default {};
/**
* <p>
* Determines what keys, if any, need to be present on the request in order to invoke the
* function. The authorization level can be one of the following values:
* </p>
*
* <ul>
* <li><strong>anonymous</strong>: No API key is required.</li>
* <li><strong>function</strong>: A function-specific API key is required. This is the default
* value if none is provided.</li>
* <li><strong>admin</strong>: The master key is required.</li>
* </ul>
*
* <p>
* For more information, see the <a href=
* "https://docs.microsoft.com/azure/azure-functions/functions-bindings-http-webhook#authorization-keys">documentation
* about authorization keys</a>.
* </p>
*
* @return An {@link AuthorizationLevel} value representing the level required to access the
* function.
*/
AuthorizationLevel authLevel() default AuthorizationLevel.FUNCTION;
}