Skip to content

Commit 8e299a4

Browse files
committed
exercises, .gitignore, solutions, tests
1 parent 6b4373d commit 8e299a4

Some content is hidden

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

62 files changed

+468
-291
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/**

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

-10
This file was deleted.

exercises/00-welcome/README.es.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `00` Welcome to Python API Requests!
22

3-
Python Requests es el paquete más popular para consumir API y hacer solicitudes HTTP.
3+
Python Requests es el paquete más popular para consumir APIs y hacer solicitudes HTTP.
44

55
Aquí aprenderás:
66

exercises/02-random-status/solution.hide.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
elif response.status_code == 400:
1212
print("Something is wrong with the request params")
1313
else:
14-
print("Unknown code")
14+
print("Unknown status code")

exercises/02-random-status/test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_url_404(capsys, app):
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):

exercises/03-response-body/app.py

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

33
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

+1-1
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: Everything 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

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Response body:
2222
}
2323
```
2424

25-
Haga una solicitud GET a ese endpoint e imprime 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

3131
## 💡 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 `hours`, `minutes` y `seconds` 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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

55
## 📝 Instructions:
66

@@ -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

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

3131
## 💡 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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import requests
2+
3+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php")
4+
5+
if response.status_code == 200:
6+
# Parsing JSON response
7+
time_data = response.json()
8+
9+
# Extracting hours, minutes, and seconds
10+
hours = time_data["hours"]
11+
minutes = time_data["minutes"]
12+
seconds = time_data["seconds"]
13+
14+
print(f"Current time: {hours} hrs {minutes} min and {seconds} sec")
15+
else:
16+
print("Failed to fetch current time.")

exercises/04-response-body-json/test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_url_call(capsys, app):
1414
app()
1515
mock_request.assert_called_once_with("https://assets.breatheco.de/apis/fake/sample/time.php")
1616

17-
@pytest.mark.it("You should print on the console a stirng like: Current time: 19 hrs 45 min and 06 sec")
17+
@pytest.mark.it("You should print on the console a string like: Current time: 19 hrs 45 min and 06 sec")
1818
def test_url_output(capsys, app):
1919
with patch('requests.get') as mock_request:
2020
mock_request.return_value = FakeResponse()
+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# `05` Nombre de Proyecto
1+
# `05` Project name
22

33
El siguiente endpoint es ideal para recuperar proyectos de estudiantes:
44

@@ -8,7 +8,7 @@ GET [https://assets.breatheco.de/apis/fake/sample/project1.php](https://assets.b
88
{
99
"name": "Coursera eLearning",
1010
"thumb": "https://unsplash.it/450/320?image=178",
11-
"description": "The coolest elarning site on the planet",
11+
"description": "The coolest eLearning site on the planet",
1212
"images": [
1313
"https://unsplash.it/450/320?image=178",
1414
"https://unsplash.it/450/320?image=179",
@@ -18,19 +18,19 @@ GET [https://assets.breatheco.de/apis/fake/sample/project1.php](https://assets.b
1818
}
1919
```
2020

21-
# 📝 Instrucciones
21+
## 📝 Instrucciones:
2222

23-
Llama al endpoint e imprime el nombre del proyecto en el terminal (solo el nombre del proyecto)
23+
1. Llama al endpoint e imprime el nombre del proyecto en el terminal (solo el nombre del proyecto).
2424

25-
Example output:
26-
```bash
25+
Ejemplo de salida:
26+
27+
```text
2728
Coursera eLearning
2829
```
2930

30-
## 💡Pista
31+
## 💡 Pistas:
3132

32-
1. Ejercicio similar al anterior.
33-
2. Haz una solicitud GET al endpoint.
34-
3. Usa el [metodo .json()](https://www.w3schools.com/python/ref_requests_response.asp) para obtener el response body como un diccionario (igual que lo hizo en el último ejercicio).
35-
4. Imprime el nombre del proyecto, puedes acceder al nombre de la propiedad en el diccionario de respuestas.
33+
+ Haz una solicitud GET al endpoint.
34+
+ Usa el [metodo .json()](https://www.w3schools.com/python/ref_requests_response.asp) para obtener el response body como un diccionario (igual que en el último ejercicio).
35+
+ Imprime el nombre del proyecto, puedes acceder al nombre de la propiedad en el diccionario de respuestas.
3636

exercises/05-project-name/README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ GET [https://assets.breatheco.de/apis/fake/sample/project1.php](https://assets.b
88
{
99
"name": "Coursera eLearning",
1010
"thumb": "https://unsplash.it/450/320?image=178",
11-
"description": "The coolest elarning site on the planet",
11+
"description": "The coolest eLearning site on the planet",
1212
"images": [
1313
"https://unsplash.it/450/320?image=178",
1414
"https://unsplash.it/450/320?image=179",
@@ -18,19 +18,19 @@ GET [https://assets.breatheco.de/apis/fake/sample/project1.php](https://assets.b
1818
}
1919
```
2020

21-
# 📝 Instructions
21+
## 📝 Instructions:
2222

23-
Please call the endpoint and print the project name on the terminal (only the project name)
23+
1. Please call the endpoint and print the project name on the terminal (only the project name).
2424

2525
Example output:
26-
```bash
26+
27+
```text
2728
Coursera eLearning
2829
```
2930

30-
## 💡Hint
31+
## 💡 Hints:
3132

32-
1. Similar exercise to the previous one.
33-
2. Do a GET request to the endpoint.
34-
3. Use the [.json() method](https://www.w3schools.com/python/ref_requests_response.asp) to get the response body as a dictionary (same as you did on last exercise).
35-
4. Print the project name, you can access the property name on the response dictionary.
33+
+ Make a GET request to the endpoint.
34+
+ Use the [.json() method](https://www.w3schools.com/python/ref_requests_response.asp) to get the response body as a dictionary (same as you did on the last exercise).
35+
+ Print the project name; you can access the property name in the response dictionary.
3636

exercises/05-project-name/app.py

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

3-
# your code here
3+
# Your code here
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import requests
2+
3+
# Your code here
4+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/project1.php")
5+
6+
if response.status_code == 200:
7+
# Parsing JSON response
8+
project_data = response.json()
9+
10+
# Extracting project name
11+
project_name = project_data["name"]
12+
13+
print(project_name)
14+
else:
15+
print("Failed to fetch project data.")

exercises/05-project-name/test.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def json(self):
88
return {
99
"name": "Sample Project",
1010
"thumb": "https://unsplash.it/450/320?image=178",
11-
"description": "The coolest elarning site on the planet",
11+
"description": "The coolest eLearning site on the planet",
1212
"images": [
1313
"https://unsplash.it/450/320?image=178",
1414
"https://unsplash.it/450/320?image=179",
@@ -17,13 +17,14 @@ def json(self):
1717
]
1818
}
1919

20-
@pytest.mark.it("requests.get has to be called to the project url")
20+
@pytest.mark.it("requests.get has to be called for the project.php url")
2121
def test_url_call(capsys, app):
2222
with patch('requests.get') as mock_request:
2323
app()
2424
mock_request.assert_called_once_with("https://assets.breatheco.de/apis/fake/sample/project1.php")
2525

26-
@pytest.mark.it("You should print on the console a stirng like: Current time: 19 hrs 45 min and 06 sec")
26+
27+
@pytest.mark.it("You should print the name of the project on the console")
2728
def test_url_output(capsys, app):
2829
with patch('requests.get') as mock_request:
2930
mock_request.return_value = FakeResponse()
+17-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# `06` Lista de Proyectos
1+
# `06` Project List
22

3-
El siguiente endpoint devuelve una respuesta con formato JSON con varios proyectos en una lista:
3+
El siguiente endpoint devuelve una respuesta en formato JSON con varios proyectos en una lista:
44

55
GET: [https://assets.breatheco.de/apis/fake/sample/project_list.php](https://assets.breatheco.de/apis/fake/sample/project_list.php)
66

77
Cada uno de los proyectos tiene el siguiente formato (ejemplo):
8+
89
```json
910
{
1011
"name": "Coursera eLearning",
1112
"thumb": "https://unsplash.it/450/320?image=178",
12-
"description": "The coolest elarning site on the planet",
13+
"description": "The coolest eLearning site on the planet",
1314
"images": [
1415
"https://unsplash.it/450/320?image=178",
1516
"https://unsplash.it/450/320?image=179",
@@ -19,22 +20,23 @@ Cada uno de los proyectos tiene el siguiente formato (ejemplo):
1920
}
2021
```
2122

22-
# 📝 Instrucciones
23+
## 📝 Instrucciones:
24+
25+
1. Llama al endpoint e imprime el nombre del **SEGUNDO** proyecto en la lista:
2326

24-
Llame al punto final e imprima el nombre del **SEGUNDO** proyecto en la lista:
27+
Ejemplo de salida:
2528

26-
Ejemplo:
27-
```bash
29+
```text
2830
Coursera eLearning
2931
```
3032

31-
## 💡Pista
33+
## 💡 Pistas:
3234

33-
1. Abre el endpoint en tu navegador y analiza la respuesta que se encuentra en el body, necesitas saber qué esperar, cuál será la estructura de los datos (body response) que regresan del servidor.
34-
2. En este caso, el body response comienza con un corchete `[`, es una lista, debe acceder al segundo proyecto utilizando posiciones numéricas.
35-
2. Haga una solicitud GET al endpoint.
36-
3. Usa el [metodo .json()](https://www.w3schools.com/python/ref_requests_response.asp)para obtener el body response como un diccionario.
37-
4. Encuentra el segundo proyecto
38-
5. Imprime el nombre del proyecto, puedes acceder a la propiedad nombre "name" del diccionario del proyecto.
39-
6. No necesitas hacer un bucle (loop), solo accede al segundo elemento como una lista usando la posición
35+
+ Abre el endpoint en tu navegador y analiza la respuesta que se encuentra en el body, necesitas saber qué esperar, cuál será la estructura de los datos (response body) que devuelve el servidor.
36+
+ En este caso, el response body comienza con un corchete `[`, es una lista, debes acceder al segundo proyecto utilizando posiciones numéricas.
37+
+ Haz una solicitud GET al endpoint.
38+
+ Usa el [metodo .json()](https://www.w3schools.com/python/ref_requests_response.asp) para obtener el response body como un diccionario.
39+
+ Encuentra el segundo proyecto de la lista.
40+
+ Imprime el nombre del proyecto, puedes acceder a la propiedad `name` del diccionario del proyecto.
41+
+ No necesitas hacer un bucle (loop), solo accede al segundo elemento como una lista usando la posición.
4042

exercises/06-project-list/README.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# `06` Project List
22

3-
The following endpoint returns a JSON formated response with several projects in a list:
3+
The following endpoint returns a JSON formatted response with several projects in a list:
44

55
GET: [https://assets.breatheco.de/apis/fake/sample/project_list.php](https://assets.breatheco.de/apis/fake/sample/project_list.php)
66

77
Each of the projects has the following format (example):
8+
89
```json
910
{
1011
"name": "Coursera eLearning",
1112
"thumb": "https://unsplash.it/450/320?image=178",
12-
"description": "The coolest elarning site on the planet",
13+
"description": "The coolest eLearning site on the planet",
1314
"images": [
1415
"https://unsplash.it/450/320?image=178",
1516
"https://unsplash.it/450/320?image=179",
@@ -19,22 +20,22 @@ Each of the projects has the following format (example):
1920
}
2021
```
2122

22-
# 📝 Instructions
23+
## 📝 Instructions:
2324

24-
Please call the endpoint and print the name of the **SECOND** project on the list:
25+
1. Please call the endpoint and print the name of the **SECOND** project on the list:
2526

2627
Example output:
27-
```bash
28+
```text
2829
Coursera eLearning
2930
```
3031

31-
## 💡Hint
32+
## 💡 Hints:
3233

33-
1. Open the endpoint on your browser and analyze the response body, you need to know what to expect, what is going to be the structure of the data (response body) coming back from the server.
34-
2. In this case the response body starts with a square bracket `[`, it's a list, you have to access the second project by using numerical positions.
35-
2. Do a GET request to the endpoint.
36-
3. Use the [.json() method](https://www.w3schools.com/python/ref_requests_response.asp) to get the response body as a dictionary.
37-
4. Find the second project on the list.
38-
5. Print the project name, you can access the property name of the project dictionary.
39-
6. You don't need to loop, just access the second item like a list using the position
34+
+ Open the endpoint on your browser and analyze the response body, you need to know what to expect, what is going to be the structure of the data (response body) coming back from the server.
35+
+ In this case, the response body starts with a square bracket `[`, it's a list, and you have to access the second project by using numerical positions.
36+
+ Make a GET request to the endpoint.
37+
+ Use the [.json() method](https://www.w3schools.com/python/ref_requests_response.asp) to get the response body as a dictionary.
38+
+ Find the second project on the list.
39+
+ Print the project name, you can access the property `name` of the project dictionary.
40+
+ You don't need to loop, just access the second item like a list using the position.
4041

exercises/06-project-list/app.py

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

3-
# your code here
3+
# Your code here

0 commit comments

Comments
 (0)