From 3099e48fb6ccf47ffdbbdcce70dde33840689bd2 Mon Sep 17 00:00:00 2001 From: mezcolantriz Date: Thu, 7 Nov 2024 22:28:09 +0000 Subject: [PATCH] ejercicios api requests --- .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 | 4 ++-- exercises/02-random-status/app.py | 11 +++++++++++ exercises/03-response-body/app.py | 9 +++++++++ exercises/04-response-body-json/app.py | 7 ++++++- exercises/05-project-name/app.py | 8 +++++++- exercises/06-project-list/app.py | 6 +++++- exercises/07-project-list-image/app.py | 7 ++++++- exercises/08-blog-post-author/app.py | 9 ++++++++- exercises/09-list-of-blog-titles/app.py | 14 +++++++++++--- exercises/10-get-post-tags/app.py | 11 ++++++++--- exercises/11-get-attachment-by-id/app.py | 10 +++++++++- exercises/12-post-request/app.py | 3 +++ exercises/13-post-request-body/app.py | 10 +++++++++- 26 files changed, 153 insertions(+), 15 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..6d39d8c 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/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/exercises/02-random-status/app.py b/exercises/02-random-status/app.py index d9ef815..2101f1e 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..ccfd9de 100644 --- a/exercises/03-response-body/app.py +++ b/exercises/03-response-body/app.py @@ -1,3 +1,12 @@ 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") + +print(response.status_code) \ 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..9887cd4 100644 --- a/exercises/04-response-body-json/app.py +++ b/exercises/04-response-body-json/app.py @@ -1,4 +1,9 @@ import requests response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php") -print(response.text) \ No newline at end of file +#print(response.text) + +json_r = response.json() +#print(json_r["hours"]) + +print(f"Current time:", (json_r["hours"]), "hrs", (json_r["minutes"]), "min and", (json_r["seconds"]), "sec") diff --git a/exercises/05-project-name/app.py b/exercises/05-project-name/app.py index 0ca5c86..0f438d4 100644 --- a/exercises/05-project-name/app.py +++ b/exercises/05-project-name/app.py @@ -1,3 +1,9 @@ import requests -# Your code here \ No newline at end of file +# Your code here +url = "https://assets.breatheco.de/apis/fake/sample/project1.php" +response = requests.get(url) + +json = response.json() +#print(json) +print(json["name"]) \ No newline at end of file diff --git a/exercises/06-project-list/app.py b/exercises/06-project-list/app.py index 0ca5c86..076380c 100644 --- a/exercises/06-project-list/app.py +++ b/exercises/06-project-list/app.py @@ -1,3 +1,7 @@ import requests -# Your code here \ No newline at end of file +# Your code here +url = "https://assets.breatheco.de/apis/fake/sample/project_list.php" +response = requests.get(url) +json = response.json() +print(json[1]["name"]) \ 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..3bdf628 100644 --- a/exercises/07-project-list-image/app.py +++ b/exercises/07-project-list-image/app.py @@ -1,3 +1,8 @@ import requests -# Your code here \ No newline at end of file +# Your code here +url = "https://assets.breatheco.de/apis/fake/sample/project_list.php" +response = requests.get(url) +json = response.json() +print(json[2]["images"][-1]) + diff --git a/exercises/08-blog-post-author/app.py b/exercises/08-blog-post-author/app.py index 0ca5c86..bd27fb6 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 +url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" +response = requests.get(url) +json = response.json() + +#print(json) + +print(json["posts"][0]["author"]["name"]) \ No newline at end of file diff --git a/exercises/09-list-of-blog-titles/app.py b/exercises/09-list-of-blog-titles/app.py index cc536a8..9c1a85f 100644 --- a/exercises/09-list-of-blog-titles/app.py +++ b/exercises/09-list-of-blog-titles/app.py @@ -1,8 +1,16 @@ import requests +url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" +#response = requests.get(url) +#json = response.json() + def get_titles(): - # Your code here - return None + titles = [] + response = requests.get(url) + json = response.json() + posts = json['posts'] + posts_titles = [ p["title"] for p in posts] + return posts_titles -print(get_titles()) \ No newline at end of file +print(get_titles()) diff --git a/exercises/10-get-post-tags/app.py b/exercises/10-get-post-tags/app.py index ff6d142..578d3cb 100644 --- a/exercises/10-get-post-tags/app.py +++ b/exercises/10-get-post-tags/app.py @@ -1,8 +1,13 @@ import requests -def get_post_tags(post_id): - # Your code here - return None +url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" +def get_post_tags(post_id): + response = requests.get(url) + json = response.json() + posts = {} + for p in json['posts']: + if p['id'] == post_id: + return p['tags'] print(get_post_tags(146)) \ 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..560e5e1 100644 --- a/exercises/11-get-attachment-by-id/app.py +++ b/exercises/11-get-attachment-by-id/app.py @@ -1,7 +1,15 @@ import requests +url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" def get_attachment_by_id(attachment_id): - # Your code here + response = requests.get(url) + data = response.json() + for post in data['posts']: + for attach in post['attachments']: + if attach['id'] == attachment_id: + return attach['title'] + + 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..19e0440 100644 --- a/exercises/12-post-request/app.py +++ b/exercises/12-post-request/app.py @@ -1,3 +1,6 @@ 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..307b78a 100644 --- a/exercises/13-post-request-body/app.py +++ b/exercises/13-post-request-body/app.py @@ -1,4 +1,12 @@ import requests -response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php") +body = {"id": 2323, "title": "Very big project"} +headers = { + "Content-Type": "application/json" +} + +response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php", +json=body, +headers=headers +) print(response.text) \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /