Beispiel #1
0
        public Outcome <HttpReply> Send(HttpRequestSpec spec)
        {
            try
            {
                var request  = CreateRequest(spec);
                var response = client.SendAsync(request).Result;

                var descriptor = new HttpReplyStatus
                                 (
                    ProtocolCode: HttpResultCodes.Find((int)response.StatusCode),
                    SystemCode: SystemResultCodes.Success
                                 );

                var data         = response.Content.ReadAsStringAsync().Result;
                var notification = response.IsSuccessStatusCode
                                 ? none <IApplicationMessage>()
                                 : some(HttpStatusMessages.ErrorStatus(descriptor, data));
                return(success(
                           new HttpReply(descriptor, data),
                           notification.ValueOrDefault(ApplicationMessage.Empty)
                           ));
            }
            catch (Exception e)
            {
                return(InterpretError(e, spec));
            }
        }
Beispiel #2
0
        static Outcome <HttpReply> InterpretWebException(WebException we, HttpRequestSpec spec)
        {
            try
            {
                //If we can successfully retrieve an error response, then whatever
                //happened is not considered a systemic failure
                var response = we.Response;
                if (response == null)
                {
                    return(SystemFailed(we, spec));
                }

                using (var stream = response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        var data       = reader.ReadToEnd();
                        var descriptor = new HttpReplyStatus
                                         (
                            ProtocolCode: response.GetHttpStatusCode(),
                            SystemCode: SystemResultCodes.Success
                                         );


                        return(new Outcome <HttpReply>(true, new HttpReply(descriptor, data),
                                                       HttpStatusMessages.ErrorStatus(descriptor, data)));
                    }
            }
            catch (Exception e)
            {
                return(SystemFailed(e, spec));
            }
        }
Beispiel #3
0
        private static HttpRequestMessage CreateRequest(HttpRequestSpec spec)
        {
            var message = new HttpRequestMessage(spec.Method, spec.RequestUri.PathAndQuery);

            if (spec.Method != HttpMethods.DELETE)
            {
                var parameters = spec.Parameters.Render(false);
                message.Content = new StringContent(parameters, Encoding.UTF8, HttpMediaTypes.UrlEncoded);
            }
            return(message);
        }
Beispiel #4
0
 static Outcome <HttpReply> InterpretError(Exception e, HttpRequestSpec spec)
 => e is WebException?InterpretWebException(e as WebException, spec)
     : SystemFailed(e, spec);
Beispiel #5
0
 static Outcome <HttpReply> SystemFailed(Exception e, HttpRequestSpec spec)
 => Outcome.Fail <HttpReply>(HttpStatusMessages.SystemError(e.ToString()));