Exemplo n.º 1
0
        public async Task <EntityLinkBatchResult[]> GetLinksBatchAsync(TypedEntityReference[] entityReferences, DateTime?pointInTime, CancellationToken cancellationToken)
        {
            const string resource = "entities/links";

            _logger.Debug($"Looking up links in batch at {resource}");

            var json = JsonConvert.SerializeObject(new GetLinksBatchRequest
            {
                Entities    = entityReferences,
                PointInTime = pointInTime,
            });

            _logger.Debug($"Links request json going to {resource} is {json}");

            var httpRequest = new RestRequest(resource, Method.POST);

            httpRequest.AppendContext(_executionContextManager.SpiExecutionContext);
            httpRequest.AddParameter(string.Empty, json, "application/json", ParameterType.RequestBody);

            var response = await _restClient.ExecuteTaskAsync(httpRequest, cancellationToken);

            if (!response.IsSuccessful)
            {
                if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    HttpDetailedErrorBody detailedErrorBody = null;
                    try
                    {
                        detailedErrorBody = JsonConvert.DeserializeObject <HttpDetailedErrorBody>(response.Content);
                    }
                    catch (Exception ex)
                    {
                        _logger.Warning($"Received bad request from registry at {resource} however body was not a valid HttpDetailedErrorBody - {ex.Message}", ex);
                    }

                    if (detailedErrorBody != null)
                    {
                        throw new InvalidRequestException(
                                  detailedErrorBody.Message,
                                  detailedErrorBody.ErrorIdentifier,
                                  detailedErrorBody.Details);
                    }
                }
                throw new RegistryApiException(resource, response.StatusCode, response.Content);
            }
            _logger.Debug($"Entity links response json from {resource} is ${response.Content}");

            var linksResponse = JsonConvert.DeserializeObject <GetLinksBatchResponse>(response.Content);

            _logger.Debug($"Deserialized response from {resource} to {JsonConvert.SerializeObject(linksResponse)}");

            return(linksResponse.Entities);
        }
Exemplo n.º 2
0
        private async Task <SearchResultSet> SearchAsync(string resource, SearchRequest request, CancellationToken cancellationToken)
        {
            _logger.Debug($"Searching {resource}");

            var json = JsonConvert.SerializeObject(request);

            _logger.Debug($"Search request json going to {resource} is {json}");

            var httpRequest = new RestRequest(resource, Method.POST);

            httpRequest.AppendContext(_executionContextManager.SpiExecutionContext);
            httpRequest.AddParameter(string.Empty, json, "application/json", ParameterType.RequestBody);

            var response = await _restClient.ExecuteTaskAsync(httpRequest, cancellationToken);

            if (!response.IsSuccessful)
            {
                if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    HttpDetailedErrorBody detailedErrorBody = null;
                    try
                    {
                        detailedErrorBody = JsonConvert.DeserializeObject <HttpDetailedErrorBody>(response.Content);
                    }
                    catch (Exception ex)
                    {
                        _logger.Warning($"Received bad request from registry at {resource} however body was not a valid HttpDetailedErrorBody - {ex.Message}", ex);
                    }

                    if (detailedErrorBody != null)
                    {
                        throw new InvalidRequestException(
                                  detailedErrorBody.Message,
                                  detailedErrorBody.ErrorIdentifier,
                                  detailedErrorBody.Details);
                    }
                }
                throw new RegistryApiException(resource, response.StatusCode, response.Content);
            }
            _logger.Debug($"Search response json from {resource} is ${response.Content}");

            var resultset = JsonConvert.DeserializeObject <SearchResultSet>(response.Content);

            _logger.Debug($"Deserialized response from {resource} to {JsonConvert.SerializeObject(resultset)}");

            return(resultset);
        }