This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsightUploadHttpTrigger.cs
63 lines (55 loc) · 2.59 KB
/
InsightUploadHttpTrigger.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
54
55
56
57
58
59
60
61
62
63
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Dta.Marketplace.Azure.Functions.Business;
using Dta.Marketplace.Azure.Functions.Util;
namespace Dta.Marketplace.Azure.Functions {
public static class InsightUploadHttpTrigger {
[FunctionName("InsightUploadHttpTrigger")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "insight-upload")] HttpRequest req,
ILogger log, ExecutionContext context) {
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var connectionString = config.GetConnectionString("DevProcurementConnectionString");
var dmApiUrl = Environment.GetEnvironmentVariable("dmApiUrl");
var dmApiKey = Environment.GetEnvironmentVariable("dmApiKey");
var monthEnding = req.Query["monthEnding"];
var insightBusiness = new InsightBusiness(connectionString);
var result = await insightBusiness.GetResult(monthEnding);
var now = StringUtil.GetMonthEnding(monthEnding);
var nowString = string.Format("{0:yyyy-MM-dd}", now);
using (var dmClient = new HttpClient()) {
var postObject = new {
data = result
};
var postData = JsonConvert.SerializeObject(postObject, new JsonSerializerSettings {
ContractResolver = new DefaultContractResolver {
NamingStrategy = new CamelCaseNamingStrategy()
}
});
dmClient.DefaultRequestHeaders.Add("X-Api-Key", dmApiKey);
var response = await dmClient.PostAsync($"{dmApiUrl}/insight?monthEnding={nowString}", new StringContent(postData, Encoding.UTF8, "application/json"));
return new OkObjectResult(new {
nowString,
response.StatusCode
});
}
}
}
}