Exemple #1
0
        /// <summary>
        /// Asynchronously downloads a JSON object from a JSON stream.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">The URL.</param>
        /// <param name="param">The request parameters. If null, defaults will be used.</param>
        /// <returns></returns>
        public static async Task <JsonResult <T> > DownloadJsonAsync <T>(Uri url,
                                                                         RequestParams param = null) where T : class
        {
            JsonResult <T> result;

            try
            {
                var asyncResult = await HttpWebClientService.DownloadStreamAsync <T>(url,
                                                                                     ParseJSONObject <T>, param);

                var error = asyncResult.Error;
                T   data;
                // Was there an HTTP error?
                if (error != null)
                {
                    result = new JsonResult <T>(error);
                }
                else if ((data = asyncResult.Result) == default(T) && !asyncResult.Response.
                         IsNotModifiedResponse)
                {
                    // This will become a json error
                    result = new JsonResult <T>(new InvalidOperationException(
                                                    "null JSON response"));
                }
                else
                {
                    result = new JsonResult <T>(asyncResult.Response, data);
                }
            }
            catch (InvalidOperationException e)
            {
                result = new JsonResult <T>(e);
                ExceptionHandler.LogException(e, true);
            }
            catch (InvalidDataContractException e)
            {
                result = new JsonResult <T>(e);
                ExceptionHandler.LogException(e, true);
            }
            catch (SerializationException e)
            {
                // For deserializing non-errors
                result = new JsonResult <T>(e);
                ExceptionHandler.LogException(e, true);
            }
            catch (APIException e)
            {
                int code;
                // Error code was converted to a string to match APIException
                if (!e.ErrorCode.TryParseInv(out code))
                {
                    code = 0;
                }
                result = new JsonResult <T>(new ResponseParams(code), e.Message);
                ExceptionHandler.LogException(e, true);
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Asynchronously downloads a JSON object from a JSON stream.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">The URL.</param>
        /// <param name="token">The ESI token.</param>
        /// <param name="acceptEncoded">if set to <c>true</c> [accept encoded].</param>
        /// <param name="postData">The post data.</param>
        /// <returns></returns>
        public static async Task <JsonResult <T> > DownloadJsonAsync <T>(Uri url, string token,
                                                                         bool acceptEncoded = false, string postData = null, string postContentType = null)
            where T : class
        {
            // Create POST data body
            HttpPostData content = null;

            if (postData != null)
            {
                content = new HttpPostData(postData, contentType: postContentType);
            }
            JsonResult <T> result;

            try
            {
                DownloadResult <T> asyncResult = await HttpWebClientService.DownloadStreamAsync <T>(
                    url, ParseJSONObject <T>, acceptEncoded, content, token);

                var error = asyncResult.Error;
                T   data;

                // Was there an HTTP error?
                if (error != null)
                {
                    result = new JsonResult <T>(error);
                }
                else if ((data = asyncResult.Result) == default(T))
                {
                    // This will become a json error
                    result = new JsonResult <T>(new InvalidOperationException("null JSON response"));
                }
                else
                {
                    result = new JsonResult <T>(asyncResult.ResponseCode, data)
                    {
                        CurrentTime = asyncResult.ServerTime
                    }
                };
            }
            catch (InvalidOperationException e)
            {
                result = new JsonResult <T>(e);
                ExceptionHandler.LogException(e, true);
            }
            catch (InvalidDataContractException e)
            {
                result = new JsonResult <T>(e);
                ExceptionHandler.LogException(e, true);
            }

            return(result);
        }