-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWriteOneDoc.java
59 lines (50 loc) · 2.25 KB
/
WriteOneDoc.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
package com.danielrocks.function.cosmos.output;
import java.util.Optional;
import java.util.Random;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.CosmosDBOutput;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
/**
* Azure Functions with HTTP Trigger.
*
* The following example shows a Java function that uses the CosmosDBOutput annotation
* to return a single JSON document that will be written to a CosmosDB collection.
*
* The function is triggered by an HTTP request that uses query string data to set the 'description'
* field of the saved document.
*/
public class WriteOneDoc {
/**
* This function listens at endpoint "/api/WriteOneDoc". Two ways to invoke it using "curl" command in bash:
* 1. curl -d "HTTP Body" {your host}/api/WriteOneDoc&desc={description}
* 2. curl "{your host}/api/WriteOneDoc?desc={description}"
*/
@FunctionName("WriteOneDoc")
@CosmosDBOutput(name = "database",
databaseName = "ToDoList",
collectionName = "Items",
connectionStringSetting = "Cosmos_DB_Connection_String")
public String run(
@HttpTrigger(name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
// Item list
context.getLogger().info("Parameters are: " + request.getQueryParameters());
// Parse query parameter
String query = request.getQueryParameters().get("desc");
String name = request.getBody().orElse(query);
// Generate random ID
final int id = Math.abs(new Random().nextInt());
// Generate document
final String jsonDocument = "{\"id\":\"" + id + "\", " +
"\"description\": \"" + name + "\"}";
context.getLogger().info("Document to be saved: " + jsonDocument);
return jsonDocument;
}
}