diff --git a/.learn/resets/01-creating-a-request/app.py b/.learn/resets/01-creating-a-request/app.py new file mode 100644 index 0000000..7b78511 --- /dev/null +++ b/.learn/resets/01-creating-a-request/app.py @@ -0,0 +1,7 @@ +import requests + +url = "https://assets.breatheco.de/apis/fake/sample/404-example.php" +# url = "https://assets.breatheco.de/apis/fake/sample/hello.php" +response = requests.get(url) + +print("The response status is: "+str(response.status_code)) \ No newline at end of file diff --git a/.learn/resets/02-random-status/app.py b/.learn/resets/02-random-status/app.py new file mode 100644 index 0000000..d9ef815 --- /dev/null +++ b/.learn/resets/02-random-status/app.py @@ -0,0 +1,3 @@ +import requests + +response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php") diff --git a/.learn/resets/03-response-body/app.py b/.learn/resets/03-response-body/app.py new file mode 100644 index 0000000..66b2f6b --- /dev/null +++ b/.learn/resets/03-response-body/app.py @@ -0,0 +1,3 @@ +import requests + +url = "https://assets.breatheco.de/apis/fake/sample/random-status.php" diff --git a/.learn/resets/04-response-body-json/app.py b/.learn/resets/04-response-body-json/app.py new file mode 100644 index 0000000..11ecbd8 --- /dev/null +++ b/.learn/resets/04-response-body-json/app.py @@ -0,0 +1,4 @@ +import requests + +response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php") +print(response.text) \ No newline at end of file diff --git a/.learn/resets/05-project-name/app.py b/.learn/resets/05-project-name/app.py new file mode 100644 index 0000000..0ca5c86 --- /dev/null +++ b/.learn/resets/05-project-name/app.py @@ -0,0 +1,3 @@ +import requests + +# Your code here \ No newline at end of file diff --git a/.learn/resets/06-project-list/app.py b/.learn/resets/06-project-list/app.py new file mode 100644 index 0000000..0ca5c86 --- /dev/null +++ b/.learn/resets/06-project-list/app.py @@ -0,0 +1,3 @@ +import requests + +# Your code here \ No newline at end of file diff --git a/.learn/resets/07-project-list-image/app.py b/.learn/resets/07-project-list-image/app.py new file mode 100644 index 0000000..0ca5c86 --- /dev/null +++ b/.learn/resets/07-project-list-image/app.py @@ -0,0 +1,3 @@ +import requests + +# Your code here \ No newline at end of file diff --git a/.learn/resets/08-blog-post-author/app.py b/.learn/resets/08-blog-post-author/app.py new file mode 100644 index 0000000..0ca5c86 --- /dev/null +++ b/.learn/resets/08-blog-post-author/app.py @@ -0,0 +1,3 @@ +import requests + +# Your code here \ No newline at end of file diff --git a/.learn/resets/09-list-of-blog-titles/app.py b/.learn/resets/09-list-of-blog-titles/app.py new file mode 100644 index 0000000..cc536a8 --- /dev/null +++ b/.learn/resets/09-list-of-blog-titles/app.py @@ -0,0 +1,8 @@ +import requests + +def get_titles(): + # Your code here + return None + + +print(get_titles()) \ No newline at end of file diff --git a/.learn/resets/10-get-post-tags/app.py b/.learn/resets/10-get-post-tags/app.py new file mode 100644 index 0000000..ff6d142 --- /dev/null +++ b/.learn/resets/10-get-post-tags/app.py @@ -0,0 +1,8 @@ +import requests + +def get_post_tags(post_id): + # Your code here + return None + + +print(get_post_tags(146)) \ No newline at end of file diff --git a/.learn/resets/11-get-attachment-by-id/app.py b/.learn/resets/11-get-attachment-by-id/app.py new file mode 100644 index 0000000..3571d84 --- /dev/null +++ b/.learn/resets/11-get-attachment-by-id/app.py @@ -0,0 +1,7 @@ +import requests + +def get_attachment_by_id(attachment_id): + # Your code here + return None + +print(get_attachment_by_id(137)) \ No newline at end of file diff --git a/.learn/resets/12-post-request/app.py b/.learn/resets/12-post-request/app.py new file mode 100644 index 0000000..0ca5c86 --- /dev/null +++ b/.learn/resets/12-post-request/app.py @@ -0,0 +1,3 @@ +import requests + +# Your code here \ No newline at end of file diff --git a/.learn/resets/13-post-request-body/app.py b/.learn/resets/13-post-request-body/app.py new file mode 100644 index 0000000..df545c3 --- /dev/null +++ b/.learn/resets/13-post-request-body/app.py @@ -0,0 +1,4 @@ +import requests + +response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php") +print(response.text) \ No newline at end of file diff --git a/exercises/01-creating-a-request/app.py b/exercises/01-creating-a-request/app.py index 7b78511..3ce09a6 100644 --- a/exercises/01-creating-a-request/app.py +++ b/exercises/01-creating-a-request/app.py @@ -1,7 +1,7 @@ import requests -url = "https://assets.breatheco.de/apis/fake/sample/404-example.php" -# url = "https://assets.breatheco.de/apis/fake/sample/hello.php" +url ="https://assets.breatheco.de/apis/fake/sample/hello.php" +# url original = "https://assets.breatheco.de/apis/fake/sample/404-example.php" response = requests.get(url) print("The response status is: "+str(response.status_code)) \ No newline at end of file diff --git a/exercises/02-random-status/app.py b/exercises/02-random-status/app.py index d9ef815..01ca13f 100644 --- a/exercises/02-random-status/app.py +++ b/exercises/02-random-status/app.py @@ -1,3 +1,27 @@ import requests response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php") + +# Diccionario que mapea c贸digos de estado a mensajes +status_messages = { + 404: "The URL you asked for is not found", + 503: "Unavailable right now", + 200: "Everything went perfect", + 400: "Something is wrong with the request params" +} + +# Obtener el mensaje correspondiente al c贸digo de estado +print(status_messages.get(response.status_code, "Unknown status code")) + +# 馃敟1. status_messages +# Es un diccionario que hemos definido anteriormente, donde las claves son los c贸digos de estado +# (como 200, 404, etc.) y los valores son los mensajes de texto asociados a esos c贸digos. + +# 馃敟2.response.status_code +# Es un atributo del objeto response que contiene el c贸digo de estado HTTP devuelto por el servidor tras hacer una solicitud. +# Este c贸digo indica el resultado de la solicitud HTTP (404, 200, etc) + +# 馃敟3.status_messages.get(...) +# El m茅todo .get() de los diccionarios en Python busca una clave espec铆fica (en este caso, +# el valor de response.status_code) y devuelve el valor asociado a esa clave. +# Si la clave no se encuentra, .get() puede devolver un valor por defecto. \ No newline at end of file diff --git a/exercises/03-response-body/app.py b/exercises/03-response-body/app.py index 66b2f6b..0dd1a74 100644 --- a/exercises/03-response-body/app.py +++ b/exercises/03-response-body/app.py @@ -1,3 +1,23 @@ import requests url = "https://assets.breatheco.de/apis/fake/sample/random-status.php" + +response = requests.get(url) +# Just testing: +# print(response.status_code) +# print(response.content) +# print(response.text) + +# Diccionario que mapea c贸digos de estado a mensajes +status_messages = { + 404: "The URL you asked for is not found", + 503: "Unavailable right now", + 200: "Everything went perfect", + 400: "Something is wrong with the request params" +} + +if response.status_code == 200: + print(response.text) +else: + print("Something went wrong") + diff --git a/exercises/04-response-body-json/app.py b/exercises/04-response-body-json/app.py index 11ecbd8..f004459 100644 --- a/exercises/04-response-body-json/app.py +++ b/exercises/04-response-body-json/app.py @@ -1,4 +1,21 @@ import requests -response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php") -print(response.text) \ No newline at end of file +url = "https://assets.breatheco.de/apis/fake/sample/time.php" +response = requests.get(url) + +# Verificar que la solicitud fue exitosa +if response.status_code == 200: + data = response.json() # Convertir la respuesta a JSON (diccionario) + + # Obtener los valores de horas, minutos y segundos + hours = data["hours"] + minutes = data["minutes"] + seconds = data["seconds"] + + # Formatear la salida + formatted_time = f"Current time: {hours} hrs {minutes} min and {seconds} sec" + + # Imprimir el resultado + print(formatted_time) +else: + print("Error fetching the time:", response.status_code) diff --git a/exercises/05-project-name/app.py b/exercises/05-project-name/app.py index 0ca5c86..7f97a3c 100644 --- a/exercises/05-project-name/app.py +++ b/exercises/05-project-name/app.py @@ -1,3 +1,15 @@ import requests -# Your code here \ No newline at end of file +url = "https://assets.breatheco.de/apis/fake/sample/project1.php" +response = requests.get(url) + +if response.status_code == 200: + # Parsing JSON response + project_data = response.json() + + # Extracting project name + project_name = project_data["name"] + + print(project_name) +else: + print("Failed to fetch project data.") \ No newline at end of file diff --git a/exercises/06-project-list/app.py b/exercises/06-project-list/app.py index 0ca5c86..1f96f58 100644 --- a/exercises/06-project-list/app.py +++ b/exercises/06-project-list/app.py @@ -1,3 +1,15 @@ import requests -# Your code here \ No newline at end of file +url = "https://assets.breatheco.de/apis/fake/sample/project_list.php" +response = requests.get(url) + +if response.status_code == 200: + # Parsing JSON response + second_project = response.json()[1] + + # Extracting second project name + project_name = second_project["name"] + + print(project_name) +else: + print("Failed to fetch project data.") \ No newline at end of file diff --git a/exercises/07-project-list-image/app.py b/exercises/07-project-list-image/app.py index 0ca5c86..b0863bd 100644 --- a/exercises/07-project-list-image/app.py +++ b/exercises/07-project-list-image/app.py @@ -1,3 +1,20 @@ import requests -# Your code here \ No newline at end of file +url = "https://assets.breatheco.de/apis/fake/sample/project_list.php" +response = requests.get(url) + + +if response.status_code == 200: + # Parsing JSON response + project_list = response.json() + + # Extracting the last project + last_project = project_list[-1] + + # Extracting the last image URL + last_image_url = last_project["images"][-1] + + # Printing the last image URL + print(last_image_url) +else: + print("Failed to fetch project list.") \ No newline at end of file diff --git a/exercises/08-blog-post-author/app.py b/exercises/08-blog-post-author/app.py index 0ca5c86..eadbdd8 100644 --- a/exercises/08-blog-post-author/app.py +++ b/exercises/08-blog-post-author/app.py @@ -1,3 +1,17 @@ import requests -# Your code here \ No newline at end of file +url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" +response = requests.get(url) + +body = response.json() + +""" Get the author name of the first post. """ + +out = body["posts"][0]["author"]["name"] + +# print (type(out)) +# Esto lo uso para irme dando cuenta si el nivel al que necesito acceder +# es una lista (accedo con el indice entre []) o +# es un diccionario (accedo con el nombre de la clave entre [""]) + +print (out) diff --git a/exercises/09-list-of-blog-titles/app.py b/exercises/09-list-of-blog-titles/app.py index cc536a8..3a69d07 100644 --- a/exercises/09-list-of-blog-titles/app.py +++ b/exercises/09-list-of-blog-titles/app.py @@ -1,8 +1,26 @@ import requests + def get_titles(): # Your code here - return None + url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" + + titles = [] + + response = requests.get(url) + + if response.status_code == 200: + # Parsing JSON response + data = response.json() + + for post in data["posts"]: + title = post["title"] + if title: + titles.append(title) + else: + print("Failed to fetch data from the endpoint.") + + return titles print(get_titles()) \ No newline at end of file diff --git a/exercises/10-get-post-tags/app.py b/exercises/10-get-post-tags/app.py index ff6d142..db34fe4 100644 --- a/exercises/10-get-post-tags/app.py +++ b/exercises/10-get-post-tags/app.py @@ -1,8 +1,29 @@ import requests + def get_post_tags(post_id): # Your code here - return None + url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" + + response = requests.get(url) + + if response.status_code == 200: + # Parsing JSON response + data = response.json() + + # Loop through each post to find the one with matching post_id + for post in data["posts"]: + if post["id"] == post_id: + return post["tags"] + print("No post found") + + else: + print("Failed to fetch data from the endpoint.") + + +print(get_post_tags(146)) + + -print(get_post_tags(146)) \ No newline at end of file + \ No newline at end of file diff --git a/exercises/11-get-attachment-by-id/app.py b/exercises/11-get-attachment-by-id/app.py index 3571d84..d09dc7d 100644 --- a/exercises/11-get-attachment-by-id/app.py +++ b/exercises/11-get-attachment-by-id/app.py @@ -2,6 +2,24 @@ def get_attachment_by_id(attachment_id): # Your code here - return None + url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" + + response = requests.get(url) + + if response.status_code == 200: + # Parsing JSON response + data = response.json() + + for post in data["posts"]: + # Check if the post has attachments + if "attachments" in post: + # Loop through each attachment + for attachment in post["attachments"]: + if attachment["id"] == attachment_id: + return attachment["title"] + + print("No attachment found") + else: + print("Failed to fetch data from the endpoint.") print(get_attachment_by_id(137)) \ No newline at end of file diff --git a/exercises/12-post-request/app.py b/exercises/12-post-request/app.py index 0ca5c86..be6be5b 100644 --- a/exercises/12-post-request/app.py +++ b/exercises/12-post-request/app.py @@ -1,3 +1,9 @@ import requests +url = "https://assets.breatheco.de/apis/fake/sample/post.php" + +response = requests.post(url) + +print(response.text) + # Your code here \ No newline at end of file diff --git a/exercises/13-post-request-body/app.py b/exercises/13-post-request-body/app.py index df545c3..73cebd2 100644 --- a/exercises/13-post-request-body/app.py +++ b/exercises/13-post-request-body/app.py @@ -1,4 +1,19 @@ import requests -response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php") + +url = "https://assets.breatheco.de/apis/fake/sample/save-project-json.php" + +data = { + "id": 2323, + "title": "Very big project" +} + +# Setting the headers +headers = { + "Content-Type": "application/json" +} + +# Sending POST request with dictionary data +response = requests.post(url, json=data, headers=headers) + print(response.text) \ No newline at end of file