示例#1
0
        protected WebHookData GetDefaultPostHeader(string userName, string avatarURL)
        {
            const string Username = "******";
            var          post     = new WebHookData();

            post.Username  = Settings.NameAsDiscordUser ? userName : Username;
            post.AvaturURL = avatarURL;
            post.Content   = "";
            return(post);
        }
示例#2
0
 public bool UpdateWebHookLastExecutionTime(WebHookData data)
 {
     using (var conn = new SqlConnection(this.ConnetionString))
     {
         return(conn.Execute(
                    "sp_Broker_WebHook_LastExecutionTime_Update",
                    commandType: CommandType.StoredProcedure,
                    param: new {
             data.Id,
             data.BrokerName,
             data.LastExecutionTime
         }
                    ) > 0);
     }
 }
示例#3
0
 public bool RegisterWebHook(WebHookData data)
 {
     using (var conn = new SqlConnection(this.ConnetionString))
     {
         return(conn.Execute(
                    "sp_Broker_WebHook_Register",
                    commandType: CommandType.StoredProcedure,
                    param: new {
             data.BrokerName,
             data.ServiceName,
             data.HookURL
         }
                    ) > 0);
     }
 }
示例#4
0
        private void HandleBrokerHook(BrokerData brokerData, WebHookData hookData)
        {
            var step = 0;

            try
            {
                step = 1;

                var httpService = new HttpServiceHelper();
                var config      = new DataUpdatesConfig {
                    FromTime = hookData.LastExecutionTime
                };

                var serviceData = this.BLL.GetServiceData(hookData.ServiceName, brokerData);
                if (serviceData == null)
                {
                    throw new Exception($"No Service Data: {hookData.ServiceName}");
                }

                step = 2;

                var proxyURL  = this.Config.ProxyServerURI; // "http://proxy.crtv.co.il";
                var proxyPort = serviceData.Port;

                var generator = new JWTGenerator(this.Config.JWTSecretKey);
                var token     = generator.GenerateToken(new {
                    brokerName = brokerData.Name,
                    role       = "Broker"
                });

                step = 3;

                var updatesURI    = $"{proxyURL}:{proxyPort}/{hookData.ServiceName}/updates";
                var serviceResult = httpService.POST(updatesURI, config, headers: new Dictionary <string, string>
                {
                    ["Content-Type"]  = "application/json",
                    ["Authorization"] = $"Bearer {token}"
                });

                step = 4;

                // TODO ->> StatusCode
                if (!serviceResult.Success)
                {
                    // remove html content
                    if (serviceResult.Content.Contains("<!DOCTYPE"))
                    {
                        serviceResult.Content = serviceResult.Content.Split('<').FirstOrDefault();
                    }

                    throw new Exception($"Get Updates from {updatesURI} -> {serviceResult.Content}");
                }

                var updates    = serviceResult.Content;
                var hasUpdates = !string.IsNullOrEmpty(updates) && updates != "[]";

                step = 5;

                // push updates
                if (hasUpdates)
                {
                    this.Logger.Info("WebHooksProcess", $"#{hookData.Id}, POST {hookData.ServiceName} Updates to {hookData.HookURL}");

                    var pushResult = httpService.POST($"{hookData.HookURL}", updates, headers: new Dictionary <string, string>
                    {
                        ["Content-Type"] = "application/json"
                    });

                    step = 6;

                    if (!pushResult.Success)
                    {
                        throw new Exception($"Push Updates to {hookData.HookURL} -> {serviceResult.Content}");
                    }
                }

                step = 7;

                hookData.LastExecutionTime = DateTime.Now; // update the last execution time
                this.BLL.UpdateWebHookLastExecutionTime(hookData);
            }
            catch (Exception ex)
            {
                ex.Data.Add("Method", "WebHooksProcess.HandleBrokerHook");
                ex.Data.Add("HookId", hookData.Id);
                ex.Data.Add("BrokerName", hookData.BrokerName);
                ex.Data.Add("ServiceName", hookData.ServiceName);
                ex.Data.Add("Step", step);
                this.Logger.Error("WebHooksProcess", ex);
            }
        }