From 74b819f965bf39b49f597e86e39a7d6b67941b2a Mon Sep 17 00:00:00 2001 From: Lsiniestro <164952161+Lsiniestro@users.noreply.github.com> Date: Sun, 26 May 2024 19:02:45 +0000 Subject: [PATCH] ejercicios resueltos --- .learn/resets/01-creating-a-request/app.py | 7 +++++++ .learn/resets/02-random-status/app.py | 3 +++ .learn/resets/03-response-body/app.py | 3 +++ .learn/resets/04-response-body-json/app.py | 4 ++++ .learn/resets/05-project-name/app.py | 3 +++ .learn/resets/06-project-list/app.py | 3 +++ .learn/resets/07-project-list-image/app.py | 3 +++ .learn/resets/08-blog-post-author/app.py | 3 +++ .learn/resets/09-list-of-blog-titles/app.py | 8 ++++++++ .learn/resets/10-get-post-tags/app.py | 8 ++++++++ .learn/resets/11-get-attachment-by-id/app.py | 7 +++++++ .learn/resets/12-post-request/app.py | 3 +++ .learn/resets/13-post-request-body/app.py | 4 ++++ exercises/01-creating-a-request/app.py | 2 +- exercises/02-random-status/app.py | 11 +++++++++++ exercises/03-response-body/app.py | 6 ++++++ exercises/04-response-body-json/app.py | 14 +++++++++++++- exercises/05-project-name/app.py | 12 +++++++++++- exercises/06-project-list/app.py | 10 +++++++++- exercises/07-project-list-image/app.py | 11 ++++++++++- exercises/08-blog-post-author/app.py | 9 ++++++++- exercises/09-list-of-blog-titles/app.py | 10 +++++++++- exercises/10-get-post-tags/app.py | 15 +++++++++++++++ exercises/11-get-attachment-by-id/app.py | 19 +++++++++++++++++++ exercises/12-post-request/app.py | 8 +++++++- exercises/13-post-request-body/app.py | 15 +++++++++++++-- 26 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 .learn/resets/01-creating-a-request/app.py create mode 100644 .learn/resets/02-random-status/app.py create mode 100644 .learn/resets/03-response-body/app.py create mode 100644 .learn/resets/04-response-body-json/app.py create mode 100644 .learn/resets/05-project-name/app.py create mode 100644 .learn/resets/06-project-list/app.py create mode 100644 .learn/resets/07-project-list-image/app.py create mode 100644 .learn/resets/08-blog-post-author/app.py create mode 100644 .learn/resets/09-list-of-blog-titles/app.py create mode 100644 .learn/resets/10-get-post-tags/app.py create mode 100644 .learn/resets/11-get-attachment-by-id/app.py create mode 100644 .learn/resets/12-post-request/app.py create mode 100644 .learn/resets/13-post-request-body/app.py 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..717626a 100644 --- a/exercises/01-creating-a-request/app.py +++ b/exercises/01-creating-a-request/app.py @@ -1,6 +1,6 @@ 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" response = requests.get(url) diff --git a/exercises/02-random-status/app.py b/exercises/02-random-status/app.py index d9ef815..926b76b 100644 --- a/exercises/02-random-status/app.py +++ b/exercises/02-random-status/app.py @@ -1,3 +1,14 @@ import requests response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php") + +if response.status_code == 404: + print ("The URL you asked for is not found") +elif response.status_code == 503: + print("Unavailable right now") +elif response.status_code == 200: + print("Everything went perfect") +elif response.status_code == 400: + print("Something is wrong with the request params") +else: + print("Unknown status code") \ No newline at end of file diff --git a/exercises/03-response-body/app.py b/exercises/03-response-body/app.py index 66b2f6b..7a1da29 100644 --- a/exercises/03-response-body/app.py +++ b/exercises/03-response-body/app.py @@ -1,3 +1,9 @@ import requests url = "https://assets.breatheco.de/apis/fake/sample/random-status.php" +response = requests.get (url) + +if response.status_code ==200: + print(response.text) +else: + print("Something went wrong") \ No newline at end of file diff --git a/exercises/04-response-body-json/app.py b/exercises/04-response-body-json/app.py index 11ecbd8..6cb6990 100644 --- a/exercises/04-response-body-json/app.py +++ b/exercises/04-response-body-json/app.py @@ -1,4 +1,16 @@ import requests response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php") -print(response.text) \ No newline at end of file + + + +if response.status_code == 200: + hora=response.json() + horas = hora["hours"] + minutos = hora["minutes"] + segundos = hora["seconds"] +else: + ('No se pudo obtener la hora') + + +print(f'Current time: {horas} hrs {minutos} min and {segundos} sec') \ No newline at end of file diff --git a/exercises/05-project-name/app.py b/exercises/05-project-name/app.py index 0ca5c86..6d64e5e 100644 --- a/exercises/05-project-name/app.py +++ b/exercises/05-project-name/app.py @@ -1,3 +1,13 @@ import requests -# Your code here \ No newline at end of file +# Your code here + +respuesta=requests.get('https://assets.breatheco.de/apis/fake/sample/project1.php') + +if respuesta.status_code ==200: + body= respuesta.json() + + nombre= body['name'] + print(nombre) +else: + print('Error de conexion') \ No newline at end of file diff --git a/exercises/06-project-list/app.py b/exercises/06-project-list/app.py index 0ca5c86..2ed6d60 100644 --- a/exercises/06-project-list/app.py +++ b/exercises/06-project-list/app.py @@ -1,3 +1,11 @@ import requests -# Your code here \ No newline at end of file +# Your code here +respuesta=requests.get('https://assets.breatheco.de/apis/fake/sample/project_list.php') + +if respuesta.status_code ==200: + body= respuesta.json() + + nombre= body[1]['name'] + print(nombre) + diff --git a/exercises/07-project-list-image/app.py b/exercises/07-project-list-image/app.py index 0ca5c86..a1d40f9 100644 --- a/exercises/07-project-list-image/app.py +++ b/exercises/07-project-list-image/app.py @@ -1,3 +1,12 @@ import requests -# Your code here \ No newline at end of file +# Your code here + +respuesta=requests.get('https://assets.breatheco.de/apis/fake/sample/project_list.php') + +if respuesta.status_code ==200: + body= respuesta.json() + + imagen= body[2]['images'][2] + print(imagen) + diff --git a/exercises/08-blog-post-author/app.py b/exercises/08-blog-post-author/app.py index 0ca5c86..50d3b2e 100644 --- a/exercises/08-blog-post-author/app.py +++ b/exercises/08-blog-post-author/app.py @@ -1,3 +1,10 @@ import requests -# Your code here \ No newline at end of file +# Your code here +respuesta=requests.get('https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php') + +if respuesta.status_code ==200: + body= respuesta.json() + + nombre= body['posts'][0]['author']['name'] + print(nombre) diff --git a/exercises/09-list-of-blog-titles/app.py b/exercises/09-list-of-blog-titles/app.py index cc536a8..042ab0b 100644 --- a/exercises/09-list-of-blog-titles/app.py +++ b/exercises/09-list-of-blog-titles/app.py @@ -2,7 +2,15 @@ def get_titles(): # Your code here - return None + titles= [] + respuesta=requests.get('https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php') + if respuesta.status_code ==200: + body= respuesta.json() + + + + + 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..a52bd74 100644 --- a/exercises/10-get-post-tags/app.py +++ b/exercises/10-get-post-tags/app.py @@ -1,7 +1,22 @@ import requests + + def get_post_tags(post_id): # Your code here + + respuesta=requests.get('https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php') + + if respuesta.status_code ==200: + body= respuesta.json() + + for post in body["posts"]: + if post['id'] == post_id: + return post['tags'] + + + + return None diff --git a/exercises/11-get-attachment-by-id/app.py b/exercises/11-get-attachment-by-id/app.py index 3571d84..a65861e 100644 --- a/exercises/11-get-attachment-by-id/app.py +++ b/exercises/11-get-attachment-by-id/app.py @@ -2,6 +2,25 @@ def get_attachment_by_id(attachment_id): # Your code here + respuesta=requests.get('https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php') + + if respuesta.status_code ==200: + body= respuesta.json() + + for post in body["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.") + + + return None 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..2548f31 100644 --- a/exercises/12-post-request/app.py +++ b/exercises/12-post-request/app.py @@ -1,3 +1,9 @@ import requests -# Your code here \ No newline at end of file +url = 'https://assets.breatheco.de/apis/fake/sample/post.php' +datos = {'prueba': 'texto de prueba'} + +mensaje = requests.post(url, json = datos) + +print(mensaje.text) +# Your code here diff --git a/exercises/13-post-request-body/app.py b/exercises/13-post-request-body/app.py index df545c3..d4acbbf 100644 --- a/exercises/13-post-request-body/app.py +++ b/exercises/13-post-request-body/app.py @@ -1,4 +1,15 @@ 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 +#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' +datos ={ + "id": 2323, + "title": "Very big project" +} +headers = { + "Content-Type": "application/json" +} + +mensaje = requests.post(url, json = datos, headers=headers) + +print(mensaje.text) \ No newline at end of file