Skip to content

Commit 6b4373d

Browse files
committed
Pending changes exported from your codespace
1 parent 8b6eb7b commit 6b4373d

File tree

22 files changed

+145
-81
lines changed

22 files changed

+145
-81
lines changed

.learn/resets/03-response-body/app.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import requests
2+
3+
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
4+
5+
response = requests.get(url)
6+
7+
if response.status_code == 200:
8+
print(response.text)
9+
else:
10+
print("Something went wrong")
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Python API Requests
1+
# `00` Welcome to Python API Requests!
22

33
Python Requests es el paquete más popular para consumir API y hacer solicitudes HTTP.
44

55
Aquí aprenderás:
66

77
1. Cómo hacer solicitudes GET.
8-
2. Cómo obtener propiedades de una data e información.
8+
2. Cómo obtener propiedades de datos e información.
99
3. Cómo configurar request headers.
1010
4. Cómo configurar request content-type.
1111
5. Cómo hacer solicitudes POST.
1212

13-
Haga click en el botón `next` en la esquina superior derecha para continuar.
13+
Haga clic en el botón `Next` en la esquina superior derecha para continuar.

exercises/00-welcome/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `00` Welcome to Python API Requests!
2+
3+
Python Requests is the most popular package for consuming APIs and doing HTTP requests.
4+
5+
Here you will learn:
6+
7+
1. How to do GET requests.
8+
2. How to fetch data properties and information.
9+
3. How to set request headers.
10+
4. How to set request content-type.
11+
5. How to do POST requests.
12+
13+
Click the `Next →` button on the top right to continue.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# `01` Creating a Request
2+
3+
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.
4+
5+
Así es como en Python hacemos una solicitud HTTP GET:
6+
7+
```python
8+
response = requests.get('<destination url>')
9+
print(response.status_code)
10+
```
11+
12+
## 📝 Instrucciones:
13+
14+
Actualiza la variable `url` para que solicite a esta dirección:
15+
16+
```bash
17+
https://assets.breatheco.de/apis/fake/sample/hello.php
18+
```
19+
20+
> Nota: La consola debe imprimir un código de estado 200.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 02 Creating a request
1+
# `01` Creating a Request
22

