-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreeter.cs
38 lines (29 loc) · 1.23 KB
/
Greeter.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
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Newtonsoft.Json;
/// <inheritdoc/>
public class Greeter : IGreeter
{
const string CANNED_MESSAGE =
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
const string SAY_HELLO_TEMPLATE =
"Hello, {0}. This HTTP triggered function executed successfully.";
private string CreateGreetingFor(string name) => string.Format(SAY_HELLO_TEMPLATE, name);
/// <inheritdoc/>
public IActionResult ReturnCannedResponse() => new OkObjectResult(CANNED_MESSAGE);
/// <inheritdoc/>
public async Task<IActionResult> SayHello(HttpRequest req, ICollector<string> msg)
{
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
name = !string.IsNullOrWhiteSpace(name) ? name : "noname";
var greeting = CreateGreetingFor(name);
msg.Add(greeting);
return new OkObjectResult(greeting);
}
}