Esempio n. 1
0
 public Task RegisterWebHook(WebHookStatInput data, CancellationToken cancel)
 {
     return(_statDataProvider.WriteDataAsync(new InputStatisticalDataRecord(data), cancel));
 }
Esempio n. 2
0
        public async Task SendAsync(string url, string eventName, int contentId, int subscriptionId, string httpMethod = null,
                                    object postData = null, IDictionary <string, string> headers = null, CancellationToken cancel = default)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException(nameof(url));
            }

            if (string.IsNullOrEmpty(httpMethod))
            {
                httpMethod = "POST";
            }

            var client = _clientFactory.CreateClient();

            httpMethod = httpMethod.ToUpperInvariant();
            var statData = new WebHookStatInput
            {
                Url         = url,
                HttpMethod  = httpMethod,
                RequestTime = DateTime.UtcNow,
                ContentId   = contentId,
                WebHookId   = subscriptionId,
                EventName   = eventName,
                Payload     = postData
            };


            try
            {
                AddWellKnownHeaders(client, headers);

                HttpResponseMessage response;
                StringContent       stringContent;
                long length;
                switch (httpMethod)
                {
                case "GET":
                    statData.RequestLength = url.Length;
                    response = await client.GetAsync(url, cancel).ConfigureAwait(false);

                    break;

                case "POST":
                    stringContent          = GetStringContent(postData, headers, out length);
                    statData.RequestLength = length + url.Length;
                    response = await client.PostAsync(url, stringContent, cancel)
                               .ConfigureAwait(false);

                    break;

                case "PUT":
                    stringContent          = GetStringContent(postData, headers, out length);
                    statData.RequestLength = length + url.Length;
                    response = await client.PutAsync(url, stringContent, cancel)
                               .ConfigureAwait(false);

                    break;

                //case "DELETE":
                // We cannot use the built-in Delete method because it does not allow sending a body.
                default:
                    var message = GetMessage(url, httpMethod, postData, headers, out length);
                    statData.RequestLength = length + url.Length;
                    response = await client.SendAsync(message, cancel)
                               .ConfigureAwait(false);

                    break;
                }

                statData.ResponseStatusCode = (int)response.StatusCode;
                statData.ResponseTime       = DateTime.UtcNow;
#pragma warning disable 4014
                _statCollector?.RegisterWebHook(statData, cancel);
#pragma warning restore 4014

                var msg = $"WebHook service request completed with {response.StatusCode}. Url: {url} Http method: {httpMethod}";

                if (response.IsSuccessStatusCode)
                {
                    _logger.LogTrace(msg);
                }
                else
                {
                    _logger.LogWarning(msg);
                }
            }
            catch (Exception ex)
            {
                statData.ResponseStatusCode = 500;
                statData.ErrorMessage       = GetErrorMessage(ex);
                _logger.LogWarning(ex, $"Error during webhook service call. Url: {url} Http method: {httpMethod}");
            }
        }
Esempio n. 3
0
 public InputStatisticalDataRecord(WebHookStatInput data)
 {
     _webHookData = data;
 }