public async Task <AddFilterResult> Execute(string requestUrl, AddFilterRequest addFilterRequest, string authorizationHeaderValue = null)
        {
            if (string.IsNullOrWhiteSpace(requestUrl))
            {
                throw new ArgumentNullException(nameof(requestUrl));
            }

            if (addFilterRequest == null)
            {
                throw new ArgumentNullException(nameof(addFilterRequest));
            }

            var httpClient = _httpClientFactory.GetHttpClient();
            var json       = JsonConvert.SerializeObject(addFilterRequest, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });
            var request = new HttpRequestMessage
            {
                Method     = HttpMethod.Post,
                Content    = new StringContent(json),
                RequestUri = new Uri(requestUrl)
            };

            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            if (!string.IsNullOrWhiteSpace(authorizationHeaderValue))
            {
                request.Headers.Add("Authorization", "Bearer " + authorizationHeaderValue);
            }

            var result = await httpClient.SendAsync(request).ConfigureAwait(false);

            var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

            try
            {
                result.EnsureSuccessStatusCode();
            }
            catch (Exception)
            {
                return(new AddFilterResult
                {
                    ContainsError = true,
                    Error = JsonConvert.DeserializeObject <ErrorResponse>(content),
                    HttpStatus = result.StatusCode
                });
            }

            return(new AddFilterResult
            {
                Content = JsonConvert.DeserializeObject <AddFilterResponse>(content),
                HttpStatus = result.StatusCode
            });
        }
        private static bool CheckParameter(AddFilterRequest addFilterRequest, out IActionResult actionResult)
        {
            actionResult = null;
            if (string.IsNullOrWhiteSpace(addFilterRequest.Name))
            {
                actionResult = BuildMissingParameter("name");
                return(false);
            }

            return(true);
        }
        public static FilterAggregate ToParameter(this AddFilterRequest addFilterRequest)
        {
            if (addFilterRequest == null)
            {
                throw new ArgumentNullException(nameof(addFilterRequest));
            }

            return(new FilterAggregate
            {
                Name = addFilterRequest.Name,
                Rules = addFilterRequest.Rules == null ? new List <FilterAggregateRule>() : addFilterRequest.Rules.Select(r => r.ToParameter())
            });
        }
        public async Task <IActionResult> AddFilter([FromBody] AddFilterRequest addFilterRequest)
        {
            if (addFilterRequest == null)
            {
                return(BuildMissingParameter(nameof(addFilterRequest)));
            }

            IActionResult errorResult = null;

            if (!CheckParameter(addFilterRequest, out errorResult))
            {
                return(errorResult);
            }

            var id = await _filterRepository.Add(addFilterRequest.ToParameter());

            return(new OkObjectResult(new AddFilterResponse
            {
                Id = id
            }));
        }
Пример #5
0
 public Task <AddFilterResult> Add(string requestUrl, AddFilterRequest addFilterRequest, string authorizationHeaderValue = null)
 {
     return(_addFilterOperation.Execute(requestUrl, addFilterRequest, authorizationHeaderValue));
 }