-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
72 changed files
with
595 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
!/exercises | ||
!/exercises/* | ||
exercises/*/__pycache__/ | ||
|
||
!/.learn | ||
/.learn/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# `00` Welcome to Python API Requests! | ||
|
||
Python Requests es el paquete más popular para consumir APIs y hacer solicitudes HTTP. | ||
|
||
Aquí aprenderás: | ||
|
||
1. Cómo hacer solicitudes GET. | ||
2. Cómo obtener propiedades de datos e información. | ||
3. Cómo configurar request headers. | ||
4. Cómo configurar request content-type. | ||
5. Cómo hacer solicitudes POST. | ||
|
||
Haga clic en el botón `Next →` en la esquina superior derecha para continuar. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# `00` Welcome to Python API Requests! | ||
|
||
Python Requests is the most popular package for consuming APIs and doing HTTP requests. | ||
|
||
Here you will learn: | ||
|
||
1. How to do GET requests. | ||
2. How to fetch data properties and information. | ||
3. How to set request headers. | ||
4. How to set request content-type. | ||
5. How to do POST requests. | ||
|
||
Click the `Next →` button on the top right to continue. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# `01` Creating a Request | ||
|
||
Python tiene integrado un [paquete de solicitudes (*requests package*)](https://requests.readthedocs.io/en/master/) eso permite a los desarrolladores crear solicitudes HTTP con bastante facilidad. | ||
|
||
Así es como en Python hacemos una solicitud HTTP GET: | ||
|
||
```python | ||
response = requests.get('<destination url>') | ||
print(response.status_code) | ||
``` | ||
|
||
## 📝 Instrucciones: | ||
|
||
Actualiza la variable `url` para que solicite a esta dirección: | ||
|
||
```bash | ||
https://assets.breatheco.de/apis/fake/sample/hello.php | ||
``` | ||
|
||
> Nota: La consola debe imprimir un código de estado 200. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import requests | ||
|
||
url = "https://assets.breatheco.de/apis/fake/sample/hello.php" | ||
response = requests.get(url) | ||
|
||
print("The response status is: "+str(response.status_code)) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
# `02` Manejar el Status de Respuesta | ||
# `02` Handle Response Status | ||
|
||
La siguiente solicitud GET siempre devuelve un código de status diferente, nunca se sabe qué respuesta está recibiendo del servidor. | ||
|
||
## 📝Instrucciones | ||
## 📝 Instrucciones: | ||
|
||
Crea una condición para imprimir en la consola los siguientes mensajes según el status de respuesta: | ||
|
||
| Status | Message | | ||
| Status | Mensaje | | ||
| ----- | ----- | | ||
| 404 | The URL you asked is not found | | ||
| 404 | The URL you asked for is not found | | ||
| 503 | Unavailable right now | | ||
| 200 | Everything went perfect | | ||
| 400 | Something is wrong on the request params | | ||
| 400 | Something is wrong with the request params | | ||
| Otro código de status | Unknown status code | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
# `02` Handle Response Status | ||
|
||
The following GET request is always returning a different status code, you never know what reponse you are getting form the server. | ||
The following GET request is always returning a different status code; you never know what response you are getting from the server. | ||
|
||
## 📝Instructions | ||
## 📝 Instructions: | ||
|
||
Create a condition to print on the console the following messages depending on the response status: | ||
Create a condition to print on the console the following messages, depending on the response status: | ||
|
||
| Status | Message | | ||
| ----- | ----- | | ||
| 404 | The URL you asked is not found | | ||
| 404 | The URL you asked for is not found | | ||
| 503 | Unavailable right now | | ||
| 200 | Everything went perfect | | ||
| 400 | Something is wrong on the request params | | ||
| anything else | anything | | ||
| 400 | Something is wrong with the request params | | ||
| Any other code | Unknown status code | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import requests | ||
|
||
response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php") | ||
|
||
if response.status_code == 404: | ||
print("The URL you asked for is not found") | ||
elif response.status_code == 503: | ||
print("Unavailable right now") | ||
elif response.status_code == 200: | ||
print("Everything went perfect") | ||
elif response.status_code == 400: | ||
print("Something is wrong with the request params") | ||
else: | ||
print("Unknown status code") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
# `03` Respuesta en Body | ||
# `03` Response Body | ||
|
||
Haga clic en este enlace para ver la respuesta que esta solicitud GET está dando en el body: | ||
[https://assets.breatheco.de/apis/fake/sample/random-status.php](https://assets.breatheco.de/apis/fake/sample/random-status.php) | ||
|
||
Ahora, si deseas obtener ese body como respuesta (texto), todo lo que tiene que hacer es: | ||
Ahora, si deseas obtener el *body* de la respuesta (texto/contenido), todo lo que tienes que hacer es: | ||
|
||
```py | ||
# plain text | ||
print(response.text) | ||
``` | ||
|
||
# 📝 Instrucciones | ||
## 📝 Instrucciones: | ||
|
||
Imprime en la consola el *body* de la respuesta solo para solicitudes con status `200`, para el resto imprime: | ||
|
||
Imprime en la consola la the responde body solo para solicitudes 200, para el resto imprima "Something went wrong". | ||
```text | ||
"Something went wrong" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import requests | ||
|
||
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php" | ||
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import requests | ||
|
||
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php" | ||
|
||
response = requests.get(url) | ||
|
||
if response.status_code == 200: | ||
print(response.text) | ||
else: | ||
print("Something went wrong") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import requests | ||
|
||
response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php") | ||
|
||
if response.status_code == 200: | ||
# Parsing JSON response | ||
time_data = response.json() | ||
|
||
# Extracting hours, minutes, and seconds | ||
hours = time_data["hours"] | ||
minutes = time_data["minutes"] | ||
seconds = time_data["seconds"] | ||
|
||
print(f"Current time: {hours} hrs {minutes} min and {seconds} sec") | ||
else: | ||
print("Failed to fetch current time.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.