Skip to content

Commit 4267b2c

Browse files
authored
Merge pull request mouredev#6815 from Hequebo/hequebo20
#20 - C#
2 parents 2e8345a + 6c77710 commit 4267b2c

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using System.Text.Json;
2+
3+
class Program
4+
{
5+
static async Task Main(string[] args)
6+
{
7+
#region Http
8+
// Peticiones Http
9+
Console.WriteLine("---Peticiones HTTP---");
10+
/*
11+
* En .Net existe la clase HttpClient la cual nos
12+
* permite crear peticiones http de manera asíncrona.
13+
* En su constructor podemos proporcionar la url a la
14+
* cual queremos realizar la petición, cual usa la
15+
* clase Uri y dentro del constructor la cadena de
16+
* texto con la url
17+
*/
18+
var httpClient = new HttpClient
19+
{
20+
BaseAddress = new Uri("https://jsonplaceholder.typicode.com/users"),
21+
};
22+
Console.WriteLine("Utilizamos el metodo GET en para la siguiente url:");
23+
Console.WriteLine(httpClient.BaseAddress);
24+
/*
25+
* Utilizamos el método GetAsync(requestUri) para realizar
26+
* la petición y guardamos el resultado en una variable de
27+
* la clase HttpResponseMessage
28+
*/
29+
var response = await httpClient.GetAsync(httpClient.BaseAddress);
30+
/*
31+
* Convertimos el resultado en un json
32+
*/
33+
var jsonResponse = await response.Content.ReadAsStringAsync();
34+
Console.WriteLine("Nos devuelve la siguiente información:");
35+
Thread.Sleep(1000);
36+
Console.WriteLine(jsonResponse);
37+
httpClient.Dispose();
38+
#endregion
39+
40+
Console.ReadLine();
41+
Console.Clear();
42+
43+
// Ejercicio extra
44+
var httpClientPokemon = new HttpClient
45+
{
46+
BaseAddress = new Uri("https://pokeapi.co/api/v2/pokemon"),
47+
};
48+
bool salir = false;
49+
do
50+
{
51+
Menu();
52+
int option = 0;
53+
int.TryParse(Console.ReadLine(), out option);
54+
switch (option)
55+
{
56+
case 1:
57+
await Search(httpClientPokemon);
58+
break;
59+
case 2:
60+
Console.Clear();
61+
Console.WriteLine("Hasta la próxima...");
62+
Thread.Sleep(1000);
63+
salir = true;
64+
break;
65+
default:
66+
Console.WriteLine("Opción no válida...");
67+
break;
68+
}
69+
70+
} while (!salir);
71+
}
72+
static void Menu()
73+
{
74+
Console.WriteLine("---Pokedex---");
75+
Console.WriteLine("1.- Buscar por id o nombre");
76+
Console.WriteLine("2.- Salir");
77+
Console.WriteLine("Ingrese opción deseada...");
78+
}
79+
static async Task Search(HttpClient httpClient)
80+
{
81+
Console.Clear();
82+
Console.WriteLine("Ingresa id o nombre de pokemón a consultar");
83+
string search = Console.ReadLine();
84+
85+
var response = await httpClient.GetAsync($"{httpClient.BaseAddress}/{search}");
86+
if (response.StatusCode != System.Net.HttpStatusCode.OK)
87+
{
88+
Console.WriteLine("Pokemon no encontrado...");
89+
return;
90+
}
91+
92+
var body = await response.Content.ReadAsStringAsync();
93+
var options = new JsonSerializerOptions
94+
{
95+
PropertyNameCaseInsensitive = true
96+
};
97+
/*
98+
* Con el metodo Deserialize podemos mapear el contenido
99+
* de la respuesta a una clase propia
100+
*/
101+
var pokemon = JsonSerializer.Deserialize<Pokemon>(body, options);
102+
103+
Console.WriteLine($"Id: {pokemon.Id}");
104+
Console.WriteLine($"Nombre: {pokemon.Name}");
105+
Console.WriteLine($"Peso: {pokemon.Weight}");
106+
Console.WriteLine($"Altura: {pokemon.Height}");
107+
Console.WriteLine("Tipos:");
108+
foreach (var type in pokemon.Types)
109+
Console.WriteLine($"{type.Slot}: {type.Type.Name}");
110+
Console.WriteLine("Juegos:");
111+
foreach (var game in pokemon.Game_Indices)
112+
Console.WriteLine($"{game.Version.Name}");
113+
Console.WriteLine();
114+
115+
}
116+
117+
}
118+
class Pokemon
119+
{
120+
public int Id { get; set; }
121+
public string Name { get; set; }
122+
public decimal Weight { get; set; }
123+
public decimal Height { get; set; }
124+
public List<TypeInfo> Types { get; set; }
125+
public List<GameInfo> Game_Indices { get; set; }
126+
}
127+
class TypeInfo
128+
{
129+
public int Slot { get; set; }
130+
public Type Type { get; set; }
131+
}
132+
class Type
133+
{
134+
public string Name { get; set; }
135+
}
136+
class GameInfo
137+
{
138+
public int Game_Index { get; set; }
139+
public Version Version { get; set; }
140+
}
141+
class Version
142+
{
143+
public string Name { get; set; }
144+
}

0 commit comments

Comments
 (0)