-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHttpTriggerToAsyncCollector.cs
53 lines (50 loc) · 1.91 KB
/
HttpTriggerToAsyncCollector.cs
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
using cosmosdboutputbindings.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace cosmosdboutputbindings
{
/// <summary>
/// This sample binds a custom array of <see cref="MyClass"/> objects from the HTTP Trigger body and uses the Cosmos DB IAsyncCollector to save them.
/// </summary>
/// <remarks>Sample payload is:
/// {
/// "id": "SomeId", /* optional */
/// "name": "SomeName",
/// "city": "SomeCity"
/// }
/// </remarks>
public static class HttpTriggerToAsyncCollector
{
[FunctionName("HttpTriggerToAsyncCollector")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
[CosmosDB(databaseName: "%CosmosDBDatabase%",
collectionName: "%CosmosDBCollection%",
ConnectionStringSetting = "CosmosDBConnectionString")] IAsyncCollector<MyClass> documentsToSave,
ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
MyClass[] inputDocuments = JsonConvert.DeserializeObject<MyClass[]>(requestBody);
if (inputDocuments == null)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
foreach (MyClass inputDocument in inputDocuments)
{
await documentsToSave.AddAsync(inputDocument);
log.LogInformation(inputDocument.id);
}
return new HttpResponseMessage(HttpStatusCode.Created);
}
}
}