Esempio n. 1
0
 public async Task <Pokemon> ToPokemon() => await Pokemon.GetInstanceAsync(EvolveTo);
Esempio n. 2
0
        /// <summary>
        /// Gets the <see cref="ApiObject{TApi}" /> represented by this <see cref="ApiResource" /> asynchronously.
        /// </summary>
        /// <typeparam name="TApi">the type of <see cref="ApiObject{TApi}" /> to return.</typeparam>
        /// <returns>The <see cref="ApiObject{TApi}" /> represented by thos <see cref="ApiResource" />.</returns>
        /// <remarks>If this is an object that derives from <see cref="ApiResource" />, no request is sent and the object itself is returned.</remarks>
        /// <exception cref="ArgumentException"><typeparamref name="TApi" /> is not the equivalent of the type represented by this <see cref="ApiResource" />.</exception>
        public async Task <TApi> GetResource <TApi>()
            where TApi : ApiObject <TApi>
        {
            if (GetType() != typeof(ApiResource) && GetType() == typeof(TApi))
            {
                return((TApi)this);
            }

            string[] split     = ResourceUri.ToString().Split(SplitChars, StringSplitOptions.RemoveEmptyEntries);
            string   type      = split[split.Length - 2].ToLowerInvariant();
            string   requested = split[split.Length - 1].ToLowerInvariant();
            int      i;
            int?     reqInt = Int32.TryParse(requested, out i) ? (int?)i : null;

            switch (type)
            {
            case "pokemon":
                if (typeof(TApi) != typeof(Pokemon))
                {
                    throw new ArgumentException(TYPE_ERR, nameof(TApi));
                }

                return((TApi)(object)(await(reqInt.HasValue
                        ? Pokemon.GetInstanceAsync(reqInt.Value)
                        : Pokemon.GetInstanceAsync(requested))));

            case "type":
                if (typeof(TApi) != typeof(PokemonType))
                {
                    throw new ArgumentException(TYPE_ERR, nameof(TApi));
                }

                return((TApi)(object)(await(reqInt.HasValue
                        ? PokemonType.GetInstanceAsync(reqInt.Value)
                        : PokemonType.GetInstanceAsync(requested))));

            case "move":
                if (typeof(TApi) != typeof(Move))
                {
                    throw new ArgumentException(TYPE_ERR, nameof(TApi));
                }

                return((TApi)(object)(await(reqInt.HasValue
                        ? Move.GetInstanceAsync(reqInt.Value)
                        : Move.GetInstanceAsync(requested))));

            case "ability":
                if (typeof(TApi) != typeof(Ability))
                {
                    throw new ArgumentException(TYPE_ERR, nameof(TApi));
                }

                return((TApi)(object)(await(reqInt.HasValue
                        ? Ability.GetInstanceAsync(reqInt.Value)
                        : Ability.GetInstanceAsync(requested))));

            case "egg":
                if (typeof(TApi) != typeof(EggGroup))
                {
                    throw new ArgumentException(TYPE_ERR, nameof(TApi));
                }

                return((TApi)(object)(await(reqInt.HasValue
                        ? EggGroup.GetInstanceAsync(reqInt.Value)
                        : EggGroup.GetInstanceAsync(requested))));

            case "description":
                if (typeof(TApi) != typeof(Description))
                {
                    throw new ArgumentException(TYPE_ERR, nameof(TApi));
                }

                return((TApi)(object)(await Description.GetInstanceAsync(reqInt.Value)));

            case "sprite":
                if (typeof(TApi) != typeof(Sprite))
                {
                    throw new ArgumentException(TYPE_ERR, nameof(TApi));
                }

                return((TApi)(object)(await(reqInt.HasValue
                        ? Sprite.GetInstanceAsync(reqInt.Value)
                        : Sprite.GetInstanceAsync(requested))));

            case "game":
                if (typeof(TApi) != typeof(Game))
                {
                    throw new ArgumentException(TYPE_ERR, nameof(TApi));
                }

                return((TApi)(object)(await(reqInt.HasValue
                        ? Game.GetInstanceAsync(reqInt.Value)
                        : Game.GetInstanceAsync(requested))));
            }

            return(null);
        }