Skip to content

Commit 2c8d00e

Browse files
committed
20-C#
1 parent 4d0eecb commit 2c8d00e

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
Console.Clear();
43+
44+
// Ejercicio extra
45+
var httpClientPokemon = new HttpClient
46+
{
47+
BaseAddress = new Uri("https://pokeapi.co/api/v2/pokemon"),
48+
};
49+
bool salir = false;
50+
do
51+
{
52+
Menu();
53+
int option = 0;
54+
int.TryParse(Console.ReadLine(), out option);
55+
switch (option)
56+
{
57+
case 1:
58+
await Search(httpClientPokemon);
59+
break;
60+
case 2:
61+
Console.Clear();
62+
Console.WriteLine("Hasta la próxima...");
63+
Thread.Sleep(1000);
64+
salir = true;
65+
break;
66+
default:
67+
Console.WriteLine("Opción no válida...");
68+
break;
69+
}
70+
71+
} while (!salir);
72+
}
73+
static void Menu()
74+
{
75+
Console.WriteLine("---Pokedex---");
76+
Console.WriteLine("1.- Buscar por id o nombre");
77+
Console.WriteLine("2.- Salir");
78+
Console.WriteLine("Ingrese opción deseada...");
79+
}
80+
static async Task Search(HttpClient httpClient)
81+
{
82+
Console.Clear();
83+
Console.WriteLine("Ingresa id de pokemón a consultar");
84+
string search = Console.ReadLine();
85+
86+
var response = await httpClient.GetAsync($"{httpClient.BaseAddress}/{search}");
87+
if (response.StatusCode != System.Net.HttpStatusCode.OK)
88+
{
89+
Console.WriteLine("Pokemon no encontrado...");
90+
return;
91+
}
92+
93+
var body = await response.Content.ReadAsStringAsync();
94+
var options = new JsonSerializerOptions
95+
{
96+
PropertyNameCaseInsensitive = true
97+
};
98+
/*
99+
* Con el metodo Deserialize podemos mapear el contenido
100+
* de la respuesta a una clase propia
101+
*/
102+
var pokemon = JsonSerializer.Deserialize<Pokemon>(body, options);
103+
104+
Console.WriteLine($"Id: {pokemon.Id}");
105+
Console.WriteLine($"Nombre: {pokemon.Name}");
106+
Console.WriteLine($"Peso: {pokemon.Weight}");
107+
Console.WriteLine($"Altura: {pokemon.Height}");
108+
Console.WriteLine("Tipos:");
109+
foreach (var type in pokemon.Types)
110+
Console.WriteLine($"{type.Slot}: {type.Type.Name}");
111+
Console.WriteLine("Juegos:");
112+
foreach (var game in pokemon.Game_Indices)
113+
Console.WriteLine($"{game.Version.Name}");
114+
Console.WriteLine();
115+
116+
}
117+
118+
}
119+
class Pokemon
120+
{
121+
public int Id { get; set; }
122+
public string Name { get; set; }
123+
public decimal Weight { get; set; }
124+
public decimal Height { get; set; }
125+
public List<TypeInfo> Types { get; set; }
126+
public List<GameInfo> Game_Indices { get; set; }
127+
}
128+
class TypeInfo
129+
{
130+
public int Slot { get; set; }
131+
public Type Type { get; set; }
132+
}
133+
class Type
134+
{
135+
public string Name { get; set; }
136+
}
137+
class GameInfo
138+
{
139+
public int Game_Index { get; set; }
140+
public Version Version { get; set; }
141+
}
142+
class Version
143+
{
144+
public string Name { get; set; }
145+
}

0 commit comments

Comments
 (0)