Exemple #1
0
        internal async Task <T> ProcessRequestJObject <T>(StructuredHttpExchange exchange, Func <JObject, T> xform)
        {
            T result = await ProcessRequestStream(exchange, stream =>
            {
                JObject jObject;
                try
                {
                    var reader        = new JsonTextReader(new StreamReader(stream));
                    jObject           = JObject.Load(reader);
                    exchange.Response = jObject.ToString();
                }
                catch (Exception ex)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    var reader        = new StreamReader(stream);
                    string content    = reader.ReadToEnd();
                    exchange.Response = content;
                    throw new Exception(content, ex);
                }
                result = xform(jObject);
                return(result);
            });

            return(result);
        }
        /// <summary>
        ///     Fire_s the on exchange complete.
        /// </summary>
        /// <param name="instance">The instance.</param>
        private static void Fire_OnExchangeComplete(StructuredHttpExchange instance)
        {
            EventHandler shadow = OnExchangeComplete;

            if (shadow != null)
            {
                shadow(instance, new EventArgs());
            }
        }
        /// <summary>
        ///     Gets the specified spec.
        /// </summary>
        /// <param name="spec">The spec.</param>
        /// <returns>StructuredHttpExchange.</returns>
        public static StructuredHttpExchange Get(CallSpec spec)
        {
            var retVal = new StructuredHttpExchange(spec)
            {
                Method = HttpMethod.Get
            };

            return(retVal);
        }
Exemple #4
0
#pragma warning disable 1998
        internal async Task <T> ProcessRequestStream <T>(StructuredHttpExchange exchange, Func <Stream, T> xform)
#pragma warning restore 1998
        {
            if (!exchange.RequestMessage.RequestUri.IsAbsoluteUri)
            {
                throw new ArgumentException("Relative URI: in Client Request!");
            }
            var sw = new Stopwatch();

            sw.Start();
            Exception thrownException = null;
            var       task            = UseClientFunc <T>(async client =>
            {
                try
                {
                    HttpRequestMessage httpRequestMessage = exchange.RequestMessage;
                    using (HttpResponseMessage response = client.SendAsync(httpRequestMessage).Result)
                    {
                        CheckForSuccess <T>(httpRequestMessage, response);
                        Stream responseBody  = await response.Content.ReadAsStreamAsync();
                        exchange.NetworkTime = sw.Elapsed;
                        var retVal           = xform(responseBody);
                        exchange.TotalTime   = sw.Elapsed;
                        exchange.MarkAsComplete();
                        return(retVal);
                    }
                }
                catch (AggregateException ex)
                {
                    exchange.RecordException(ex);
                    if (ex.InnerExceptions.Count == 1)
                    {
                        Exception innerException = ex.InnerException;
                        exchange.RecordException(innerException);
                        thrownException = innerException;
                        throw innerException;
                    }
                    exchange.RecordException(ex);
                    thrownException = ex;
                    throw;
                }
                catch (Exception ex)
                {
                    exchange.RecordException(ex);
                    Debug.WriteLine(ex.Message);
                    thrownException = ex;
                    throw;
                }
            });

            if (thrownException != null)
            {
                throw thrownException;
            }
            return(task.Result);
        }
        /// <summary>
        ///     Optionses the specified relative route.
        /// </summary>
        /// <param name="relativeRoute">The relative route.</param>
        /// <returns>StructuredHttpExchange.</returns>
        public static StructuredHttpExchange Options(string relativeRoute)
        {
            var retVal = new StructuredHttpExchange
            {
                RelativeRoute = relativeRoute,
                Method        = HttpMethod.Options
            };

            return(retVal);
        }
        /// <summary>
        ///     Gets the specified URI.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns>StructuredHttpExchange.</returns>
        public static StructuredHttpExchange Get(Uri uri)
        {
            var retVal = new StructuredHttpExchange
            {
                BaseRoute = uri.AbsoluteUri,
                Method    = HttpMethod.Get
            };

            return(retVal);
        }
        /// <summary>
        ///     Posts the specified spec.
        /// </summary>
        /// <param name="spec">The spec.</param>
        /// <param name="body">The body.</param>
        /// <returns>StructuredHttpExchange.</returns>
        public static StructuredHttpExchange Post(CallSpec spec, string body)
        {
            var retVal = new StructuredHttpExchange(spec)
            {
                Method = HttpMethod.Post,
                Body   = body
            };

            return(retVal);
        }
        /// <summary>
        ///     Patches the specified spec.
        /// </summary>
        /// <param name="spec">The spec.</param>
        /// <param name="body">The body.</param>
        /// <returns>StructuredHttpExchange.</returns>
        public static StructuredHttpExchange Patch(CallSpec spec, string body)
        {
            var retVal = new StructuredHttpExchange(spec)
            {
                Method = new HttpMethod("PATCH"),
                Body   = body
            };

            return(retVal);
        }
        /// <summary>
        ///     Patches the specified relative route.
        /// </summary>
        /// <param name="relativeRoute">The relative route.</param>
        /// <param name="body">The body.</param>
        /// <returns>StructuredHttpExchange.</returns>
        public static StructuredHttpExchange Patch(string relativeRoute, string body)
        {
            var retVal = new StructuredHttpExchange
            {
                RelativeRoute = relativeRoute,
                Method        = new HttpMethod("PATCH"),
                Body          = body
            };

            return(retVal);
        }
        /// <summary>
        ///     Posts the specified relative route.
        /// </summary>
        /// <param name="relativeRoute">The relative route.</param>
        /// <param name="body">The body.</param>
        /// <returns>StructuredHttpExchange.</returns>
        public static StructuredHttpExchange Post(string relativeRoute, string body)
        {
            var retVal = new StructuredHttpExchange
            {
                RelativeRoute = relativeRoute,
                Method        = HttpMethod.Post,
                Body          = body
            };

            return(retVal);
        }
Exemple #11
0
 protected async Task <T> ProcessRequest <T>(StructuredHttpExchange exchange, Func <JObject, T> xform)
 {
     return(await r_ClientManager.ProcessRequestJObject(exchange, xform));
 }