-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPollingWebRequestFunction.cs
85 lines (74 loc) · 3.29 KB
/
PollingWebRequestFunction.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using PollingWebRequest.Classes;
using System.Net.Http.Headers;
using System.Net.Http;
namespace PollingWebRequest
{
public static class PollingWebRequestFunction
{
private static StorageHelper storageHelper = new StorageHelper();
[StorageAccount("BlobStorageConnectionString")]
[FunctionName("PollingWebRequestFunction")]
public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
storageHelper.InitializeStorageAccount();
var apiUrl = Utilities.GetEnvironmentVariable("PollUrl");
HttpClient Client = new HttpClient();
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Utilities.GetEnvironmentVariable("BearerToken"));
var result = await Client.GetAsync(apiUrl);
var requestBody = await result.Content.ReadAsStringAsync();
var alerts = JsonConvert.DeserializeObject<List<Alert>>(requestBody);
log.LogInformation($"Initial GET request complete. "+alerts.Count+" alerts found.");
try
{
log.LogInformation($"Updating and looking for duplicated alerts list in Storage.");
alerts = CheckDuplicatesInStorage(alerts);
log.LogInformation($"Duplicate check completed. "+alerts.Count+" new alerts identified.");
if (alerts.Count > 0)
{
var endpoint = Utilities.GetEnvironmentVariable("PostUrl");
HttpClient c = new HttpClient();
var myContent = JsonConvert.SerializeObject(alerts);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var trigger = await c.PostAsync(endpoint, byteContent);
Console.WriteLine(JsonConvert.SerializeObject(alerts));
log.LogInformation($"Alerts sent!");
}
}
catch (Exception e)
{
log.LogInformation(e.ToString());
}
}
static List<Alert> CheckDuplicatesInStorage(List<Alert> alerts)
{
List<Alert> filteredAlerts = new List<Alert>();
var alertsFromStorage = storageHelper.GetListOfAlertsAsync().Result;
if (alertsFromStorage != null)
{
foreach (var alert in alerts)
{
int index = alertsFromStorage.FindIndex(f => string.Equals(f.alert_id, alert.alert_id));
if (index < 0)
{
alertsFromStorage.Add(alert);
filteredAlerts.Add(alert);
}
}
storageHelper.UpdateBlobAsync(JsonConvert.SerializeObject(alertsFromStorage));
}
return filteredAlerts;
}
}
}