Skip to content

Commit bd46239

Browse files
committed
my progress
1 parent 9ea60a2 commit bd46239

File tree

22 files changed

+237
-12
lines changed

22 files changed

+237
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import requests
2+
3+
url = "https://assets.breatheco.de/apis/fake/sample/404-example.php"
4+
# url = "https://assets.breatheco.de/apis/fake/sample/hello.php"
5+
response = requests.get(url)
6+
7+
print("The response status is: "+str(response.status_code))

.learn/resets/02-random-status/app.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php")

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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import requests
2+
3+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php")
4+
print(response.text)

.learn/resets/05-project-name/app.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here

.learn/resets/06-project-list/app.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import requests
2+
3+
def get_titles():
4+
# Your code here
5+
return None
6+
7+
8+
print(get_titles())

.learn/resets/10-get-post-tags/app.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import requests
2+
3+
def get_post_tags(post_id):
4+
# Your code here
5+
return None
6+
7+
8+
print(get_post_tags(146))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import requests
2+
3+
def get_attachment_by_id(attachment_id):
4+
# Your code here
5+
return None
6+
7+
print(get_attachment_by_id(137))
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import requests
22

3-
url = "https://assets.breatheco.de/apis/fake/sample/404-example.php"
4-
# url = "https://assets.breatheco.de/apis/fake/sample/hello.php"
3+
url ="https://assets.breatheco.de/apis/fake/sample/hello.php"
4+
# url original = "https://assets.breatheco.de/apis/fake/sample/404-example.php"
55
response = requests.get(url)
66

77
print("The response status is: "+str(response.status_code))

exercises/02-random-status/app.py

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

33
response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php")
4+
5+
# Diccionario que mapea códigos de estado a mensajes
6+
status_messages = {
7+
404: "The URL you asked for is not found",
8+
503: "Unavailable right now",
9+
200: "Everything went perfect",
10+
400: "Something is wrong with the request params"
11+
}
12+
13+
# Obtener el mensaje correspondiente al código de estado
14+
print(status_messages.get(response.status_code, "Unknown status code"))
15+
16+
# 🔥1. status_messages
17+
# Es un diccionario que hemos definido anteriormente, donde las claves son los códigos de estado
18+
# (como 200, 404, etc.) y los valores son los mensajes de texto asociados a esos códigos.
19+
20+
# 🔥2.response.status_code
21+
# Es un atributo del objeto response que contiene el código de estado HTTP devuelto por el servidor tras hacer una solicitud.
22+
# Este código indica el resultado de la solicitud HTTP (404, 200, etc)
23+
24+
# 🔥3.status_messages.get(...)
25+
# El método .get() de los diccionarios en Python busca una clave específica (en este caso,
26+
# el valor de response.status_code) y devuelve el valor asociado a esa clave.
27+
# Si la clave no se encuentra, .get() puede devolver un valor por defecto.

exercises/03-response-body/app.py

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

33
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
4+
5+
response = requests.get(url)
6+
# Just testing:
7+
# print(response.status_code)
8+
# print(response.content)
9+
# print(response.text)
10+
11+
# Diccionario que mapea códigos de estado a mensajes
12+
status_messages = {
13+
404: "The URL you asked for is not found",
14+
503: "Unavailable right now",
15+
200: "Everything went perfect",
16+
400: "Something is wrong with the request params"
17+
}
18+
19+
if response.status_code == 200:
20+
print(response.text)
21+
else:
22+
print("Something went wrong")
23+
+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
import requests
22

3-
response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php")
4-
print(response.text)
3+
url = "https://assets.breatheco.de/apis/fake/sample/time.php"
4+
response = requests.get(url)
5+
6+
# Verificar que la solicitud fue exitosa
7+
if response.status_code == 200:
8+
data = response.json() # Convertir la respuesta a JSON (diccionario)
9+
10+
# Obtener los valores de horas, minutos y segundos
11+
hours = data["hours"]
12+
minutes = data["minutes"]
13+
seconds = data["seconds"]
14+
15+
# Formatear la salida
16+
formatted_time = f"Current time: {hours} hrs {minutes} min and {seconds} sec"
17+
18+
# Imprimir el resultado
19+
print(formatted_time)
20+
else:
21+
print("Error fetching the time:", response.status_code)

exercises/05-project-name/app.py

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

3-
# Your code here
3+
url = "https://assets.breatheco.de/apis/fake/sample/project1.php"
4+
response = requests.get(url)
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/06-project-list/app.py

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

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

3-
# Your code here
3+
url = "https://assets.breatheco.de/apis/fake/sample/project_list.php"
4+
response = requests.get(url)
5+
6+
7+
if response.status_code == 200:
8+
# Parsing JSON response
9+
project_list = response.json()
10+
11+
# Extracting the last project
12+
last_project = project_list[-1]
13+
14+
# Extracting the last image URL
15+
last_image_url = last_project["images"][-1]
16+
17+
# Printing the last image URL
18+
print(last_image_url)
19+
else:
20+
print("Failed to fetch project list.")

exercises/08-blog-post-author/app.py

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

3-
# Your code here
3+
url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php"
4+
response = requests.get(url)
5+
6+
body = response.json()
7+
8+
""" Get the author name of the first post. """
9+
10+
out = body["posts"][0]["author"]["name"]
11+
12+
# print (type(out))
13+
# Esto lo uso para irme dando cuenta si el nivel al que necesito acceder
14+
# es una lista (accedo con el indice entre []) o
15+
# es un diccionario (accedo con el nombre de la clave entre [""])
16+
17+
print (out)
+19-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
import requests
22

3+
34
def get_titles():
45
# Your code here
5-
return None
6+
url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php"
7+
8+
titles = []
9+
10+
response = requests.get(url)
11+
12+
if response.status_code == 200:
13+
# Parsing JSON response
14+
data = response.json()
15+
16+
for post in data["posts"]:
17+
title = post["title"]
18+
if title:
19+
titles.append(title)
20+
else:
21+
print("Failed to fetch data from the endpoint.")
22+
23+
return titles
624

725

826
print(get_titles())

exercises/10-get-post-tags/app.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
import requests
22

3+
34
def get_post_tags(post_id):
45
# Your code here
5-
return None
6+
url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php"
7+
8+
response = requests.get(url)
9+
10+
if response.status_code == 200:
11+
# Parsing JSON response
12+
data = response.json()
13+
14+
# Loop through each post to find the one with matching post_id
15+
for post in data["posts"]:
16+
if post["id"] == post_id:
17+
return post["tags"]
18+
print("No post found")
19+
20+
else:
21+
print("Failed to fetch data from the endpoint.")
22+
23+
24+
print(get_post_tags(146))
25+
26+
627

728

8-
print(get_post_tags(146))
29+

exercises/11-get-attachment-by-id/app.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
def get_attachment_by_id(attachment_id):
44
# Your code here
5-
return None
5+
url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php"
6+
7+
response = requests.get(url)
8+
9+
if response.status_code == 200:
10+
# Parsing JSON response
11+
data = response.json()
12+
13+
for post in data["posts"]:
14+
# Check if the post has attachments
15+
if "attachments" in post:
16+
# Loop through each attachment
17+
for attachment in post["attachments"]:
18+
if attachment["id"] == attachment_id:
19+
return attachment["title"]
20+
21+
print("No attachment found")
22+
else:
23+
print("Failed to fetch data from the endpoint.")
624

725
print(get_attachment_by_id(137))

0 commit comments

Comments
 (0)