33
Python has a [requests package](https://requests.readthedocs.io/en/master/) that allows developers to create HTTP request pretty easily.
44

@@ -9,12 +9,12 @@ response = requests.get('<destination url>')
99
print(response.status_code)
1010
```
1111

12-
# 📝 Instructions
12+
## 📝 Instructions:
1313

14-
Change the variable url to make it request to:
14+
Update the `url` variable to make it request to this address:
1515

16-
```bash
16+
```text
1717
https://assets.breatheco.de/apis/fake/sample/hello.php
1818
```
1919

20-
Note: The console should print a 200 status code.
20+
> Note: The console should print a 200 status code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import requests
2+
3+
url = "https://assets.breatheco.de/apis/fake/sample/hello.php"
4+
response = requests.get(url)
5+
6+
print("The response status is: "+str(response.status_code))

exercises/01-hello-world/README.md

-13
This file was deleted.

exercises/01-what-is-a-request/README.es.md

-20
This file was deleted.
+6-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# `02` Manejar el Status de Respuesta
1+
# `02` Handle Response Status
22

33
La siguiente solicitud GET siempre devuelve un código de status diferente, nunca se sabe qué respuesta está recibiendo del servidor.
44

5-
## 📝Instrucciones
5+
## 📝 Instrucciones:
66

77
Crea una condición para imprimir en la consola los siguientes mensajes según el status de respuesta:
88

9-
| Status | Message |
9+
| Status | Mensaje |
1010
| ----- | ----- |
11-
| 404 | The URL you asked is not found |
11+
| 404 | The URL you asked for is not found |
1212
| 503 | Unavailable right now |
1313
| 200 | Everything went perfect |
14-
| 400 | Something is wrong on the request params |
14+
| 400 | Something is wrong with the request params |
15+
| Otro código de status | Unknown status code |

exercises/02-random-status/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# `02` Handle Response Status
22

3-
The following GET request is always returning a different status code, you never know what reponse you are getting form the server.
3+
The following GET request is always returning a different status code; you never know what response you are getting from the server.
44

5-
## 📝Instructions
5+
## 📝 Instructions:
66

7-
Create a condition to print on the console the following messages depending on the response status:
7+
Create a condition to print on the console the following messages, depending on the response status:
88

99
| Status | Message |
1010
| ----- | ----- |
11-
| 404 | The URL you asked is not found |
11+
| 404 | The URL you asked for is not found |
1212
| 503 | Unavailable right now |
1313
| 200 | Everything went perfect |
14-
| 400 | Something is wrong on the request params |
15-
| anything else | anything |
14+
| 400 | Something is wrong with the request params |
15+
| Any other code | Unknown status code |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import requests
2+
3+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php")
4+
5+
if response.status_code == 404:
6+
print("The URL you asked for is not found")
7+
elif response.status_code == 503:
8+
print("Unavailable right now")
9+
elif response.status_code == 200:
10+
print("Everything went perfect")
11+
elif response.status_code == 400:
12+
print("Something is wrong with the request params")
13+
else:
14+
print("Unknown code")

exercises/02-random-status/test.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_url_200(capsys, app):
1616
captured = capsys.readouterr()
1717
assert "Everything went perfect\n" == captured.out
1818

19-
@pytest.mark.it("When testing for 404 it should print The URL you asked is not found'")
19+
@pytest.mark.it("Testing for 404: The URL you asked for is not found'")
2020
def test_url_404(capsys, app):
2121
with patch('requests.get') as mock_request:
2222
mock_request.return_value.status_code = 404
@@ -32,10 +32,18 @@ def test_url_503(capsys, app):
3232
captured = capsys.readouterr()
3333
assert "Unavailable right now\n" == captured.out
3434

35-
@pytest.mark.it("Testing for 400: Something is wrong on the request params")
35+
@pytest.mark.it("Testing for 400: Something is wrong with the request params")
3636
def test_url_400(capsys, app):
3737
with patch('requests.get') as mock_request:
3838
mock_request.return_value.status_code = 400
3939
app()
4040
captured = capsys.readouterr()
41-
assert "Something is wrong on the request params\n" == captured.out
41+
assert "Something is wrong with the request params\n" == captured.out
42+
43+
@pytest.mark.it("Testing for any other code: Unknown status code")
44+
def test_url_generic_response(capsys, app):
45+
with patch('requests.get') as mock_request:
46+
mock_request.return_value.status_code = 500 # For example, using status code 500 for generic response
47+
app()
48+
captured = capsys.readouterr()
49+
assert "Unknown status code\n" == captured.out
+9-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
# `03` Respuesta en Body
1+
# `03` Response Body
22

33
Haga clic en este enlace para ver la respuesta que esta solicitud GET está dando en el body:
44
[https://assets.breatheco.de/apis/fake/sample/random-status.php](https://assets.breatheco.de/apis/fake/sample/random-status.php)
55

6-
Ahora, si deseas obtener ese body como respuesta (texto), todo lo que tiene que hacer es:
6+
Ahora, si deseas obtener el *body* de la respuesta (texto/contenido), todo lo que tienes que hacer es:
7+
78
```py
8-
# plain text
99
print(response.text)
1010
```
1111

12-
# 📝 Instrucciones
12+
## 📝 Instrucciones:
13+
14+
Imprime en la consola el *body* de la respuesta solo para solicitudes con status `200`, para el resto imprime:
1315

14-
Imprime en la consola la the responde body solo para solicitudes 200, para el resto imprima "Something went wrong".
16+
```text
17+
"Something went wrong"
18+
```

exercises/03-response-body/README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
Click on this link to see the response body that this GET request is giving:
44
[https://assets.breatheco.de/apis/fake/sample/random-status.php](https://assets.breatheco.de/apis/fake/sample/random-status.php)
55

6-
Now, if you want to get that response body (text) all you have to do is:
6+
Now, if you want to get that response body (text/content) all you have to do is this:
7+
78
```py
8-
# plain text
99
print(response.text)
1010
```
1111

12-
# 📝 Instructions
12+
## 📝 Instructions:
13+
14+
Print on the console the response body for status code `200`, for all the other print:
1315

14-
Print on the console the response body only for 200 requests, for all the other print "Something went wrong".
16+
```text
17+
"Something went wrong"
18+
```

exercises/03-response-body/app.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import requests
22

3-
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
3+
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
4+
5+
response = requests.get(url)
6+
7+
if response.status_code == 200:
8+
print(response.text)
9+
else:
10+
print("Something went wrong")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import requests
2+
3+
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
4+
5+
response = requests.get(url)
6+
7+
if response.status_code == 200:
8+
print(response.text)
9+
else:
10+
print("Something went wrong")

exercises/03-response-body/test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def test_url_call(capsys, app):
77
app()
88
mock_request.assert_called_once_with("https://assets.breatheco.de/apis/fake/sample/random-status.php")
99

10-
@pytest.mark.it("Testing for 200: Everythign went perfect")
10+
@pytest.mark.it("Testing for 200: Everything went perfect")
1111
def test_url_200(capsys, app):
1212
with patch('requests.get') as mock_request:
1313
mock_request.return_value.status_code = 200
@@ -16,7 +16,7 @@ def test_url_200(capsys, app):
1616
captured = capsys.readouterr()
1717
assert "something\n" == captured.out
1818

19-
@pytest.mark.it("When testing for 404 it should print 'Something went wrong'")
19+
@pytest.mark.it("Testing for any other code: Something went wrong")
2020
def test_url_404(capsys, app):
2121
with patch('requests.get') as mock_request:
2222
mock_request.return_value.status_code = 404

exercises/04-response-body-json/README.es.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# `04` Respuesta JSON
1+
# `04` Response JSON
22

33
Pero tener una respuesta basada en texto no es muy útil, es por eso que las API normalmente responden en formato CSV, JSON, YAML o XML.
44

5-
# 📝 Instrucciones
5+
## 📝 Instrucciones:
66

77
El siguiente endpoint devuelve la hora actual en un formato JSON cada vez que se solicita utilizando el método GET.
88

@@ -22,15 +22,15 @@ Response body:
2222
}
2323
```
2424

25-
Haga una solicitud GET a ese endpoint e imprima la hora en la consola con este formato:
25+
Haga una solicitud GET a ese endpoint e imprime la hora en la consola con este formato:
2626

2727
```bash
2828
Current time: 17 hrs 06 min and 23 sec
2929
```
3030

31-
## 💡Pista
31+
## 💡 Pistas:
3232

33-
1. Usa el [metodo .json()](https://www.w3schools.com/python/ref_requests_response.asp) para obtener el response body como un diccionario y almacenarlo en una variable
34-
2. Obtenga las propiedades `horas`,` minutos` y `segundos` del diccionario
35-
3. Concatenar todo de esta manera: `Hora actual: 17 h 06 min y 23 seg`
33+
1. Usa el [metodo .json()](https://www.w3schools.com/python/ref_requests_response.asp) para obtener el response body como un diccionario y almacenarlo en una variable.
34+
2. Obtenga las propiedades `hours`, `minutes` y `seconds` del diccionario.
35+
3. Concatenar todo de esta manera: `Hora actual: 17 h 06 min y 23 seg`.
3636

exercises/04-response-body-json/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
But having a text based response is not very useful, that is why API's normally respond in CSV, JSON, YAML or XML format.
44

5-
# 📝 Instructions
5+
## 📝 Instructions:
66

77
The following endpoint returns the current time in a JSON format every time it is requested using the GET method.
88

@@ -24,13 +24,13 @@ Response body:
2424

2525
Please do a GET request to that endpoint and print the time on the console with this format:
2626

27-
```bash
27+
```text
2828
Current time: 17 hrs 06 min and 23 sec
2929
```
3030

31-
## 💡Hint
31+
## 💡 Hints:
3232

33-
1. Use the [.json() method](https://www.w3schools.com/python/ref_requests_response.asp) to get the response body as a dictionary and store it in a variable
34-
2. Get the `hours`, `minutes` and `seconds` properties from the dictionary
35-
3. Concatenate everything together like this: `Current time: 17 hrs 06 min and 23 sec`
33+
1. Use the [.json() method](https://www.w3schools.com/python/ref_requests_response.asp) to get the response body as a dictionary and store it in a variable.
34+
2. Get the `hours`, `minutes` and `seconds` properties from the dictionary.
35+
3. Concatenate everything together like this: `Current time: 17 hrs 06 min and 23 sec`.
3636

learn.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"slug": "python-http-requests-api-tutorial-exercises",
88
"description": {
9-
"us": "Learn interactively to consume and create HTTP requests to API's using Python",
9+
"us": "Learn interactively to consume and create HTTP requests to APIs using Python",
1010
"es": "Aprende interactivamente cómo consumir APIs y hacer requests HTTP con Python"
1111
},
1212
"repository": "https://github.com/4GeeksAcademy/python-http-requests-api-tutorial-exercises",

0 commit comments

Comments
 (0)