-
Notifications
You must be signed in to change notification settings - Fork 763
/
Copy path09-apis.js
171 lines (136 loc) · 3.63 KB
/
09-apis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Clase 5 - Manejo de APIs (26/02/2025)
Vídeo: https://www.twitch.tv/videos/2391820998?t=00h17m25s
*/
// Manejo de APIs
// - APIs REST (HTTP + URLs + JSON)
// Métodos HTTP:
// - GET
// - POST
// - PUT
// - DELETE
// Códigos de respuesta HTTP:
// - 200 OK
// - 201
// - 400
// - 404
// - 500
// Consumir una API
// https://jsonplaceholder.typicode.com
// GET
fetch("https://jsonplaceholder.typicode.com/posts")
.then(response => {
// Transforma la respuesta a JSON
return response.json()
})
.then(data => {
// Procesa los datos
console.log(data)
})
.catch(error => {
// Captura errores
console.log("Error", error)
})
// Uso de Async/Await
async function getPosts() {
try {
const response = await fetch("https://jsonplaceholder.typicode.com/posts")
const data = await response.json()
console.log(data)
} catch (error) {
console.log("Error", error)
}
}
getPosts()
// Solicitud POST
async function createPost() {
try {
const newPost = {
userId: 1,
title: "Este es el título de mi post",
body: "Este es el cuerpo de mi post"
}
const response = await fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(newPost)
})
const data = await response.json()
console.log(data)
} catch (error) {
console.log("Error", error)
}
}
createPost()
// Herramientas para realizar peticiones HTTP
// - https://postman.com
// - https://apidog.com
// - https://thunderclient.com
// Manejo de errores
fetch("https://jsonplaceholder.typicode.com/mouredev")
.then(response => {
if (!response.ok) {
throw Error(`Status HTTP: ${response.status}`)
}
return response.json()
})
.catch(error => {
console.log("Error", error)
})
// Métodos HTTP adicionales
// - PATCH
// - OPTIONS
async function partialPostUpdate() {
try {
const response = await fetch("https://jsonplaceholder.typicode.com/posts/10", {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ title: "Este es el nuevo título de mi post" })
})
const data = await response.json()
console.log(data)
} catch (error) {
console.log("Error", error)
}
}
partialPostUpdate()
// Autenticación mediante API Key
async function getWeather(city) {
// https://openweathermap.org
const apiKey = "TU_API_KEY"
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`
try {
const response = await fetch(url)
const data = await response.json()
console.log(data)
} catch (error) {
console.log("Error", error)
}
}
getWeather("Madrid")
// Otros métodos de Autenticación y Autorización
// - Bearer Tokens
// - JWT
// Versionado de APIs
// - https://api.example.com/v1/resources
// - https://api.example.com/v2/resources
// Otras APIs
async function getPokemon(pokemon) {
// https://pokeapi.co
const url = `https://pokeapi.co/api/v2/pokemon/${pokemon}`
try {
const response = await fetch(url)
const data = await response.json()
console.log(`Habilidades de ${data.name}`)
data.abilities.forEach(ability => {
console.log(ability.ability.name)
})
} catch (error) {
console.log("Error", error)
}
}
getPokemon("pikachu")