Example #1
0
        /// <summary>
        /// Asynchronously download an XML and deserializes it into the specified type.
        /// </summary>
        /// <typeparam name="T">The inner type to deserialize</typeparam>
        /// <param name="url">The url to query</param>
        /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param>
        /// <param name="postData">The post data.</param>
        /// <param name="transform">The XSL transform to apply, may be null.</param>
        internal static async Task <CCPAPIResult <T> > DownloadAPIResultAsync <T>(Uri url, bool acceptEncoded    = false,
                                                                                  string postData                = null,
                                                                                  XslCompiledTransform transform = null)
        {
            DownloadResult <IXPathNavigable> asyncResult =
                await HttpWebClientService.DownloadXmlAsync(url, HttpMethod.Post, acceptEncoded, postData);

            CCPAPIResult <T> result;

            try
            {
                // Was there an HTTP error ?
                result = asyncResult.Error != null
                    ? new CCPAPIResult <T>(asyncResult.Error)
                    : DeserializeAPIResultCore <T>(asyncResult.Result, transform);

                // We got the result
                return(result);
            }
            catch (Exception e)
            {
                result = new CCPAPIResult <T>(HttpWebClientServiceException.Exception(url, e));

                ExceptionHandler.LogException(e, false);
                EveMonClient.Trace(
                    $"Method: DownloadAPIResultAsync, url: {url.AbsoluteUri}, postdata: {postData}, type: {typeof(T).Name}",
                    false);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Asynchronously download an XML and deserializes it into the specified type.
        /// </summary>
        /// <typeparam name="T">The inner type to deserialize</typeparam>
        /// <param name="url">The url to query</param>
        /// <param name="param">The request parameters. If null, defaults will be used.</param>
        /// <param name="transform">The XSL transform to apply, may be null.</param>
        internal static async Task <CCPAPIResult <T> > DownloadAPIResultAsync <T>(Uri url,
                                                                                  RequestParams param = null, XslCompiledTransform transform = null)
        {
            var asyncResult = await HttpWebClientService.DownloadXmlAsync(url, param);

            CCPAPIResult <T> result;

            try
            {
                // Was there an HTTP error ?
                result = (asyncResult.Error != null) ? new CCPAPIResult <T>(asyncResult.Error) :
                         DeserializeAPIResultCore <T>(asyncResult.Result, transform);
                // We got the result
                return(result);
            }
            catch (Exception e)
            {
                result = new CCPAPIResult <T>(HttpWebClientServiceException.Exception(url, e));

                ExceptionHandler.LogException(e, false);
                EveMonClient.Trace($"Method: DownloadAPIResultAsync, url: {url.AbsoluteUri}, postdata: {param?.Content}, type: {typeof(T).Name}",
                                   false);
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// Gets the troubleshooter.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <returns>A troubleshooter for the error message.</returns>
        private ApiErrorTroubleshooter GetTroubleshooter(Exception exception)
        {
            HttpWebClientServiceException httpException = exception as HttpWebClientServiceException;

            return(httpException?.Status == HttpWebClientServiceExceptionStatus.Timeout
                ? m_httpTimeoutTroubleshooter
                : null);
        }
            /// <summary>
            /// Begins the download.
            /// </summary>
            private void BeginDownload()
            {
                Uri    url = new Uri($"{m_datafile.Address}/{m_datafile.Name}");
                string urlValidationError;

                if (!HttpWebClientService.IsValidURL(url, out urlValidationError))
                {
                    return;
                }

                if (File.Exists(m_tempFilename))
                {
                    FileHelper.DeleteFile(m_tempFilename);
                }

                try
                {
                    using (WebClient)
                    {
                        WebClient.DownloadFileCompleted   += DownloadCompleted;
                        WebClient.DownloadProgressChanged += ProgressChanged;

                        try
                        {
                            WebClient.DownloadFileAsync(url, m_tempFilename);
                        }
                        catch (WebException ex)
                        {
                            throw HttpWebClientServiceException.HttpWebClientException(url, ex);
                        }
                        catch (Exception ex)
                        {
                            throw HttpWebClientServiceException.Exception(url, ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ExceptionHandler.LogRethrowException(ex);
                    throw;
                }
            }
Example #5
0
        /// <summary>
        /// Asynchronously download an object from a JSON stream.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">The URL.</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 <DownloadResult <T> > DownloadJsonAsync <T>(Uri url, bool acceptEncoded = false,
                                                                             string postData             = null)
            where T : class
        {
            DownloadResult <String> asyncResult =
                await HttpWebClientService.DownloadStringAsync(url, HttpMethod.Post, acceptEncoded, postData);

            T result = null;
            HttpWebClientServiceException error = null;

            // Was there an HTTP error ??
            if (asyncResult.Error != null)
            {
                error = asyncResult.Error;
            }
            else
            {
                // No http error, let's try to deserialize
                try
                {
                    // Deserialize
                    result = new JavaScriptSerializer().Deserialize <T>(asyncResult.Result);
                }
                catch (ArgumentException exc)
                {
                    // An error occurred during the deserialization
                    ExceptionHandler.LogException(exc, true);
                    error = new HttpWebClientServiceException(exc.InnerException?.Message ?? exc.Message);
                }
                catch (InvalidOperationException exc)
                {
                    // An error occurred during the deserialization
                    ExceptionHandler.LogException(exc, true);
                    error = new HttpWebClientServiceException(exc.InnerException?.Message ?? exc.Message);
                }
            }

            return(new DownloadResult <T>(result, error));
        }
Example #6
0
        /// <summary>
        /// Handles the Shown event of the UpdateDownloadForm control.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            string urlValidationError;

            if (!HttpWebClientService.IsValidURL(m_url, out urlValidationError))
            {
                throw new ArgumentException(urlValidationError);
            }

            try
            {
                using (m_client = HttpWebClientService.GetWebClient())
                {
                    m_client.DownloadFileCompleted   += DownloadCompleted;
                    m_client.DownloadProgressChanged += ProgressChanged;

                    try
                    {
                        m_client.DownloadFileAsync(m_url, m_fileName);
                    }
                    catch (WebException ex)
                    {
                        throw HttpWebClientServiceException.HttpWebClientException(m_url, ex);
                    }
                    catch (Exception ex)
                    {
                        throw HttpWebClientServiceException.Exception(m_url, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.LogRethrowException(ex);
                throw;
            }
        }
Example #7
0
 /// <summary>
 /// Constructor from an http exception
 /// </summary>
 /// <param name="exception">The exception.</param>
 public JsonResult(HttpWebClientServiceException exception)
     : this(exception as Exception)
 {
     ErrorType = APIErrorType.Http;
     Response  = new ResponseParams((int)exception.StatusCode);
 }
Example #8
0
        /// <summary>
        /// Asynchronously download an object from an XML stream.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">The url to download from</param>
        /// <param name="param">The request parameters. If null, defaults will be used.</param>
        /// <param name="transform">The transform.</param>
        /// <returns></returns>
        public static async Task <DownloadResult <T> > DownloadXmlAsync <T>(Uri url,
                                                                            RequestParams param = null, XslCompiledTransform transform = null) where T : class
        {
            var asyncResult = await HttpWebClientService.DownloadXmlAsync(url, param);

            T result = null;
            HttpWebClientServiceException error = null;

            // Was there an HTTP error ??
            if (asyncResult.Error != null)
            {
                error = asyncResult.Error;
            }
            else
            {
                // No http error, let's try to deserialize
                try
                {
                    // Deserialize
                    using (XmlNodeReader reader = new XmlNodeReader((XmlDocument)asyncResult.Result))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(T));
                        if (transform != null)
                        {
                            MemoryStream stream = GetMemoryStream();
                            using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
                            {
                                // Apply the XSL transform
                                writer.Formatting = Formatting.Indented;
                                transform.Transform(reader, writer);
                                writer.Flush();

                                // Deserialize from the given stream
                                stream.Seek(0, SeekOrigin.Begin);
                                result = (T)xs.Deserialize(stream);
                            }
                        }
                        // Deserialization without transform
                        else
                        {
                            result = (T)xs.Deserialize(reader);
                        }
                    }
                }
                // An error occurred during the XSL transform
                catch (XsltException exc)
                {
                    ExceptionHandler.LogException(exc, true);
                    error = new HttpWebClientServiceException(exc.GetBaseException().Message);
                }
                catch (InvalidOperationException exc)
                {
                    // An error occurred during the deserialization
                    ExceptionHandler.LogException(exc, true);
                    error = new HttpWebClientServiceException(exc.GetBaseException().Message);
                }
                catch (XmlException exc)
                {
                    ExceptionHandler.LogException(exc, true);
                    error = new HttpWebClientServiceException(exc.GetBaseException().Message);
                }
            }
            return(new DownloadResult <T>(result, error, asyncResult.Response));
        }
Example #9
0
 /// <summary>
 /// Constructor from an http exception
 /// </summary>
 /// <param name="exception">The exception.</param>
 public CCPAPIResult(HttpWebClientServiceException exception)
     : this(exception as Exception)
 {
     m_error = APIErrorType.Http;
 }
Example #10
0
 /// <summary>
 /// Constructor from an http exception
 /// </summary>
 /// <param name="exception">The exception.</param>
 public EsiResult(HttpWebClientServiceException exception) : base(exception)
 {
     CachedUntil = GetErrorCacheTime();
     HasData     = false;
 }
Example #11
0
 /// <summary>
 /// Constructor from an http exception
 /// </summary>
 /// <param name="exception">The exception.</param>
 public EsiResult(HttpWebClientServiceException exception) : base(exception)
 {
     CachedUntil = CurrentTime;
 }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DownloadResult{T}"/> class.
 /// </summary>
 /// <param name="result">The result.</param>
 /// <param name="error">The error.</param>
 public DownloadResult(T result, HttpWebClientServiceException error)
 {
     Error  = error;
     Result = result;
 }
Example #13
0
 /// <summary>
 /// Constructor from an http exception
 /// </summary>
 /// <param name="exception">The exception.</param>
 public JsonResult(HttpWebClientServiceException exception)
     : this(exception as Exception)
 {
     ErrorType = APIErrorType.Http;
 }