/// <summary> /// Synchronously 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 CCPAPIResult <T> DownloadAPIResult <T>(Uri url, bool acceptEncoded = false, string postData = null, XslCompiledTransform transform = null) { CCPAPIResult <T> result; try { DownloadResult <IXPathNavigable> apiResult = HttpWebClientService.DownloadXmlAsync(url, HttpMethod.Post, acceptEncoded, postData).Result; // Was there an HTTP error ? result = apiResult.Error != null ? new CCPAPIResult <T>(apiResult.Error) : DeserializeAPIResultCore <T>(apiResult.Result, transform); } catch (Exception e) { ExceptionHandler.LogException(e, true); result = new CCPAPIResult <T>(Enumerations.CCPAPI.CCPAPIErrors.Http, e.Message); EveMonClient.Trace( $"Method: DownloadAPIResult, url: {url.AbsoluteUri}, postdata: {postData}, type: {typeof(T).Name}", false); } // Returns return(result); }
/// <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); }
/// <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); }
/// <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)); }