Esempio n. 1
0
        /// <summary>
        /// Perform an OPTIONS request
        /// </summary>
        /// <param name="url">The API Host url</param>
        /// <param name="args">Query string args</param>
        /// <param name="headers">Dictionary of _headers</param>
        /// <param name="content">The message content</param>
        /// <exception cref="ArgumentException">The url is not valid</exception>
        /// <exception cref="InvalidOperationException">Probably misused header value</exception>
        /// <returns>RESTResponse, null if something goes wrong</returns>
        public async Task <IRESTResponse> PerformOptionsRequestAsync(string url, Dictionary <string, string> args = null, Dictionary <string, string> headers = null, HttpContent content = null)
        {
            IRESTResponse      response          = null;
            HttpRequestHeaders headersCollection = null;
            string             queryArgs         = "";

            try
            {
                headersCollection = SetHeaders(_httpClient.DefaultRequestHeaders, headers);
                if (args != null)
                {
                    queryArgs = "?";
                    foreach (var item in args)
                    {
                        queryArgs = queryArgs + item.Key + "=" + item.Value + "&";
                    }
                    queryArgs = queryArgs.Substring(0, queryArgs.Length - 1);
                }
                //Send the OPTIONS request
                this.Log($"Performing OPTIONS Request to: {url} with args:{queryArgs}");
                HttpResponseMessage httpResponse = await _httpClient.PatchAsync(new Uri(url + queryArgs), content, this._cancellationToken);

                this.Log($"With response status code: {httpResponse.StatusCode}");
                response = GetResponse(httpResponse);
            }
            catch (Exception)
            {
                throw;
            }
            return(response);
        }
Esempio n. 2
0
        /// <summary>
        /// Perform a HEAD request
        /// </summary>
        /// <param name="url">The api url</param>
        /// <param name="args">The querystring args</param>
        /// <param name="headers">The _headers</param>
        /// <exception cref="ArgumentException">The url is not valid</exception>
        /// <exception cref="InvalidOperationException">Probably misused header value</exception>
        /// <returns>RESTResponse, null if something goes wrong</returns>
        public async Task <IRESTResponse> PerformHeadRequestAsync(string url, Dictionary <string, string> args = null, Dictionary <string, string> headers = null)
        {
            IRESTResponse      response          = null;
            HttpRequestHeaders headersCollection = null;
            string             queryArgs         = "";

            if (!this.IsValidUrl(url))
            {
                throw new ArgumentException("The current url is not valid");
            }
            try
            {
                headersCollection = SetHeaders(_httpClient.DefaultRequestHeaders, headers);
                if (args != null)
                {
                    queryArgs = "?";
                    foreach (var item in args)
                    {
                        queryArgs = queryArgs + item.Key + "=" + item.Value + "&";
                    }
                    queryArgs = queryArgs.Substring(0, queryArgs.Length - 1);
                }
                //Send the HEAD request
                this.Log($"Performing HEAD Request to: {url} with args:{queryArgs}");
                HttpResponseMessage httpResponse = await _httpClient.HeadAsync(new Uri(url + queryArgs), this._cancellationToken);

                this.Log($"With response status code: {httpResponse.StatusCode}");
                response = GetResponse(httpResponse);
            }
            catch (Exception)
            {
                throw;
            }
            return(response);
        }
Esempio n. 3
0
        /// <summary>
        /// Perform a Request by an IRESTRequest object
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <IRESTResponse> PerformRequestAsync(IRESTRequest request)
        {
            IRESTResponse response = null;

            switch (request.GetMethod())
            {
            case Method.POST:
            {
                response = await this.PerformPostRequestAsync(request.GetUrl(), request.GetQueryArgs(), request.GetHeaders(), request.GetContent());

                break;
            }

            case Method.PUT:
            {
                response = await this.PerformPutRequestAsync(request.GetUrl(), request.GetQueryArgs(), request.GetHeaders(), request.GetContent());

                break;
            }

            case Method.GET:
            {
                response = await this.PerformGetRequestAsync(request.GetUrl(), request.GetQueryArgs(), request.GetHeaders());

                break;
            }

            case Method.PATCH:
            {
                response = await this.PerformPatchRequestAsync(request.GetUrl(), request.GetQueryArgs(), request.GetHeaders(), request.GetContent());

                break;
            }

            case Method.OPTIONS:
            {
                response = await this.PerformOptionsRequestAsync(request.GetUrl(), request.GetQueryArgs(), request.GetHeaders(), request.GetContent());

                break;
            }

            case Method.HEAD:
            {
                response = await this.PerformHeadRequestAsync(request.GetUrl(), request.GetQueryArgs(), request.GetHeaders());

                break;
            }

            case Method.DELETE:
            {
                response = await this.PerformDeleteRequestAsync(request.GetUrl(), request.GetQueryArgs(), request.GetHeaders());

                break;
            }
            }
            return(response);
        }