Skip to content

Commit 8e299a4

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

File tree

62 files changed

+468
-291
lines changed

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 10 deletions
This file was deleted.

exercises/00-welcome/README.es.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 16 additions & 0 deletions
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.")

0 commit comments

Comments
 (0)