|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * Utilizando un mecanismo de peticiones HTTP de tu lenguaje, realiza |
| 4 | + * una petición a la web que tú quieras, verifica que dicha petición |
| 5 | + * fue exitosa y muestra por consola el contenido de la web. |
| 6 | + * |
| 7 | + * DIFICULTAD EXTRA (opcional): |
| 8 | + * Utilizando la PokéAPI (https://pokeapi.co), crea un programa por |
| 9 | + * terminal al que le puedas solicitar información de un Pokémon concreto |
| 10 | + * utilizando su nombre o número. |
| 11 | + * - Muestra el nombre, id, peso, altura y tipo(s) del Pokémon |
| 12 | + * - Muestra el nombre de su cadena de evoluciones |
| 13 | + * - Muestra los juegos en los que aparece |
| 14 | + * - Controla posibles errores |
| 15 | + */ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + |
| 24 | + "net/http" |
| 25 | +) |
| 26 | + |
| 27 | +func exercice(url string) { |
| 28 | + resp, err := http.Get(url) |
| 29 | + |
| 30 | + if err != nil { |
| 31 | + |
| 32 | + fmt.Printf("Error readinng web site %v\n", err) |
| 33 | + return |
| 34 | + } |
| 35 | + defer resp.Body.Close() |
| 36 | + data, err := io.ReadAll(resp.Body) |
| 37 | + |
| 38 | + if err != nil { |
| 39 | + fmt.Printf("Error readinng data %v", err) |
| 40 | + } |
| 41 | + if resp.StatusCode == 200 { |
| 42 | + fmt.Println("Conexion Exitosa. Code :", resp.StatusCode) |
| 43 | + } |
| 44 | + fmt.Println(string(data)) |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +func getPokeUrl(url string) *http.Response { |
| 49 | + response, e := http.Get(url) |
| 50 | + |
| 51 | + if e != nil { |
| 52 | + |
| 53 | + fmt.Printf("Error de Red %v \n", e) |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + if response.StatusCode == http.StatusNotFound { |
| 58 | + fmt.Println("Pokemmon no encontrado ") |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + return response |
| 63 | +} |
| 64 | + |
| 65 | +func extra() { |
| 66 | + |
| 67 | + var PokemonName string |
| 68 | + fmt.Println("Nombre o NUmero del Pokemon ") |
| 69 | + fmt.Scanln(&PokemonName) |
| 70 | + url := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon/%v", PokemonName) |
| 71 | + fmt.Println(url) |
| 72 | + |
| 73 | + response := getPokeUrl(url) |
| 74 | + if response == nil { |
| 75 | + return |
| 76 | + } |
| 77 | + // response, e := http.Get(url) |
| 78 | + |
| 79 | + // if e != nil { |
| 80 | + |
| 81 | + // fmt.Printf("Error de Red %v \n", e) |
| 82 | + // return |
| 83 | + // } |
| 84 | + |
| 85 | + // if response.StatusCode == http.StatusNotFound { |
| 86 | + // fmt.Println("Pokemmon no encontrado ") |
| 87 | + // return |
| 88 | + // } |
| 89 | + |
| 90 | + defer response.Body.Close() |
| 91 | + |
| 92 | + data, e := io.ReadAll(response.Body) |
| 93 | + if e != nil { |
| 94 | + fmt.Printf("Error reading Data %v \n", e) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + var poke Pokemon |
| 99 | + e = json.Unmarshal(data, &poke) |
| 100 | + if e != nil { |
| 101 | + fmt.Printf("Error json data %v\n", e) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + fmt.Printf("Name : %v\t", poke.Name) |
| 106 | + fmt.Printf("ID : %v\t", poke.Id) |
| 107 | + fmt.Printf("Weight : %v\t", poke.Weight) |
| 108 | + fmt.Printf("Height : %v\t\n", poke.Height) |
| 109 | + |
| 110 | + fmt.Println("tIpOs") |
| 111 | + |
| 112 | + for _, v := range poke.Types { |
| 113 | + fmt.Printf(" \t Name %v URL %v \n", v.Type.Name, v.Type.Url) |
| 114 | + |
| 115 | + } |
| 116 | + fmt.Println("Games") |
| 117 | + for _, v := range poke.GameIndices { |
| 118 | + fmt.Printf(" \t Index %v Name %v URL %v \n", v.GameIndex, v.Version.Name, v.Version.Url) |
| 119 | + } |
| 120 | + |
| 121 | + specie_url := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon-species//%v", PokemonName) |
| 122 | + |
| 123 | + Respspecie := getPokeUrl(specie_url) |
| 124 | + |
| 125 | + if Respspecie == nil { |
| 126 | + |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + defer Respspecie.Body.Close() |
| 131 | + |
| 132 | + data, e = io.ReadAll(Respspecie.Body) |
| 133 | + |
| 134 | + if e != nil { |
| 135 | + fmt.Printf("Error reading Data %v \n", e) |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + var evolutionChain EvolutionChainUrl |
| 140 | + e = json.Unmarshal(data, &evolutionChain) |
| 141 | + if e != nil { |
| 142 | + fmt.Printf("Error json data %v\n", e) |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + fmt.Println("URL", evolutionChain.Evolution_chain.Url) |
| 147 | + |
| 148 | + evolution_chain := getPokeUrl(evolutionChain.Evolution_chain.Url) |
| 149 | + if evolution_chain == nil { |
| 150 | + |
| 151 | + return |
| 152 | + } |
| 153 | + defer evolution_chain.Body.Close() |
| 154 | + |
| 155 | + var evolution_to eEvo |
| 156 | + respEvolutio_to, _ := io.ReadAll(evolution_chain.Body) |
| 157 | + _ = json.Unmarshal(respEvolutio_to, &evolution_to) |
| 158 | + |
| 159 | + fmt.Println("EStado Original : ", evolution_to.Chain.Species.Name) |
| 160 | + fmt.Println("Cadena de Evolution: ") |
| 161 | + printEvo(evolution_to.Chain) |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | +func printEvo(dd Ggg) { |
| 166 | + |
| 167 | + for _, evo := range dd.Evolve_to { |
| 168 | + printEvo(evo) |
| 169 | + fmt.Println(evo.Species.Name) |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | +} |
| 174 | + |
| 175 | +func GetPokemon(url string) *http.Response { |
| 176 | + response, e := http.Get(url) |
| 177 | + |
| 178 | + if e != nil { |
| 179 | + |
| 180 | + fmt.Printf("Error de Red %v \n", e) |
| 181 | + return nil |
| 182 | + } |
| 183 | + |
| 184 | + if response.StatusCode == http.StatusNotFound { |
| 185 | + fmt.Println("Pokemmon no encontrado ") |
| 186 | + return nil |
| 187 | + } |
| 188 | + |
| 189 | + defer response.Body.Close() |
| 190 | + return response |
| 191 | + |
| 192 | +} |
| 193 | + |
| 194 | +type eEvo struct { |
| 195 | + Chain Ggg `json:"chain"` |
| 196 | +} |
| 197 | + |
| 198 | +type Ggg struct { |
| 199 | + Evolve_to []Ggg `json:"evolves_to"` |
| 200 | + |
| 201 | + Species struct { |
| 202 | + Name string `json:"name"` |
| 203 | + } `json:"species"` |
| 204 | +} |
| 205 | + |
| 206 | +type EvolutionChainUrl struct { |
| 207 | + Evolution_chain struct { |
| 208 | + Url string `json:"url"` |
| 209 | + } `json:"evolution_chain"` |
| 210 | +} |
| 211 | + |
| 212 | +type Pokemon struct { |
| 213 | + Id int `json:"id"` |
| 214 | + Name string `json:"name"` |
| 215 | + Weight int `json:"weight"` |
| 216 | + Height int `json:"height"` |
| 217 | + |
| 218 | + GameIndices []struct { |
| 219 | + GameIndex int `json:"game_index"` |
| 220 | + Version struct { |
| 221 | + Name string `json:"name"` |
| 222 | + Url string `json:"url"` |
| 223 | + } `json:"version"` |
| 224 | + } `json:"game_indices"` |
| 225 | + |
| 226 | + Types []struct { |
| 227 | + Slot int `json:"slot"` |
| 228 | + Type struct { |
| 229 | + Name string `json:"name"` |
| 230 | + Url string `json:"url"` |
| 231 | + } `json:"type"` |
| 232 | + } `json:"types"` |
| 233 | +} |
| 234 | + |
| 235 | +func main() { |
| 236 | + // exercice("https://mouredev.pro") |
| 237 | + extra() |
| 238 | +} |
0 commit comments