Пример #1
0
        /// <summary>
        /// Remove data by web api
        /// </summary>
        /// <param name="controller">Controller name</param>
        /// <param name="data">Request data</param>
        /// <returns>
        /// <see cref="WebApiResponse"/>s object containing the web api response.
        /// </returns>
        protected WebApiResponse RemoveDataByApi(string controller, string data)
        {
            var request = new WebApiRequest <string>(controller,
                                                     HttpMethod.Delete, data);

            var response = _webApiClient
                           .Send <string>(request);

            ValidatePermissions(response);

            return(response);
        }
Пример #2
0
        /// <summary>
        /// Retrive data by web api
        /// </summary>
        /// <typeparam name="T">Request data type</typeparam>
        /// <param name="controller">Controller name</param>
        /// <param name="data">Request data</param>
        /// <returns>
        /// <see cref="WebApiResponse"/>s object containing the web api response.
        /// </returns>
        protected WebApiResponse RetriveDataByApi <T>(string controller, T data)
            where T : class
        {
            var request = new WebApiRequest <T>(controller,
                                                HttpMethod.Get, data);

            var response = _webApiClient
                           .Send <T>(request);

            ValidatePermissions(response);

            return(response);
        }
Пример #3
0
        public WebApiResponse Send <T>(WebApiRequest <T> request)
            where T : class
        {
            WebApiResponse result = new WebApiResponse();

            Uri uri = null;

            try
            {
                var cookieContainer = new CookieContainer();
                var handler         = new HttpClientHandler
                {
                    UseCookies            = true,
                    UseDefaultCredentials = true,
                    CookieContainer       = cookieContainer
                };

                var client = new HttpClient(handler);

                string url = string.Empty;

                HttpResponseMessage response = null;

                if (request.HttpMethod == HttpMethod.Get)
                {
                    url = String.Format("http://{0}/{1}/{2}",
                                        _address,
                                        request.Controller,
                                        ((request.Value != null && request.Value is String)
                            ? (request.Value  as String)
                            : String.Empty));

                    url = url.Replace("=/", "=");
                }
                else if (request.HttpMethod == HttpMethod.Post)
                {
                    url = String.Format("http://{0}/{1}",
                                        _address,
                                        request.Controller);
                }
                else if (request.HttpMethod == HttpMethod.Put)
                {
                    url = String.Format("http://{0}/{1}",
                                        _address,
                                        request.Controller);
                }
                else if (request.HttpMethod == HttpMethod.Delete)
                {
                    url = String.Format("http://{0}/{1}/{2}",
                                        _address,
                                        request.Controller,
                                        (request.Value as String));
                }

                if (!String.IsNullOrEmpty(url))
                {
                    uri = new Uri(url);
                }

                _logger.LogInfo(String.Format("{0} {1}\r\nParams:",
                                              request.HttpMethod.Method, url));

                if (!(request.Value is String))
                {
                    _logger.LogInfoObject(request.Value);
                }

                if (!String.IsNullOrEmpty(_sessionValue))
                {
                    cookieContainer.Add(uri, new Cookie(_sessionKey, _sessionValue));

                    _logger.LogInfo(String.Format("Cookies:\r\n{0}={1}",
                                                  _sessionKey, _sessionValue));
                }

                if (request.HttpMethod == HttpMethod.Get)
                {
                    response = client
                               .GetAsync(url)
                               .Result;
                }
                else if (request.HttpMethod == HttpMethod.Post)
                {
                    response = client
                               .PostAsJsonAsync <T>(url, request.Value)
                               .Result;
                }
                else if (request.HttpMethod == HttpMethod.Put)
                {
                    response = client
                               .PutAsJsonAsync <T>(url, request.Value)
                               .Result;
                }
                else if (request.HttpMethod == HttpMethod.Delete)
                {
                    response = client
                               .DeleteAsync(url)
                               .Result;
                }

                if (response != null)
                {
                    response
                    .EnsureSuccessStatusCode();

                    var serverResponse = response
                                         .Content
                                         .ReadAsStringAsync()
                                         .Result;

                    var responseCookies = cookieContainer
                                          .GetCookies(uri)
                                          .Cast <Cookie>();

                    foreach (Cookie cookie in responseCookies)
                    {
                        if (cookie.Name.ToLower() == _sessionKey.ToLower())
                        {
                            _sessionValue = cookie.Value;
                            break;
                        }
                    }

                    result.Code = response
                                  .StatusCode;

                    result.Response = serverResponse;

                    result.Result = true;

                    _logger.LogInfo(String.Format("Http code: {0}\r\nResponse:\r\n{1}",
                                                  result.Code, result.Response));
                }
            }
            catch (HttpRequestException hre)
            {
                _logger.LogError(hre);

                var msg = hre.Message;

                if (msg.Contains("404 "))
                {
                    result.Code = HttpStatusCode
                                  .NotFound;
                }
                else if (msg.Contains("401 "))
                {
                    result.Code = HttpStatusCode
                                  .Unauthorized;
                }
                else if (msg.Contains("403 "))
                {
                    result.Code = HttpStatusCode
                                  .Forbidden;
                }
                else if (msg.Contains("500 "))
                {
                    result.Code = HttpStatusCode
                                  .InternalServerError;
                }

                result.Response = msg;

                result.Result = false;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex);

                result.Response = ex
                                  .ToString();
                result
                .Result = false;
            }

            return(result);
        }