예제 #1
0
        public static WebberResponse <T> GetDeserializedResponse <T>(WebberResponse webberResponse) where T : new()
        {
            var contentType = webberResponse.ContentType.Split(';')[0];

            if (contentType?.ToLower() != ContentType.ApplicationJson)
            {
                throw new NotSupportedException($"{webberResponse.ContentType} is not supported. " +
                                                "Only JSON is supported for auto-deserialization. Use ");
            }

            WebberResponse <T> response = null;

            if (webberResponse.Success)
            {
                response = new WebberResponse <T>(webberResponse);

                try
                {
                    response.Result = JsonConvert.DeserializeObject <T>(webberResponse.RawResult);
                }
                catch (JsonSerializationException ex)
                {
                    OnError(response, ex);

                    response.Result = new T();
                }
            }

            return(response);
        }
예제 #2
0
 private static void OnError(WebberResponse response)
 {
     try
     {
         InvokeOnErrorHandler?.Invoke(response);
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex);
         Trace.WriteLine(response.RawResult);
     }
 }
예제 #3
0
        private static void OnError <T>(WebberResponse response, T exception) where T : Exception
        {
            try
            {
                Trace.WriteLine(exception);

                InvokeOnErrorHandler?.Invoke(response);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
                Trace.WriteLine(response.RawResult);
            }
        }
예제 #4
0
        /// <summary>
        ///     Perform a HTTP request
        /// </summary>
        /// <param name="url">Url of the request</param>
        /// <param name="methodType">HTTP Verb type of the reqeust</param>
        /// <param name="data">Request payload</param>
        /// <param name="contentType">Content Type of the request. Default is application/json</param>
        /// <param name="encodingType">Encoding Type of the request. Default is UTF-8</param>
        /// <param name="credentials">ICredential. Default is NULL</param>
        /// <param name="customHeaders">Additional headers to append to the request. Default is NULL</param>
        /// <returns></returns>
        public static WebberResponse Invoke(
            string url,
            string data                       = "",
            string contentType                = ContentType.ApplicationJson,
            string methodType                 = MethodType.Post,
            EncodingType encodingType         = EncodingType.Utf8,
            ICredentials credentials          = null,
            NameValueCollection customHeaders = null)
        {
            var webberResponse = new WebberResponse();

            try
            {
                var uri     = new Uri(url);
                var request = (HttpWebRequest)WebRequest.Create(uri);

                request.Credentials = credentials;
                request.Method      = methodType;
                request.ContentType = contentType;
                request.UserAgent   = AppName;
                request.KeepAlive   = false;

                if (!string.IsNullOrEmpty(data))
                {
                    request.ContentLength = data.Length;

                    using (var writeStream = request.GetRequestStream())
                    {
                        var bytes = GetBytes(encodingType, data);
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }

                if (customHeaders != null)
                {
                    request.Headers.Add(customHeaders);
                }

                var httpWebResponse = (HttpWebResponse)request.GetResponse();
                var responseStream  = httpWebResponse.GetResponseStream() ?? new MemoryStream();

                using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
                {
                    webberResponse.RawResult = readStream.ReadToEnd();
                }

                webberResponse.StatusCode  = (short)httpWebResponse.StatusCode;
                webberResponse.ContentType = httpWebResponse.ContentType;
                webberResponse.Headers     = httpWebResponse.Headers;
                webberResponse.Success     = true;
            }
            catch (Exception exception)
            {
                Trace.WriteLine(exception);

                webberResponse.RawResult  = exception.ToString();
                webberResponse.StatusCode = -1;

                OnError(webberResponse);
            }

            return(webberResponse);
        }