Пример #1
0
        /// <summary>
        /// HTTP request with byte return type
        /// For a more detailed documentation see: https://github.com/FrendsPlatform/Frends.Web#HttpRequestBytes
        /// </summary>
        /// <param name="input">Input parameters</param>
        /// <param name="options">Optional parameters with default values</param>
        /// <returns>Object with the following properties: string BodyBytes, Dictionary(string,string) Headers. int StatusCode</returns>
        public static async Task <object> HttpRequestBytes([CustomDisplay(DisplayOption.Tab)] Input input, [CustomDisplay(DisplayOption.Tab)] Options options, CancellationToken cancellationToken)
        {
            using (var handler = new WebRequestHandler())
            {
                cancellationToken.ThrowIfCancellationRequested();
                handler.SetHandleSettingsBasedOnOptions(options);

                using (var httpClient = new HttpClient(handler))
                {
                    var responseMessage = await GetHttpRequestResponseAsync(httpClient, input, options, cancellationToken).ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();

                    var response = new HttpByteResponse()
                    {
                        BodyBytes   = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false),
                        ContentType = responseMessage.Content.Headers.ContentType,
                        StatusCode  = (int)responseMessage.StatusCode,
                        Headers     = GetResponseHeaderDictionary(responseMessage.Headers, responseMessage.Content.Headers)
                    };

                    if (!responseMessage.IsSuccessStatusCode && options.ThrowExceptionOnErrorResponse)
                    {
                        throw new WebException($"Request to '{input.Url}' failed with status code {(int)responseMessage.StatusCode}.");
                    }

                    return(response);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// For a more detailed documentation see: https://github.com/FrendsPlatform/Frends.Web#RestRequest
        /// </summary>
        /// <param name="input">Input parameters</param>
        /// <param name="options">Optional parameters with default values</param>
        /// <returns>Object with the following properties: JToken Body. Dictionary(string,string) Headers. int StatusCode</returns>
        public static async Task <object> RestRequest([CustomDisplay(DisplayOption.Tab)] Input input, [CustomDisplay(DisplayOption.Tab)] Options options, CancellationToken cancellationToken)
        {
            using (var handler = new WebRequestHandler())
            {
                cancellationToken.ThrowIfCancellationRequested();
                handler.SetHandleSettingsBasedOnOptions(options);

                using (var httpClient = new HttpClient(handler))
                {
                    httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

                    var responseMessage = await GetHttpRequestResponseAsync(httpClient, input, options, cancellationToken).ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();

                    var response = new RestResponse
                    {
                        Body       = TryParseRequestStringResultAsJToken(await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false)),
                        StatusCode = (int)responseMessage.StatusCode,
                        Headers    = GetResponseHeaderDictionary(responseMessage.Headers, responseMessage.Content.Headers)
                    };

                    if (!responseMessage.IsSuccessStatusCode && options.ThrowExceptionOnErrorResponse)
                    {
                        throw new WebException($"Request to '{input.Url}' failed with status code {(int)responseMessage.StatusCode}. Response body: {response.Body}");
                    }

                    return(response);
                }
            }
        }
        /// <summary>
        /// Send file using StreamContent
        /// </summary>
        /// <param name="input">Input parameters</param>
        /// <param name="options">Optional parameters with default values</param>
        /// <returns>Object with the following properties: JToken Body. Dictionary(string,string) Headers. int StatusCode</returns>
        /// public static bool Delete([PropertyTab] string fileName, [PropertyTab] OptionsClass options)
        public static async Task <object> PostFile([PropertyTab] Input input, [PropertyTab] Options options, CancellationToken cancellationToken)
        {
            using (var handler = new WebRequestHandler())
            {
                handler.SetHandleSettingsBasedOnOptions(options);

                using (var httpClient = new HttpClient(handler))
                {
                    var responseMessage = await GetHttpRequestResponseAsync(httpClient, input, options, cancellationToken).ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();

                    string body = string.Empty;
                    IEnumerable <KeyValuePair <string, IEnumerable <string> > > contentHeaders = new Dictionary <string, IEnumerable <string> >();

                    if (responseMessage.Content != null)
                    {
                        body = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                        contentHeaders = responseMessage.Content.Headers;
                    }
                    var response = new Response
                    {
                        Body       = body,
                        StatusCode = (int)responseMessage.StatusCode,
                        Headers    = GetResponseHeaderDictionary((IEnumerable <KeyValuePair <string, IEnumerable <string> > >)responseMessage.Headers ?? new Dictionary <string, IEnumerable <string> >(), contentHeaders)
                    };

                    if (!responseMessage.IsSuccessStatusCode && options.ThrowExceptionOnErrorResponse)
                    {
                        throw new WebException($"Request to '{input.Url}' failed with status code {(int)responseMessage.StatusCode}. Response body: {response.Body}");
                    }

                    return(response);
                }
            }
        }