Exemplo n.º 1
0
        /// <summary>
        /// A Get method to fetch the stream from the URI
        /// </summary>
        /// <typeparam name="TRequest">The Request Object to pass to</typeparam>
        /// <param name="httpMethod">HTTP Method</param>
        /// <param name="uri">The Source URL from where Stream can be downloaded</param>
        /// <param name="request">The input object which should be passed as part of request</param>
        /// <returns></returns>
        public Stream GetStream <TRequest>(HttpMethod httpMethod, string uri, TRequest request) where TRequest : class
        {
            HttpClient httpClient = EVHttpClient.GetHttpClient();

            try
            {
                HttpResponseMessage httpResp;
                if (null == request)
                {
                    httpResp = httpClient.Send(httpMethod, uri);
                }
                else
                {
                    httpResp = httpClient.Send(httpMethod, uri,
                                               HttpContentExtensions.CreateXmlSerializable <TRequest>(request));
                }

                if (!httpResp.IsStatusIsSuccessful())
                {
                    throw new EVException().AddHttpResponse(httpResp);
                }


                // -- get the xml
                return(httpResp.Content.ReadAsStream());
            }
            catch (Exception exception)
            {
                CheckAndThrowWebException(exception);
                throw;
            }
        }
        /// <summary>
        /// This is a generic Http send method which takes data from
        /// stub if needed else calls the service and returns the data
        /// back to the caller
        /// </summary>
        /// <typeparam name="TRequest">Request type</typeparam>
        /// <typeparam name="TResponse">Response type</typeparam>
        /// <param name="httpMethod">HttpMethod type</param>
        /// <param name="uri">Uri of stubbed filename/Rest web service</param>
        /// <param name="obj">Request type object</param>
        /// <returns>Response type object</returns>
        public TResponse SendUsingDataContract <TRequest, TResponse>(HttpMethod httpMethod, string uri, TRequest obj)
        {
            HttpResponseMessage httpResp = null;
            string xml = string.Empty;

            // -- try to get stub data
            xml = GetStubData(uri);

            if (string.IsNullOrEmpty(xml))
            {
                httpResp = obj == null?EVHttpClient.GetHttpClient().Send(httpMethod, uri) : EVHttpClient.GetHttpClient().Send(httpMethod, uri, HttpContentExtensions.CreateDataContract <TRequest>(obj));

                if (httpResp != null && !httpResp.IsStatusIsSuccessful())
                {
                    throw new EVException().AddHttpResponse(httpResp);
                }
                // -- get response data
                xml = GetResponseData(uri, httpResp);
            }

            return((TResponse)XmlUtility.DeserializeObject(xml, typeof(TResponse)));
        }
        /// <summary>
        /// A Get method to fetch the stream from the URI
        /// </summary>
        /// <typeparam name="TRequest">The Request Object to pass to</typeparam>
        /// <param name="httpMethod">HTTP Method</param>
        /// <param name="uri">The Source URL from where Stream can be downloaded</param>
        /// <param name="obj">The input object which should be passed as part of request</param>
        /// <returns></returns>
        public Stream GetStream <TRequest>(HttpMethod httpMethod, string uri, TRequest obj) where TRequest : class
        {
            HttpResponseMessage httpResp = null;
            string xml = string.Empty;

            // -- try to get stub data
            xml = GetStubData(uri);

            if (string.IsNullOrEmpty(xml))
            {
                httpResp = obj == null?EVHttpClient.GetHttpClient().Send(httpMethod, uri) : EVHttpClient.GetHttpClient().Send(httpMethod, uri, HttpContentExtensions.CreateXmlSerializable <TRequest>(obj));

                if (!httpResp.IsStatusIsSuccessful())
                {
                    throw new EVException().AddHttpResponse(httpResp);
                }

                // -- get response data
                return(httpResp.Content.ReadAsStream());
            }

            return(null);
        }