Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Microsoft.Azure.WebJobs;
  5. using Microsoft.Azure.WebJobs.Extensions.Http;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.Extensions.Logging;
  8. using Newtonsoft.Json;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using PollingWebRequest.Classes;
  12. using System.Net.Http.Headers;
  13. using System.Net.Http;
  14. namespace PollingWebRequest
  15. {
  16. public static class PollingWebRequestFunction
  17. {
  18. private static StorageHelper storageHelper = new StorageHelper();
  19. [StorageAccount("BlobStorageConnectionString")]
  20. [FunctionName("PollingWebRequestFunction")]
  21. public static async Task Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
  22. {
  23. storageHelper.InitializeStorageAccount();
  24. var apiUrl = Utilities.GetEnvironmentVariable("PollUrl");
  25. HttpClient Client = new HttpClient();
  26. Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Utilities.GetEnvironmentVariable("BearerToken"));
  27. var result = await Client.GetAsync(apiUrl);
  28. var requestBody = await result.Content.ReadAsStringAsync();
  29. var alerts = JsonConvert.DeserializeObject<List<Alert>>(requestBody);
  30. log.LogInformation($"Initial GET request complete. "+alerts.Count+" alerts found.");
  31. try
  32. {
  33. log.LogInformation($"Updating and looking for duplicated alerts list in Storage.");
  34. alerts = CheckDuplicatesInStorage(alerts);
  35. log.LogInformation($"Duplicate check completed. "+alerts.Count+" new alerts identified.");
  36. if (alerts.Count > 0)
  37. {
  38. var endpoint = Utilities.GetEnvironmentVariable("PostUrl");
  39. HttpClient c = new HttpClient();
  40. var myContent = JsonConvert.SerializeObject(alerts);
  41. var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
  42. var byteContent = new ByteArrayContent(buffer);
  43. byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  44. var trigger = await c.PostAsync(endpoint, byteContent);
  45. Console.WriteLine(JsonConvert.SerializeObject(alerts));
  46. log.LogInformation($"Alerts sent!");
  47. }
  48. }
  49. catch (Exception e)
  50. {
  51. log.LogInformation(e.ToString());
  52. }
  53. }
  54. static List<Alert> CheckDuplicatesInStorage(List<Alert> alerts)
  55. {
  56. List<Alert> filteredAlerts = new List<Alert>();
  57. var alertsFromStorage = storageHelper.GetListOfAlertsAsync().Result;
  58. if (alertsFromStorage != null)
  59. {
  60. foreach (var alert in alerts)
  61. {
  62. int index = alertsFromStorage.FindIndex(f => string.Equals(f.alert_id, alert.alert_id));
  63. if (index < 0)
  64. {
  65. alertsFromStorage.Add(alert);
  66. filteredAlerts.Add(alert);
  67. }
  68. }
  69. storageHelper.UpdateBlobAsync(JsonConvert.SerializeObject(alertsFromStorage));
  70. }
  71. return filteredAlerts;
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement