-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeople.cs
More file actions
50 lines (43 loc) · 1.82 KB
/
people.cs
File metadata and controls
50 lines (43 loc) · 1.82 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
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Corr.Starwars.api.Models;
using Azure.Storage.Blobs;
using System.Linq;
namespace Corr.Starwars.api
{
public static class people
{
[FunctionName("people")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
{
log.LogInformation("Function started.");
string id = req.Query["id"];
// read app settings from ENV vars
var peopleUrl = Environment.GetEnvironmentVariable("PEOPLE_DATAFILE_URL");
var test = await new BlobClient(new Uri(peopleUrl)).DownloadContentAsync();
var peopleRoot = JsonConvert.DeserializeObject<PeopleRoot>(test.Value.Content.ToString());
if (!string.IsNullOrEmpty(id))
{
log.LogInformation($"Searching for people data with id '{id}'.");
var results = peopleRoot.PeopleData.Where(pd => pd.id == int.Parse(id));
return new OkObjectResult(results);
}
else
{
log.LogInformation($"Returning list of all names and some metadata.");
var results = peopleRoot.PeopleData.Select(pd => new {id= pd.id, name = pd.name, homeworld = pd.homeworld, url = generateUrl(req, pd.id)});
return new OkObjectResult(results);
}
}
private static string generateUrl(HttpRequest request, int id)
{
return $"{(request.IsHttps?"https://":"http://")}{request.Host}{request.Path}?id={id}";
}
}
}