-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerlessAPI.cs
More file actions
43 lines (34 loc) · 1.44 KB
/
ServerlessAPI.cs
File metadata and controls
43 lines (34 loc) · 1.44 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
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.Text.Json;
namespace ServerlessAPI
{
public class ServeCountryData
{
private readonly ILogger<ServeCountryData> _logger;
public ServeCountryData(ILogger<ServeCountryData> logger)
{
_logger = logger;
}
[Function("ServeCountryData")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "GetLocations/{name}")] HttpRequest req, string name)
{
_logger.LogInformation($"HTTP {req.Method.ToString()} request recieved.");
HttpClient client = new HttpClient();
client.BaseAddress = new Uri($"https://rawcdn.githack.com/kamikazechaser/administrative-divisions-db/master/api/{name}.json");
Uri address = client.BaseAddress;
var response = await client.GetAsync(address);
if (response.IsSuccessStatusCode) {
var responseObject = await response.Content.ReadAsStringAsync();
return new OkObjectResult($"Serving country data now...{"\n"}{responseObject.ToString()}");
}
else
{
return new BadRequestObjectResult("Failed to retrieve country information. Bad request.");
}
}
}
}