public void SaveWorkBook(string path)
 {
     StorageHelper.CreateDirectory(path);
     _xBook.Save(path);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Base handler for making the api request
        /// </summary>
        /// <param name="httpMethod"></param>
        /// <param name="requestToDownloadFile"></param>
        /// <returns></returns>
        private async Task <TestApiResponse> SendRequestAsync(HttpMethod httpMethod, string requestToDownloadFile = null)
        {
            var httpClientHandler = new HttpClientHandler
            {
                AllowAutoRedirect      = true,
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                CookieContainer        = new CookieContainer(),
                UseCookies             = true
            };

            if (_handleSslCertificateErrors)
            {
                httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
                {
                    if (_logSslCertificateErrors)
                    {
                        Console.WriteLine($"Problem with the Certificate: {cert.Subject}");
                        Console.WriteLine($"Sender: {sender}");
                        Console.WriteLine($"cert: {cert}");
                        Console.WriteLine($"chain: {chain}");
                        Console.WriteLine($"sslPolicyErrors: {sslPolicyErrors}");
                    }
                    return(true);
                };
            }

            if (NtmlAuthentication)
            {
                httpClientHandler.UseDefaultCredentials = true;
            }

            var client = new HttpClient(httpClientHandler);

            if (HeaderAuthentication != null)
            {
                client.DefaultRequestHeaders.Authorization = HeaderAuthentication;
            }

            client.Timeout = TimeSpan.FromSeconds(SeAppConfig.DefaultApiResponseTimeoutWaitPeriodInSeconds);

            foreach (var kvp in Headers)
            {
                client.DefaultRequestHeaders.TryAddWithoutValidation(kvp.Key, kvp.Value);
            }

            foreach (Cookie cookie in Cookies)
            {
                httpClientHandler.CookieContainer.Add(cookie);
            }

            HttpResponseMessage httpResponseMessage = null;

            if (httpMethod == HttpMethod.Post)
            {
                if (JsonBody.IsEmpty() && Body.IsEmpty())
                {
                    var content = new FormUrlEncodedContent(PostParams.Select(kvp => new KeyValuePair <string, string>(kvp.Key, (string)kvp.Value)));
                    httpResponseMessage = await client.PostAsync(Uri.AbsoluteUri, content);
                }
                else if (JsonBody.HasValue())
                {
                    var request = new HttpRequestMessage(httpMethod, Uri);
                    request.Content     = new StringContent(JsonBody, Encoding.UTF8, "application/json");
                    httpResponseMessage = await client.SendAsync(request);
                }
                else if (Body.HasValue())
                {
                    var request = new HttpRequestMessage(httpMethod, Uri);
                    request.Content     = new StringContent(Body, Encoding.UTF8, "text/xml");
                    httpResponseMessage = await client.SendAsync(request);
                }
            }
            else if (httpMethod == HttpMethod.Put)
            {
                if (JsonBody.IsEmpty() && Body.IsEmpty())
                {
                    var content = new FormUrlEncodedContent(PostParams.Select(kvp => new KeyValuePair <string, string>(kvp.Key, (string)kvp.Value)));
                    httpResponseMessage = await client.PutAsync(Uri.AbsoluteUri, content);
                }
                else if (JsonBody.HasValue())
                {
                    var request = new HttpRequestMessage(httpMethod, Uri);
                    request.Content     = new StringContent(JsonBody, Encoding.UTF8, "application/json");
                    httpResponseMessage = await client.SendAsync(request);
                }
                else if (Body.HasValue())
                {
                    var request = new HttpRequestMessage(httpMethod, Uri);
                    request.Content     = new StringContent(Body, Encoding.UTF8, "text/xml");
                    httpResponseMessage = await client.SendAsync(request);
                }
            }
            else if (httpMethod == HttpMethod.Get)
            {
                Console.WriteLine($"Requesting GET: {Uri.AbsoluteUri}");
                httpResponseMessage = requestToDownloadFile.IsEmpty() ? await client.GetAsync(Uri) : await client.GetAsync(Uri, HttpCompletionOption.ResponseHeadersRead);
            }
            else if (httpMethod == HttpMethod.Delete)
            {
                if (JsonBody.IsEmpty())
                {
                    Console.WriteLine($"Requesting DELETE: {Uri.AbsoluteUri}");
                    httpResponseMessage = await client.DeleteAsync(Uri);
                }
                else
                {
                    var request = new HttpRequestMessage(httpMethod, Uri);
                    request.Content     = new StringContent(JsonBody, Encoding.UTF8, "application/json");
                    httpResponseMessage = await client.SendAsync(request);
                }
            }

            var schemeHostAndPortPart = Uri.GetLeftPart(UriPartial.Authority);
            var uri = new Uri(schemeHostAndPortPart);

            if (requestToDownloadFile.HasValue())
            {
                StorageHelper.CreateDirectory(requestToDownloadFile);
                if (!File.Exists(requestToDownloadFile))
                {
                    using (Stream streamToReadFrom = await httpResponseMessage.Content.ReadAsStreamAsync())
                        using (Stream output = File.OpenWrite(requestToDownloadFile))
                        {
                            streamToReadFrom.CopyTo(output);
                        }
                }
                return(new TestApiResponse()
                {
                    ResponseCode = httpResponseMessage.StatusCode,
                    Cookies = httpClientHandler.CookieContainer.GetCookies(uri),
                    ResponseBody = null,
                    HttpResponseMessage = httpResponseMessage,
                });
            }

            var result = await httpResponseMessage.Content.ReadAsStringAsync();

            var response = new TestApiResponse()
            {
                ResponseCode        = httpResponseMessage.StatusCode,
                Cookies             = httpClientHandler.CookieContainer.GetCookies(uri),
                ResponseBody        = new TestApiBody(result),
                HttpResponseMessage = httpResponseMessage,
            };

            if (response.ResponseCode != HttpStatusCode.OK && response.ResponseCode != HttpStatusCode.Accepted)
            {
                Console.WriteLine($"Resposne status: {response.ResponseCode}, ResponseMessage: {response.ResponseBody.ContentString}");
            }

            return(response);
        }