Example #1
0
        public static async void GetPokemon(string pokeName)
        {
            try
            {
                var content = (string) await GetData(pokeName);

                PokeItem pokeItem = JsonConvert.DeserializeObject <PokeItem>(content);

                content = (string) await GetData($"{pokeItem.Id}/encounters");

                JObject[] locationEncounters = JsonConvert.DeserializeObject <JObject[]>(content);
                string    canEncounter;
                string[]  locationNames = new string[locationEncounters.Length];

                if (locationEncounters.Length > 0)
                {
                    canEncounter = "This Pokémon could be encountered in the following areas...";
                    pokeItem.LocationEncounters = locationEncounters;

                    for (var i = 0; i < pokeItem.LocationEncounters.Length; i++)
                    {
                        locationNames[i] = (string)pokeItem.LocationEncounters[i]["location_area"]["name"];
                        locationNames[i] = locationNames[i].Replace("-", " ");
                    }
                }
                else
                {
                    canEncounter = "There's no known area where this Pokémon could be found...";
                }

                string totalLocations = "";

                foreach (var location in locationNames)
                {
                    totalLocations += $@"- {location}
";
                }

                string totalTypes = $"{pokeItem.Types[0]["type"]["name"]}";
                if (pokeItem.Types.Length > 1)
                {
                    totalTypes += $" & {pokeItem.Types[1]["type"]["name"]}";
                }

                System.Console.WriteLine($@"Id: {pokeItem.Id}
Height: {pokeItem.Height}
Weight: {pokeItem.Weight}
Types: {totalTypes}
Encounters: {canEncounter}
{totalLocations}");
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                System.Console.WriteLine("HELP: Maybe you misspelled the name...?");
            }
        }
Example #2
0
        //Define your static method which will make the method become part of the class
        //Also make it asynchronous meaning it is retrieving data from a api.
        //Have it void since your are logging the result into the console.
        //Which would take a integar as a argument.
        public static async void GetOnePokemon(int pokeId)
        {
            //Define your base url
            string baseURL = $"http://pokeapi.co/api/v2/pokemon/{pokeId}/";

            //Have your api call in try/catch block.
            try {
                //Now we will have our using directives which would have a HttpClient
                using (HttpClient client = new HttpClient())
                {
                    //Now get your response from the client from get request to baseurl.
                    //Use the await keyword since the get request is asynchronous, and want it run before next asychronous operation.
                    using (HttpResponseMessage res = await client.GetAsync(baseURL))
                    {
                        //Now we will retrieve content from our response, which would be HttpContent, retrieve from the response Content property.
                        using (HttpContent content = res.Content)
                        {
                            //Retrieve the data from the content of the response, have the await keyword since it is asynchronous.
                            string data = await content.ReadAsStringAsync();

                            //If the data is not null, parse the data to a C# object, then create a new instance of PokeItem.
                            if (data != null)
                            {
                                //Parse your data into a object.
                                var dataObj = JObject.Parse(data);
                                //Then create a new instance of PokeItem, and string interpolate your name property to your JSON object.
                                //Which will convert it to a string, since each property value is a instance of JToken.
                                PokeItem pokeItem = new PokeItem(name: $"{dataObj["name"]}");
                                //Log your pokeItem's name to the Console.
                                Console.WriteLine("Pokemon Name: {0}", pokeItem.Name);
                            }
                            else
                            {
                                //If data is null log it into console.
                                Console.WriteLine("Data is null!");
                            }
                        }
                    }
                }
                //Catch any exceptions and log it into the console.
            } catch (Exception exception) {
                Console.WriteLine(exception);
            }
        }
Example #3
0
        //Define your static method which will make the method become part of the class
        //Also make it asynchronous meaning it is retrieving data from a api.
        //Have it void since your are logging the result into the console.
        //Which would take a integar as a argument.
        public async void GetAllPokemon(int total)
        {
            total = total + 1;
            int progress = 0;

            type2    = null;
            ability2 = null;

            for (int pokeId = 1; pokeId < total; pokeId++)
            {
                //Define your base url
                string baseURL = $"http://pokeapi.co/api/v2/pokemon/{pokeId}/";

                //Have your api call in try/catch block.
                try
                {
                    //Now we will have our using directives which would have a HttpClient
                    using (HttpClient client = new HttpClient())
                    {
                        //Now get your response from the client from get request to baseurl.
                        //Use the await keyword since the get request is asynchronous, and want it run before next asychronous operation.
                        using (HttpResponseMessage res = await client.GetAsync(baseURL))
                        {
                            //Now we will retrieve content from our response, which would be HttpContent, retrieve from the response Content property.
                            using (HttpContent content = res.Content)
                            {
                                //Retrieve the data from the content of the response, have the await keyword since it is asynchronous.
                                string data = await content.ReadAsStringAsync();

                                //If the data is not null, parse the data to a C# object, then create a new instance of PokeItem.
                                if (data != null)
                                {
                                    // MessageBox.Show("NO Data");
                                    //Parse your data into a object.
                                    var dataObj = JObject.Parse(data);
                                    //Then create a new instance of PokeItem, and string interpolate your name property to your JSON object
                                    //Which will convert it to a string, since each property value is a instance of JToken.
                                    PokeItem pokeItem = new PokeItem(name: $"{dataObj["name"]}", type1: $"{dataObj["types"][0]["type"]["name"]}", id: $"{dataObj["id"]}", abilities: $"{dataObj["abilities"][0]["ability"]["name"]}", baseex: $"{dataObj["base_experience"]}", weight: $"{dataObj["weight"]}", height: $"{dataObj["height"]}");

                                    Task.Run(async() => { await CheckType2(pokeId); }).Wait();
                                    Task.Run(async() => { await CheckAbility2(pokeId); }).Wait();

                                    pokeItem.Abilities = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(pokeItem.Abilities);
                                    abilities          = pokeItem.Abilities;
                                    if (type2 != null)
                                    {
                                        pokeItem.Type1 = pokeItem.Type1 + " & " + type2;
                                    }
                                    if (ability2 != null)
                                    {
                                        abilities = pokeItem.Abilities + " & " + ability2;
                                    }
                                    progress           = (pokeId * 100) / total;
                                    progressBar1.Value = progress;
                                    abilist.Add(" ID: " + pokeItem.Id + "\n" + " Base Experience: " + pokeItem.BaseEx + "\n " + "Weight: " + pokeItem.Weight + " \n" + " Height: " + pokeItem.Height);
                                    //  setImageSource(pokeId);
                                    populateDatagrid(pokeItem.Name, pokeItem.Type1, pokeItem.Id, abilities);
                                }
                                else
                                {
                                    //If data is null log it into console.
                                    MessageBox.Show("Data is null!");
                                }
                            }
                        }
                    }
                }

                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                }
            }
            progressBar1.Visibility   = Visibility.Hidden;
            progressborder.Visibility = Visibility.Hidden;
            FileNameLabel.Visibility  = Visibility.Hidden;
            pika.Visibility           = Visibility.Hidden;

            buttonColumn.Header     = "Misc Info";
            buttonColumn.Width      = 80;
            buttonColumn.Visibility = Visibility.Visible;

            DataGrid2.Columns.Add(buttonColumn);

            DataGrid2.Visibility = Visibility.Visible;
            DataGrid2.Columns[0].SetIsFilterVisible(false);
        }