private async Task <HttpResponseMessage> SendApiRequest(FirstTitleRequestBase fTRequest, CancellationToken cancellationToken) { if (fTRequest is null) { throw new ArgumentNullException(nameof(fTRequest)); } if (fTRequest.FirstTitleCredential is null) { throw new ArgumentException(nameof(fTRequest.FirstTitleCredential)); } var client_ = _httpClient; using (var httpRequest = new HttpRequestMessage()) { if (string.IsNullOrEmpty(fTRequest.Content)) { httpRequest.Content = null; } else { var soapRequest = CreateSoapEnvelope(fTRequest.Content); httpRequest.Content = new StringContent(soapRequest.OuterXml, Encoding.UTF8); httpRequest.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("text/xml"); } httpRequest.Method = fTRequest.HttpMethod; httpRequest.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/soap+xml")); httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", GetBasicAuthValue(fTRequest.FirstTitleCredential)); httpRequest.Headers.Add("SOAPAction", fTRequest.SOAPAction); httpRequest.RequestUri = ServiceUri; return(await client_.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(true)); } }
public async Task <TResponse> Handle <TResponse>(FirstTitleRequestBase fTRequest, CancellationToken cancellationToken) where TResponse : class { if (fTRequest is null) { throw new ArgumentNullException(nameof(fTRequest)); } var isValidCredentials = await CheckCredentials(fTRequest.FirstTitleCredential); if (!isValidCredentials) { throw new InvalidFirstTitleCredentialsException(fTRequest.FirstTitleCredential); } var response = await SendApiRequest(fTRequest, cancellationToken); try { var headers = System.Linq.Enumerable.ToDictionary(response.Headers, h_ => h_.Key, h_ => h_.Value); if (response.Content != null && response.Content.Headers != null) { foreach (var item_ in response.Content.Headers) { headers[item_.Key] = item_.Value; } } var status_ = ((int)response.StatusCode).ToString(CultureInfo.InvariantCulture); if (status_ == "200" || status_ == "206") { if (response.Content == null) { return(null); } var rawStringContent = await response.Content.ReadAsStringAsync(); var xdoc = XDocument.Parse(rawStringContent); XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/"; XNamespace m = "http://ws.etitle.com.au/schemas"; var responseXml = xdoc.Element(soap + "Envelope").Element(soap + "Body") .Element(m + "TitleInsuranceResponse"); var message = responseXml.Elements().First(); message.Name = message.Name.LocalName; var content = message.ToString(); content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + content; using (var reader = new StringReader(content)) using (var xmlReader = new XmlTextReader(reader)) { var serializer = new XmlSerializer(typeof(TResponse)); var rawResponse = serializer.Deserialize(xmlReader); return((TResponse)rawResponse); } } else if (status_ == "400") { if (response.Content == null) { throw new FirstTitleException(); } using (var contentStream = await response.Content.ReadAsStreamAsync()) using (var xmlReader = XmlReader.Create(contentStream)) { ExceptionResponse exceptionResponse; var exceptionResponseSerializerv2 = new XmlSerializer(typeof(ExceptionResponse)); exceptionResponse = (ExceptionResponse)exceptionResponseSerializerv2.Deserialize(xmlReader); if (xmlReader != null) { xmlReader.Dispose(); } throw new FirstTitleException(exceptionResponse); } } else if (status_ == "401") { var responseData_ = response.Content == null ? null : await response.Content.ReadAsStringAsync().ConfigureAwait(false); throw new FirstTitleException("Unauthenticated", (int)response.StatusCode, responseData_, headers, null); } else if (status_ == "403") { var responseData_ = response.Content == null ? null : await response.Content.ReadAsStringAsync().ConfigureAwait(false); throw new FirstTitleException("Forbidden", (int)response.StatusCode, responseData_, headers, null); } else if (status_ == "500") { var responseData_ = response.Content == null ? null : await response.Content.ReadAsStringAsync().ConfigureAwait(false); throw new FirstTitleException("Internal Server Error", (int)response.StatusCode, responseData_, headers, null); } else if (status_ != "200" && status_ != "204") { var responseData_ = response.Content == null ? null : await response.Content.ReadAsStringAsync().ConfigureAwait(false); throw new FirstTitleException("The HTTP status code of the response was not expected (" + (int)response.StatusCode + ").", (int)response.StatusCode, responseData_, headers, null); } return(null); } finally { if (response != null) { response.Dispose(); } } }