//Define your static method which will make the method become part of the class //Have it void since your are logging the result into the console. //Which would take a integer as a argument. public static async void GetOnePokemon(string 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 retrieve content from the 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 result = JObject.Parse(data)["abilities"]; Debug.WriteLine(result); PokeItem pokeItem = new PokeItem(name: $"{result[0]["ability"]["name"]}"); 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); } }
public static async Task <string> 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 { using (HttpClient client = new HttpClient()) { using (HttpResponseMessage res = await client.GetAsync(baseURL)) { using (HttpContent content = res.Content) { string data = await content.ReadAsStringAsync(); if (data != null) { //Parses the data to a object var dataObj = JObject.Parse(data); //this will create a new instance of PokeItem, and string interpolate the name property to the JSON object. ////Which will convert it to a string, since each property value is a instance of JToken. try { PokeItem pokeItems = new PokeItem(name: $"{dataObj["name"]}" // for the pokemon's name , sprite: $"{dataObj["sprites"]["front_default"]}" // for the sprite url , type: $"{dataObj["types"][0]["type"]["name"] + "/" + dataObj["types"][1]["type"]["name"]}" //for the types , pokedexid: $"{dataObj["id"]}"); Console.WriteLine("Pokemon Name: {0} \nSprite URL: {1} \nPokemon Type(s): {2}\nPokedex ID: {3}", pokeItems.Name, pokeItems.Sprite, pokeItems.pokemonType, pokeItems.pokedexID); return(pokeItems.Name); } catch (Exception ex) { PokeItem pokeItems = new PokeItem(name: $"{dataObj["name"]}" // for the pokemon's name , sprite: $"{dataObj["sprites"]["front_default"]}" // for the sprite url , type: $"{dataObj["types"][0]["type"]["name"]}" //for the types - dont know if it prints for dual type , pokedexid: $"{dataObj["id"]}"); Console.WriteLine("Pokemon Name: {0} \nSprite URL: {1} \nPokemon Type: {2}\nPokedex ID: {3}", pokeItems.Name, pokeItems.Sprite, pokeItems.pokemonType, pokeItems.pokedexID); return(pokeItems.Name); } } else { //If data is null log it into console. Console.WriteLine("Data is null!"); return(null); } } } } //Catch any exceptions and log it into the console. } catch (Exception exception) { Console.WriteLine(exception); return(null); } }