private static async Task <XmlReader> GetXmlContent(HttpResponseMessage response, string trackingId) { try { if (response.Content == null) { return(XmlReader.Create(new StringReader(string.Empty))); } var xmlReader = XmlReader.Create(await response.Content.ReadAsStreamAsync()); if (xmlReader.ReadToFollowing("entry")) { if (xmlReader.ReadToDescendant("content")) { xmlReader.ReadStartElement(); } } return(xmlReader); } catch (XmlException ex) { throw ExceptionsUtility.HandleXmlException(ex, trackingId); } }
private static T GetModelFromResponse <T>(XmlReader xmlReader, string trackingId) where T : class { var serializer = new DataContractSerializer(typeof(T)); try { using (xmlReader) { return((T)serializer.ReadObject(xmlReader)); } } catch (SerializationException ex) when(ex.InnerException is XmlException xmlException) { throw ExceptionsUtility.HandleXmlException(xmlException, trackingId); } }
private async Task <HttpResponseMessage> SendAsync(Func <HttpRequestMessage> generateHttpRequestMessage, CancellationToken cancellationToken) { var trackingId = Guid.NewGuid().ToString(); var httpRequestMessage = generateHttpRequestMessage(); httpRequestMessage.Headers.Add(TrackingIdHeaderKey, trackingId); try { var response = await _httpClient.SendAsync(httpRequestMessage, cancellationToken).ConfigureAwait(false); response.Headers.Add(TrackingIdHeaderKey, trackingId); if (response.IsSuccessStatusCode) { return(response); } else { throw await response.TranslateToMessagingExceptionAsync(trackingId).ConfigureAwait(false); } } catch (HttpRequestException ex) { var innerException = ex.GetBaseException(); if (innerException is SocketException socketException) { throw ExceptionsUtility.HandleSocketException(socketException, OperationTimeout.Milliseconds, trackingId); } else { throw ExceptionsUtility.HandleUnexpectedException(ex, trackingId); } } catch (XmlException ex) { throw ExceptionsUtility.HandleXmlException(ex, trackingId); } }