Demo entry 2843199

RR Error

   

Submitted by Alexander on Oct 19, 2015 at 16:40
Language: C#. Code size: 1.8 kB.

 private static async Task<TResponse> Post<TRequest, TResponse>(string serviceUrl, string integratorId, Guid integratorKey, TRequest request, Func<string, WebException> faultHandler)
            where TResponse : class
            where TRequest : class
        {
            Contract.Requires<ArgumentException>(!String.IsNullOrEmpty(serviceUrl));
            Contract.Requires<ArgumentException>(!String.IsNullOrEmpty(integratorId));
            Contract.Requires<ArgumentException>(integratorKey != Guid.Empty);
            Contract.Requires<ArgumentNullException>(faultHandler != null, "faultHandler");

            //execute async method
            var contentBody = Serializer.Serialize(request);
            var contentResponse = await Send(serviceUrl, integratorId, integratorKey, (httpRequest, url) => httpRequest.PostAsync(url, new StringContent(contentBody, Encoding.UTF8, ResponseAndRequestFormat)));
            if (contentResponse == null)
                throw new WebException("Respironics Service is unavailable");

            if (!contentResponse.IsSuccessStatusCode && contentResponse.Content == null)
                throw new WebException(contentResponse.StatusCode.ToString());

            var content = await contentResponse.Content.ReadAsStringAsync();

            if (!contentResponse.IsSuccessStatusCode || contentResponse.Content == null)
                throw faultHandler(content);

            //old line
//          return !string.IsNullOrWhiteSpace(content) ? Serializer.Deserialize<TResponse>(content) : default(TResponse);
            
            //new lines
            return contentResponse.IsSuccessStatusCode 
                && !string.IsNullOrWhiteSpace(content) ? Serializer.Deserialize<TResponse>(content) : default(TResponse);
        }

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).