Exemple #1
0
        private string ConvertToJson(BodyParameters parameters)
        {
            var jsonSettings = CreateJsonSerializerSettings();
            var jsonString   = JsonConvert.SerializeObject(parameters, jsonSettings);

            return(jsonString);
        }
Exemple #2
0
        /// <summary>
        /// Executes given HTTP method and saves returned data to file. Method is capable of downloading large files.
        /// Data is not stored in memory, but is written directly to file on disk.
        /// </summary>
        /// <param name="targetFile">Returned data will be written to this file.</param>
        /// <param name="url">Full resource URL.</param>
        /// <param name="method">HTTP method.</param>
        /// <param name="cancellationToken"></param>
        /// <param name="queryParameters">URL parameters.</param>
        /// <param name="bodyParameters">Parameters, that will be sent in body of the request.</param>
        /// <returns></returns>
        protected virtual async Task <ApiCallResponse> DownloadToFileAsync(string targetFile, Uri url, HttpMethod method,
                                                                           CancellationToken cancellationToken, QueryParameters queryParameters = null, BodyParameters bodyParameters = null)
        {
            using (var requestMessage = ConstructRequestMessage(method, url, queryParameters, bodyParameters))
            {
                using (var response = await _client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead,
                                                              cancellationToken))
                {
                    var apiCallResponse = await ProcessResponseAsync(response, false);

                    using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
                    {
                        using (Stream streamToWriteTo = File.Open(targetFile, FileMode.Create))
                        {
                            await streamToReadFrom.CopyToAsync(streamToWriteTo);

                            return(apiCallResponse);
                        }
                    }
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Executes given HTTP method. Does not expect any response content.
 /// </summary>
 /// <param name="url">Full resource URL.</param>
 /// <param name="method">HTTP method.</param>
 /// <param name="cancellationToken"></param>
 /// <param name="queryParameters">URL parameters.</param>
 /// <param name="bodyParameters">Parameters, that will be sent in body of the request.</param>
 /// <returns></returns>
 protected virtual async Task <ApiCallResponse> SendAsync(Uri url, HttpMethod method, CancellationToken cancellationToken,
                                                          QueryParameters queryParameters = null, BodyParameters bodyParameters = null)
 {
     using (var requestMessage = ConstructRequestMessage(method, url, queryParameters, bodyParameters))
     {
         using (var response = await _client.SendAsync(requestMessage, cancellationToken))
         {
             return(await ProcessResponseAsync(response));
         }
     }
 }
Exemple #4
0
        private HttpRequestMessage ConstructRequestMessage(HttpMethod method, Uri url, QueryParameters queryParameters, BodyParameters bodyParameters)
        {
            var urlString = ConstructUrlString(url, queryParameters);
            var request   = new HttpRequestMessage(method, urlString);
            var hasBody   = bodyParameters != null && bodyParameters.GetParameters().Any();

            if (hasBody)
            {
                var jsonString = ConvertToJson(bodyParameters);
                request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
            }
            return(request);
        }