Ejemplo n.º 1
0
        internal PepperiHttpClientResponse Get(
            string BaseUri,
            string RequestUri,
            Dictionary <string, string> dicQueryStringParameters,        //where, order by, any other parameters (page, page_size etc)
            string accept
            )
        {
            var HttpClient = new HttpClient(new LoggingHandler(this.Logger))
            {
                BaseAddress = new Uri(BaseUri)
            };

            //add authorization header (for basic authentiction, Oauth2 etc)
            AddAuthorizationHeader(HttpClient);

            //add: accept header
            HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));

            //add: custom request headers
            AddAuthenticationCustomRequestHeaders(HttpClient);

            //create query string (required by endpoint and authentication)
            string QueryString = BuildQueryString(dicQueryStringParameters, null);


            //send request
            HttpResponseMessage HttpResponseMessage = HttpClient.GetAsync(RequestUri + QueryString).Result;
            string body            = HttpResponseMessage.Content.ReadAsStringAsync().Result;
            var    ResponseHeaders = this.GetResponseHeaders(HttpResponseMessage);

            //Create result
            var PepperiHttpClientResponse = new PepperiHttpClientResponse(HttpResponseMessage.StatusCode, body, ResponseHeaders);

            return(PepperiHttpClientResponse);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Throws relevant exception, according to the status code
        /// </summary>
        /// <param name="PepperiHttpClientResponse"></param>
        internal void HandleError(PepperiHttpClientResponse PepperiHttpClientResponse)
        {
            if (PepperiHttpClientResponse.HttpStatusCode == System.Net.HttpStatusCode.OK || PepperiHttpClientResponse.HttpStatusCode == System.Net.HttpStatusCode.Created)
            {
                //on upsert (post): 200 status code is returned for Update    201 status code is returned for Insert
                return;
            }


            PepperiException PepperiException = null;

            try
            {
                ApiException ApiException = PepperiJsonSerializer.DeserializeOne <ApiException>(PepperiHttpClientResponse.Body);

                PepperiException = new PepperiException(
                    string.Format("{0} (error code ={1})",
                                  (ApiException != null && ApiException.fault != null && ApiException.fault.faultstring != null) ? ApiException.fault.faultstring : PepperiHttpClientResponse.Body,  //0
                                  ApiException != null && ApiException.fault != null && ApiException.fault.detail != null && ApiException.fault.detail.errorcode != null ?  ApiException.fault.detail.errorcode : string.Empty
                                  ),
                    ApiException,
                    PepperiHttpClientResponse.HttpStatusCode
                    );
            }
            catch (Exception e)
            {
                PepperiException = new PepperiException("Api returned Bad Request. Failed to Parse body as exception.\r\nBody:" +
                                                        PepperiHttpClientResponse.Body == null ? "" : PepperiHttpClientResponse.Body +
                                                        "\r\nParsing error: " +
                                                        e.Message);
            }

            throw PepperiException;
        }
        /// <summary>
        /// post string (eg, json)
        /// </summary>
        /// <param name="BaseUri"></param>
        /// <param name="RequestUri"></param>
        /// <param name="dicQueryStringParameters"></param>
        /// <param name="postBody"></param>
        /// <param name="contentType"></param>
        /// <param name="accept"></param>
        /// <returns></returns>
        internal PepperiHttpClientResponse PostStringContent(
            string BaseUri,
            string RequestUri,
            Dictionary <string, string> dicQueryStringParameters, //where, order by, any other parameters (page, page_size etc)
            string postBody,
            string contentType,                                   //eg, application/json
            string accept
            )
        {
            var HttpClient = new HttpClient(new LoggingHandler(this.Logger))
            {
                BaseAddress = new Uri(BaseUri)
            };

            //add authentication header (for basic authentiction, Oauth2 etc)
            AddAuthorizationHeader(HttpClient);


            //add: accept header
            HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));

            //add: custom request headers
            AddAuthenticationCustomRequestHeaders(HttpClient);

            HttpClient.Timeout = new TimeSpan(0, 5, 0);// by default wait 5 minutes

            //create query string (required by endpoint and authentication)
            string QueryString = BuildQueryString(dicQueryStringParameters, null);

            //send request
            StringContent StringContent = new StringContent(postBody, Encoding.UTF8);       //new StringContent(postBody, Encoding.UTF8, "application/json")

            StringContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);      //add content type header
            HttpResponseMessage HttpResponseMessage = HttpClient.PostAsync(RequestUri + QueryString, StringContent).Result;
            string body = HttpResponseMessage.Content.ReadAsStringAsync().Result;

            // uncomment in case of utf-8 isues
            //var byteArray = HttpResponseMessage.Content.ReadAsByteArrayAsync().Result;
            //string body = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            var ResponseHeaders = this.GetResponseHeaders(HttpResponseMessage);

            //Create result
            var PepperiHttpClientResponse = new PepperiHttpClientResponse(HttpResponseMessage.StatusCode, body, ResponseHeaders);

            return(PepperiHttpClientResponse);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="BaseUri"></param>
        /// <param name="RequestUri"></param>
        /// <param name="dicQueryStringParameters"></param>
        /// <param name="postBody"></param>
        /// <param name="contentType"></param>
        /// <param name="accept"></param>
        /// <returns></returns>
        /// <remarks>
        /// 1./https://msdn.microsoft.com/en-us/library/system.net.http.formurlencodedcontent(v=vs.118).aspx
        /// </remarks>
        internal PepperiHttpClientResponse PostFormUrlEncodedContent(
            string BaseUri,
            string RequestUri,
            Dictionary <string, string> dicQueryStringParameters, //where, order by, any other parameters (page, page_size etc)
            List <KeyValuePair <string, string> > postBody,
            string contentType,                                   //eg, application/x-www-form-urlencoded
            string accept
            )
        {
            var HttpClient = new HttpClient(new LoggingHandler(this.Logger))
            {
                BaseAddress = new Uri(BaseUri)
            };

            HttpClient.Timeout = new TimeSpan(0, 5, 0);// by default wait 5 minutes

            //add authentication header (for basic authentiction, Oauth2 etc)
            AddAuthorizationHeader(HttpClient);


            //add: accept header
            HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));

            //add: custom request headers
            AddAuthenticationCustomRequestHeaders(HttpClient);


            //create query string (required by endpoint and authentication)
            string QueryString = BuildQueryString(dicQueryStringParameters, null);

            //send request
            FormUrlEncodedContent FormUrlEncodedContent = new FormUrlEncodedContent(postBody);

            FormUrlEncodedContent.Headers.ContentType = new MediaTypeHeaderValue(contentType); //add content type header
            HttpResponseMessage HttpResponseMessage = HttpClient.PostAsync(RequestUri + QueryString, FormUrlEncodedContent).Result;
            string body            = HttpResponseMessage.Content.ReadAsStringAsync().Result;
            var    ResponseHeaders = this.GetResponseHeaders(HttpResponseMessage);

            //Create result
            var PepperiHttpClientResponse = new PepperiHttpClientResponse(HttpResponseMessage.StatusCode, body, ResponseHeaders);

            return(PepperiHttpClientResponse);
        }