File tree 22 files changed +237
-12
lines changed
22 files changed +237
-12
lines changed Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ response = requests .get ("https://assets.breatheco.de/apis/fake/sample/random-status.php" )
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ response = requests .get ("https://assets.breatheco.de/apis/fake/sample/time.php" )
4
+ print (response .text )
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ # Your code here
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ # Your code here
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ # Your code here
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ # Your code here
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ def get_titles ():
4
+ # Your code here
5
+ return None
6
+
7
+
8
+ print (get_titles ())
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change 1
1
import requests
2
2
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"
5
5
response = requests .get (url )
6
6
7
7
print ("The response status is: " + str (response .status_code ))
Original file line number Diff line number Diff line change 1
1
import requests
2
2
3
3
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.
Original file line number Diff line number Diff line change 1
1
import requests
2
2
3
3
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
+
Original file line number Diff line number Diff line change 1
1
import requests
2
2
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 )
Original file line number Diff line number Diff line change 1
1
import requests
2
2
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." )
Original file line number Diff line number Diff line change 1
1
import requests
2
2
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." )
Original file line number Diff line number Diff line change 1
1
import requests
2
2
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." )
Original file line number Diff line number Diff line change 1
1
import requests
2
2
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 )
Original file line number Diff line number Diff line change 1
1
import requests
2
2
3
+
3
4
def get_titles ():
4
5
# 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
6
24
7
25
8
26
print (get_titles ())
Original file line number Diff line number Diff line change 1
1
import requests
2
2
3
+
3
4
def get_post_tags (post_id ):
4
5
# 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
+
6
27
7
28
8
- print ( get_post_tags ( 146 ))
29
+
Original file line number Diff line number Diff line change 2
2
3
3
def get_attachment_by_id (attachment_id ):
4
4
# 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." )
6
24
7
25
print (get_attachment_by_id (137 ))
You can’t perform that action at this time.
0 commit comments