Beispiel #1
0
        /// <summary>
        /// Get pokemon species details - only place which some kind of description
        ///   {pokeApi}/pokemon-species/{pokemonName}/
        /// Extract Flavor text by language - configured via AppSetting
        /// </summary>
        /// <param name="pokemonName">Pokemon name or yours, if you are one.</param>
        /// <returns>Pokemon Details with error, if any</returns>
        public async Task <PokemonDetails> GetPokemonDetailsAsync(string pokemonName)
        {
            try
            {
                _logger.LogDebug("Calling {methodName}, Getting details for {pokemon}", nameof(GetPokemonDetailsAsync), pokemonName);

                PokemonSpecies species = await _client.Get($"{_appSettings.PokeApi.Key}/{pokemonName.ToLower()}", new PokemonQueryParams());

                if (!(species is null) && species.FlavorTextEntries?.Count > 0)
                {
                    _logger.LogInformation("{methodName} called successfully, returned species data", nameof(GetPokemonDetailsAsync));

                    PokemonSpeciesFlavorTexts flavorTexts = species.FlavorTextEntries.Where(x => x.Language.Name == _appSettings.PokeApi.DefaultLanguage).FirstOrDefault();

                    if (flavorTexts is null || string.IsNullOrEmpty(flavorTexts?.FlavorText))
                    {
                        _logger.LogError("Error in {methodName} - Empty Flavor text for language {lang}", nameof(GetPokemonDetailsAsync), _appSettings.PokeApi.DefaultLanguage);
                        return(GetErrorResponse(StatusCodes.Status500InternalServerError, "API Response Error"));
                    }

                    return(new PokemonDetails
                    {
                        Name = species.Name,
                        Description = flavorTexts.FlavorText.Trim().Replace('\n', ' ')
                    });
                }
                else
                {
                    _logger.LogError("Error in {methodName} - Null/Empty response from PokeAPI", nameof(GetPokemonDetailsAsync));
                    return(GetErrorResponse(StatusCodes.Status500InternalServerError, "API Response Error"));
                }
            }
 private static string ParseLineBreaks(PokemonSpeciesFlavorTexts flavorTexts)
 => Regex.Replace(flavorTexts.FlavorText, @"\t|\n|\r", " ");
        /// <summary>
        /// Fetches the formatted name of the version from which the pokedex entry game
        /// to display to the user, i.e., instead of "alpha-sapphire", returns "Alpha Sapphire"
        /// </summary>
        /// <param name="client">The PokeAPI client currently in use</param>
        /// <param name="flavorText">The pokedex entry selected in the body of <see cref="GetPokedexEntryAsyncFor(string)"/></param>
        /// <returns></returns>
        private async Task <string> GetFormattedGameVersionNameInEnglishAsync(PokeApiClient client, PokemonSpeciesFlavorTexts flavorText)
        {
            // In order to get the properly formatted name from the API, we need to get the Version object from the resource
            var flavorTextVersion = await client.GetResourceAsync(flavorText.Version);

            return(flavorTextVersion.Names.Where(x => x.Language.Name == "en").SingleOrDefault().Name);
        }