private static Dictionary <string, Type> ConvertMappingsResponseToFieldsAndTypes(
            GetEnumerationMappingsResponse getEnumerationMappingsResponse)
        {
            Dictionary <string, Type> toReturn = null;

            toReturn = getEnumerationMappingsResponse
                       .MappingsResult
                       .Mappings
                       .ToDictionary(
                x => x.Key,
                x =>
            {
                Type type = null;

                string typeStr = x.Value.FirstOrDefault();

                type = Type.GetType(typeStr);

                if (type == null)
                {
                    throw new InvalidMappingTypeException(typeStr);
                }

                return(type);
            });

            return(toReturn);
        }
Exemplo n.º 2
0
        private async Task <IActionResult> ProcessWellFormedRequestAsync(
            GetEnumerationMappingsRequest getEnumerationMappingsRequest,
            CancellationToken cancellationToken)
        {
            IActionResult toReturn = null;

            this.loggerWrapper.Debug(
                $"Invoking {nameof(IGetEnumerationMappingsProcessor)} with " +
                $"{getEnumerationMappingsRequest}...");

            GetEnumerationMappingsResponse getEnumerationMappingsResponse =
                await this.getEnumerationMappingsProcessor.GetEnumerationMappingsAsync(
                    getEnumerationMappingsRequest,
                    cancellationToken)
                .ConfigureAwait(false);

            this.loggerWrapper.Info(
                $"{nameof(IGetEnumerationMappingsProcessor)} invoked with " +
                $"success: {getEnumerationMappingsResponse}.");

            Dictionary <string, string[]> mappings =
                getEnumerationMappingsResponse.MappingsResult.Mappings;

            if (mappings.Any())
            {
                this.loggerWrapper.Debug(
                    $"Looks like we got some results: " +
                    $"{getEnumerationMappingsResponse}. Returning as JSON.");

                toReturn = new JsonResult(getEnumerationMappingsResponse);
            }
            else
            {
                EnumerationKey enumerationsKey =
                    getEnumerationMappingsRequest.EnumerationsKey;

                this.loggerWrapper.Info(
                    $"No results found for {enumerationsKey}!");

                string name    = enumerationsKey.Name;
                string adapter = enumerationsKey.Adapter;

                toReturn = this.httpErrorBodyResultProvider.GetHttpErrorBodyResult(
                    HttpStatusCode.NotFound,
                    1,
                    name,
                    adapter);
            }

            return(toReturn);
        }
        /// <inheritdoc />
        public async Task <GetEnumerationMappingsResponse> GetEnumerationMappingsAsync(
            string enumerationName,
            string adapterName,
            CancellationToken cancellationToken)
        {
            GetEnumerationMappingsResponse toReturn = null;

            string getEnumerationValuesPath = string.Format(
                CultureInfo.InvariantCulture,
                GetEnumerationValuesPath,
                enumerationName,
                adapterName);

            Uri getEnumerationValuesUri = new Uri(
                getEnumerationValuesPath,
                UriKind.Relative);

            RestRequest restRequest = new RestRequest(
                getEnumerationValuesUri,
                Method.GET);

            SpiExecutionContext spiExecutionContext =
                this.spiExecutionContextManager.SpiExecutionContext;

            restRequest.AppendContext(spiExecutionContext);

            IRestResponse restResponse =
                await this.restClient.ExecuteTaskAsync(
                    restRequest,
                    cancellationToken)
                .ConfigureAwait(false);

            if (!restResponse.IsSuccessful)
            {
                this.ParseErrorInformationThrowException(restResponse);
            }

            string responseBody = restResponse.Content;

            toReturn =
                JsonConvert.DeserializeObject <GetEnumerationMappingsResponse>(
                    responseBody);

            return(toReturn);
        }
        /// <inheritdoc />
        public async Task <GetEnumerationMappingsResponse> GetEnumerationMappingsAsync(
            GetEnumerationMappingsRequest getEnumerationMappingsRequest,
            CancellationToken cancellationToken)
        {
            GetEnumerationMappingsResponse toReturn = null;

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

            EnumerationKey enumerationsKey =
                getEnumerationMappingsRequest.EnumerationsKey;

            string enumerationsKeyStr = enumerationsKey.ExportToString();

            this.loggerWrapper.Debug(
                $"Pulling back {nameof(MappingsResult)} for " +
                $"{nameof(enumerationsKey)} {enumerationsKey} " +
                $"(\"{enumerationsKeyStr}\") from the " +
                $"{nameof(ICacheManager)}...");

            object unboxedMappingsResult = await this.cacheManager.GetAsync(
                enumerationsKeyStr,
                cancellationToken)
                                           .ConfigureAwait(false);

            MappingsResult mappingsResult =
                unboxedMappingsResult as MappingsResult;

            // Result will be non-null.
            this.loggerWrapper.Info(
                $"{nameof(MappingsResult)} pulled back from the " +
                $"{nameof(ICacheManager)}: {mappingsResult}.");

            toReturn = new GetEnumerationMappingsResponse()
            {
                MappingsResult = mappingsResult,
            };

            return(toReturn);
        }
        private async Task <Census> GetCensusAggregates(
            GetCensusRequest getCensusRequest,
            DatasetQueryFile datasetQueryFile,
            CancellationToken cancellationToken)
        {
            Census toReturn = null;

            CensusIdentifier censusIdentifier =
                getCensusRequest.CensusIdentifier;

            // Get an entire list of aggregation fields that we can use.
            Dictionary <string, Type> aggregationFieldsAndTypes = null;

            if (this.aggregationFieldsCache.AggregationFieldsAndTypes == null)
            {
                this.loggerWrapper.Debug(
                    "Fetching available aggregation mappings from the " +
                    "Translator API...");

                GetEnumerationMappingsResponse getEnumerationMappingsResponse =
                    await this.translationApiAdapter.GetEnumerationMappingsAsync(
                        this.aggregationFieldsEnumerationName,
                        this.aggregationFieldsAdapterName,
                        cancellationToken)
                    .ConfigureAwait(false);

                aggregationFieldsAndTypes =
                    ConvertMappingsResponseToFieldsAndTypes(
                        getEnumerationMappingsResponse);

                this.loggerWrapper.Info(
                    $"Aggregation fields and types obtained - " +
                    $"{aggregationFieldsAndTypes.Count} in total.");

                this.aggregationFieldsCache.AggregationFieldsAndTypes =
                    aggregationFieldsAndTypes;
            }

            aggregationFieldsAndTypes =
                this.aggregationFieldsCache.AggregationFieldsAndTypes;

            IEnumerable <string> aggregationFields =
                aggregationFieldsAndTypes.Keys.ToList();

            string parameterName  = censusIdentifier.ParameterName;
            string parameterValue = censusIdentifier.ParameterValue;

            Dictionary <string, AggregateQuery> aggregateQueries =
                getCensusRequest.AggregateQueries;

            AssertFieldsAreSupported(aggregationFields, aggregateQueries);

            this.loggerWrapper.Debug(
                $"Fetching {nameof(Census)} using {datasetQueryFile} and " +
                $"supplied aggregate queries...");

            toReturn = await this.censusAdapter.GetCensusAsync(
                aggregationFields,
                datasetQueryFile,
                aggregateQueries,
                parameterName,
                parameterValue,
                this.BuildCensusResults,
                cancellationToken)
                       .ConfigureAwait(false);

            return(toReturn);
        }