Skip to content

Commit 1e0edda

Browse files
authored
Merge pull request #24 from josemoracard/jose3-exercises
todos los ejercicios, .gitignore, learn.json
2 parents 1b12a8e + 0bb603d commit 1e0edda

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+599
-354
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
!/exercises
1616
!/exercises/*
17+
exercises/*/__pycache__/
1718

1819
!/.learn
1920
/.learn/**

exercises/00-hello-world/README.es.md

-13
This file was deleted.

exercises/00-hello-world/README.md

-13
This file was deleted.

exercises/00-welcome/README.es.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `00` Welcome to Python API Requests!
2+
3+
Python Requests es una herramienta potente y ampliamente utilizada para interactuar con APIs y realizar solicitudes HTTP en aplicaciones Python. Simplifica el proceso de enviar solicitudes HTTP y manejar respuestas, convirtiéndose en una herramienta favorita entre los desarrolladores para integrarse con servicios web y obtener datos de APIs.
4+
5+
Con Python Requests, puedes hacer fácilmente solicitudes GET, POST, PUT, DELETE para comunicarte con servidores web y obtener datos. Admite el manejo de autenticación, encabezados, cookies y sesiones, permitiendo una integración sin problemas con diversos servicios web.
6+
7+
Aquí aprenderás:
8+
9+
1. ¿Cómo hacer solicitudes GET?
10+
2. ¿Cómo obtener propiedades de datos e información?
11+
3. ¿Cómo configurar request headers?
12+
4. ¿Cómo configurar request content-type?
13+
5. ¿Cómo hacer solicitudes POST?
14+
15+
Haga clic en el botón `Next →` en la esquina superior derecha para continuar.

exercises/00-welcome/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `00` Welcome to Python API Requests!
2+
3+
Python Requests is a powerful and widely-used package for interacting with APIs and performing HTTP requests in Python applications. It simplifies the process of sending HTTP requests and handling responses, making it a favorite tool among developers for integrating with web services and fetching data from APIs.
4+
5+
With Python Requests, you can easily make GET, POST, PUT, DELETE, to communicate with web servers and retrieve data. It supports handling authentication, headers, cookies, and sessions, allowing for seamless integration with various web services.
6+
7+
Here you will learn:
8+
9+
1. How to do GET requests?
10+
2. How to fetch data properties and information?
11+
3. How to set request headers?
12+
4. How to set request content-type?
13+
5. How to do POST requests?
14+
15+
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-
# 01 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-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 status code")

exercises/02-random-status/test.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ 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
2323
app()
2424
captured = capsys.readouterr()
25-
assert "The URL you asked is not found\n" == captured.out
25+
assert "The URL you asked for is not found\n" == captured.out
2626

2727
@pytest.mark.it("Testing for 503: Unavailable right now")
2828
def test_url_503(capsys, app):
@@ -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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
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"
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: Ok")
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+
1. Haz 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+
+ 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+
+ Obtenga las propiedades `hours`, `minutes` y `seconds` del diccionario.
35+
+ Concatenar todo de esta manera: `Hora actual: 17 h 06 min y 23 seg`.
3636

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

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

3-
But having a text based response is not very useful, that is why API's normally respond in CSV, JSON, YAML or XML format.
3+
But having a text based response is not very useful, that is why APIs 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

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

25-
Please do a GET request to that endpoint and print the time on the console with this format:
25+
1. 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+
+ 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+
+ Get the `hours`, `minutes` and `seconds` properties from the dictionary.
35+
+ Concatenate everything together like this: `Current time: 17 hrs 06 min and 23 sec`.
3636

0 commit comments

Comments
 (0)