Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Net.Http;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Converters;
  8. using WebStore.Db;
  9. using WebStore.Model.Webhooks;
  10. using DbWebhook = WebStore.Model.Webhooks.Webhook;
  11.  
  12. namespace WebStore.Webhooks
  13. {
  14.     public sealed class Webhook
  15.     {
  16.         private static HttpClient Client { get; }
  17.  
  18.         static Webhook()
  19.         {
  20.             Client = new HttpClient();
  21.         }
  22.  
  23.         public static async Task Broadcast<T>(T obj, BroadcastType type) where T : class
  24.         {
  25.             var connections = (await Database.SelectAllAsync<DbWebhook>())
  26.                 .Where(e => !e.Suspended)
  27.                 .Select(hook => new WebhookConnection(hook, type, obj))
  28.                 .ToList();
  29.  
  30.             var tasks = connections.Select(connection => Task.Run(connection.Start)).ToArray();
  31.             Task.WaitAll(tasks);
  32.  
  33.             foreach (var connection in connections)
  34.             {
  35.                 if (connection.Failed)
  36.                 {
  37.                     // TODO suspend webhook connection, send email etc
  38.                 }
  39.             }
  40.         }
  41.  
  42.         private class WebhookConnection
  43.         {
  44.             private const int MaxRetries = 3;
  45.             private const int RetryDelay = 30;
  46.  
  47.             private IWebhook Webhook { get; }
  48.  
  49.             [JsonConverter(typeof(StringEnumConverter))]
  50.             private BroadcastType BroadcastType { get; }
  51.  
  52.             private object Obj { get; }
  53.             private int Retry { get; set; }
  54.  
  55.             public bool Failed => Retry == 3;
  56.  
  57.             public WebhookConnection(IWebhook webhook, BroadcastType broadcastType, object obj)
  58.             {
  59.                 Webhook = webhook;
  60.                 BroadcastType = broadcastType;
  61.                 Obj = obj;
  62.             }
  63.  
  64.             public async Task Start()
  65.             {
  66.                 var uri = new Uri($"https://{Webhook.IPEndpoint}:{Webhook.PortEndpoint}/{Webhook.PathEndpoint}");
  67.                 var content = new StringContent(JsonConvert.SerializeObject(new {Type = BroadcastType, Object = Obj}),
  68.                     Encoding.UTF8, "application/json");
  69.  
  70.                 while (!(await SendAsync(uri, content)).IsSuccessStatusCode && Retry++ < MaxRetries)
  71.                     await Task.Delay(TimeSpan.FromSeconds(RetryDelay));
  72.             }
  73.  
  74.             private static async Task<HttpResponseMessage> SendAsync(Uri uri, HttpContent content) =>
  75.                 await Client.PostAsync(uri, content);
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement