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..78f713a 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..c47b14e 100644 --- a/exercises/03-response-body/app.py +++ b/exercises/03-response-body/app.py @@ -1,3 +1,11 @@ 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') + diff --git a/exercises/04-response-body-json/app.py b/exercises/04-response-body-json/app.py index 11ecbd8..dcd3358 100644 --- a/exercises/04-response-body-json/app.py +++ b/exercises/04-response-body-json/app.py @@ -1,4 +1,15 @@ 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: + timeData = response.json() + + hours = timeData["hours"] + minutes = timeData["minutes"] + seconds = timeData["seconds"] + + print(f"Current time: { hours } hrs { minutes } min and { seconds } sec") +else: + print("Failed to fetch current time.") + diff --git a/exercises/05-project-name/app.py b/exercises/05-project-name/app.py index 0ca5c86..df5dcb4 100644 --- a/exercises/05-project-name/app.py +++ b/exercises/05-project-name/app.py @@ -1,3 +1,16 @@ import requests -# Your code here \ No newline at end of file +# Your code here + + +response = requests.get("https://assets.breatheco.de/apis/fake/sample/project1.php") + +if response.status_code == 200: + Data = response.json() + + name = Data["name"] + + print(name) +else: + print("Failed to fetch current time.") + diff --git a/exercises/06-project-list/app.py b/exercises/06-project-list/app.py index 0ca5c86..3d6b8e1 100644 --- a/exercises/06-project-list/app.py +++ b/exercises/06-project-list/app.py @@ -1,3 +1,16 @@ import requests -# Your code here \ No newline at end of file +# Your code here + +response = requests.get("https://assets.breatheco.de/apis/fake/sample/project_list.php") + + +if response.status_code == 200: + Data = response.json() + + second = Data[1]["name"] + + print(second) +else: + print("Failed to fetch current time.") + \ 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..b52a93f 100644 --- a/exercises/07-project-list-image/app.py +++ b/exercises/07-project-list-image/app.py @@ -1,3 +1,19 @@ import requests -# Your code here \ No newline at end of file +# Your code here +response = requests.get("https://assets.breatheco.de/apis/fake/sample/project_list.php") + +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/07-project-list-image/solution.hide.py b/exercises/07-project-list-image/solution.hide.py index b52a93f..c254349 100644 --- a/exercises/07-project-list-image/solution.hide.py +++ b/exercises/07-project-list-image/solution.hide.py @@ -1,7 +1,7 @@ import requests # Your code here -response = requests.get("https://assets.breatheco.de/apis/fake/sample/project_list.php") +response = requests.get("https://image.shutterstock.com/image-vector/trophy-cup-award-vector-icon-260nw-592525184.jpg") if response.status_code == 200: # Parsing JSON response diff --git a/exercises/08-blog-post-author/app.py b/exercises/08-blog-post-author/app.py index 0ca5c86..81cff1b 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 +# Your code here +response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php") + +if response.status_code == 200: + data = response.json() + + first_post = data["posts"][0] + + author_dict = first_post["author"] + + author_name = author_dict["name"] + + print(author_name) +else: + print("Failed to fetch data from the endpoint.") \ 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..25b1e27 100644 --- a/exercises/09-list-of-blog-titles/app.py +++ b/exercises/09-list-of-blog-titles/app.py @@ -1,8 +1,25 @@ 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: + data = response.json() + + for post in data["posts"]: + title = post["title"] + if title: + titles.append(title) + else: + print("Error") + + 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..25c19e1 100644 --- a/exercises/10-get-post-tags/app.py +++ b/exercises/10-get-post-tags/app.py @@ -1,8 +1,24 @@ import requests def get_post_tags(post_id): - # Your code here - return None + + + url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" + + postTags = [] + + response = requests.get(url) + + if response.status_code == 200: + data = response.json() + + for post in data["posts"]: + if post["id"] == post_id: + return post["tags"] + else: + print("Error") + + return postTags 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..bd8f33e 100644 --- a/exercises/11-get-attachment-by-id/app.py +++ b/exercises/11-get-attachment-by-id/app.py @@ -1,7 +1,28 @@ import requests def get_attachment_by_id(attachment_id): - # Your code here + + url = "https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php" + + postTags = [] + response = requests.get(url) + if response.status_code == 200: + + data = response.json() + + for post in data["posts"]: + + if "attachments" in post: + + for attachment in post["attachments"]: + if attachment["id"] == attachment_id: + return attachment["title"] + else: + print("Error") + + + + 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..4de7a53 100644 --- a/exercises/12-post-request/app.py +++ b/exercises/12-post-request/app.py @@ -1,3 +1,7 @@ import requests -# Your code here \ No newline at end of file +url = "https://assets.breatheco.de/apis/fake/sample/post.php" + +response = requests.post(url) + +print(response.text) \ 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..97ca6d4 100644 --- a/exercises/13-post-request-body/app.py +++ b/exercises/13-post-request-body/app.py @@ -1,4 +1,18 @@ 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