Пример #1
0
        /// <summary>
        /// Sends message to target as HTTP request and waits for response.
        /// Ok Accepted and Created responses return true, others return false
        /// </summary>
        public override Task <bool> Send(MqClient sender, HorseMessage message)
        {
            try
            {
                HttpClient client = new HttpClient();

                // ReSharper disable once PossibleInvalidOperationException
                HttpBindingMethod          method   = (HttpBindingMethod)ContentType.Value;
                string                     content  = message.Length > 0 ? message.ToString() : null;
                Task <HttpResponseMessage> response = null;

                switch (method)
                {
                case HttpBindingMethod.Get:
                {
                    string uri = Target;
                    if (!string.IsNullOrEmpty(content))
                    {
                        uri += "?" + content;
                    }

                    response = client.GetAsync(uri);
                    break;
                }

                case HttpBindingMethod.Delete:
                {
                    string uri = Target;
                    if (!string.IsNullOrEmpty(content))
                    {
                        uri += "?" + content;
                    }

                    response = client.DeleteAsync(uri);
                    break;
                }

                case HttpBindingMethod.Post:
                    response = client.PostAsync(Target, new StringContent(content, Encoding.UTF8, "application/json"));
                    break;

                case HttpBindingMethod.Put:
                    response = client.PutAsync(Target, new StringContent(content, Encoding.UTF8, "application/json"));
                    break;

                case HttpBindingMethod.Patch:
                    response = client.PatchAsync(Target, new StringContent(content, Encoding.UTF8, "application/json"));
                    break;
                }

                if (response == null)
                {
                    return(Task.FromResult(false));
                }

                return(ProcessResponse(response));
            }
            catch (Exception e)
            {
                Router.Server.SendError("BINDING_SEND", e, $"Type:Http, Binding:{Name}");
                return(Task.FromResult(false));
            }
        }