Exemplo n.º 1
0
        public async Task <PokeModel> LoadPokemon(int PokeNumber)
        {
            //Since our ApiClient stores the base URL, 'https://pokeapi.co/api/v2/', for the API calls. All we need to do is concatinate for remainder of the API call in the correct format
            //Check for valid input, then format your Api call using StringBuilder url
            if (PokeNumber > 0)
            {
                url.Append($"pokemon/" + PokeNumber.ToString());
            }
            else
            {
                url.Append($"pokemon/1");
            }

            //This opens and awaits a request from our ApiClient, the using brackets will close all ports and resources associated with response
            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url.ToString().Trim()))
            {
                if (response.IsSuccessStatusCode)//If we get a response back, we will pass the data to our PokeModel
                {
                    PokeModel Pokemon = await response.Content.ReadAsAsync <PokeModel>();

                    url.Clear();
                    return(Pokemon);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);//Throw us an error if we did not get any data returned
                }
            }
        }
Exemplo n.º 2
0
        public async Task <PokeModel> LoadPokemon(string PokeName)
        {
            url.Append($"pokemon/" + PokeName);
            //This opens and awaits a request from our ApiClient, the using brackets will close all ports and resources associated with response
            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url.ToString().Trim()))
            {
                if (response.IsSuccessStatusCode)//If we get a response back, we will look at the data.
                {
                    PokeModel Pokemon = await response.Content.ReadAsAsync <PokeModel>();

                    url.Clear();
                    return(Pokemon);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);//Throw us an error if we did not get any data returned
                }
            }
        }