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..0e6f2f4 100644 --- a/exercises/01-creating-a-request/app.py +++ b/exercises/01-creating-a-request/app.py @@ -4,4 +4,10 @@ # 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 +print("The response status is: "+str(response.status_code)) + + +#exercise 1 + +response = requests.get('https://assets.breatheco.de/apis/fake/sample/hello.php') +print(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..135d801 100644 --- a/exercises/02-random-status/app.py +++ b/exercises/02-random-status/app.py @@ -1,3 +1,16 @@ 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") + + diff --git a/exercises/03-response-body/app.py b/exercises/03-response-body/app.py index 66b2f6b..9409dea 100644 --- a/exercises/03-response-body/app.py +++ b/exercises/03-response-body/app.py @@ -1,3 +1,10 @@ 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..cc2f069 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: + # Parsing JSON response + time_data = response.json() + + # Extracting hours, minutes, and seconds + hours = time_data["hours"] + minutes = time_data["minutes"] + seconds = time_data["seconds"] + + print(f"Current time: {hours} hrs {minutes} min and {seconds} sec") +else: + print("Failed to fetch current time.") \ No newline at end of file diff --git a/exercises/05-project-name/app.py b/exercises/05-project-name/app.py index 0ca5c86..1a7bd82 100644 --- a/exercises/05-project-name/app.py +++ b/exercises/05-project-name/app.py @@ -1,3 +1,8 @@ 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") + +data = response.json() + +print(data["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..81cb843 100644 --- a/exercises/06-project-list/app.py +++ b/exercises/06-project-list/app.py @@ -1,3 +1,8 @@ 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") + +data = response.json() + +print(data[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..fee9379 100644 --- a/exercises/07-project-list-image/app.py +++ b/exercises/07-project-list-image/app.py @@ -1,3 +1,10 @@ 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") + + +data = response.json() + + +print(data[2]["images"][2]) \ 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..87728a6 100644 --- a/exercises/08-blog-post-author/app.py +++ b/exercises/08-blog-post-author/app.py @@ -1,3 +1,11 @@ import requests -# Your code here \ No newline at end of file +response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php") + +data = response.json() + + +print(data["posts"][0]["author"]["name"] + + ) + diff --git a/exercises/09-list-of-blog-titles/app.py b/exercises/09-list-of-blog-titles/app.py index cc536a8..6060c2f 100644 --- a/exercises/09-list-of-blog-titles/app.py +++ b/exercises/09-list-of-blog-titles/app.py @@ -2,7 +2,27 @@ 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 + +print(get_titles()) + #for post in range(len(data["posts"])): + # titles.append(data["posts"][post]["title"]) + \ 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..5fe9d55 100644 --- a/exercises/10-get-post-tags/app.py +++ b/exercises/10-get-post-tags/app.py @@ -1,8 +1,22 @@ 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: + data = response.json() + + for post in data["posts"]: + if post["id"] == post_id: + return post["tags"] + # for values in data["posts"][0]["tags"]: + # tags_.append(values) + + else: + print("Failed to fetch data from the endpoint") + + + -print(get_post_tags(146)) \ No newline at end of file +print(get_post_tags(146)) diff --git a/exercises/11-get-attachment-by-id/app.py b/exercises/11-get-attachment-by-id/app.py index 3571d84..e83883a 100644 --- a/exercises/11-get-attachment-by-id/app.py +++ b/exercises/11-get-attachment-by-id/app.py @@ -1,7 +1,19 @@ import requests 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: + 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"] + 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..3a8f4a5 100644 --- a/exercises/12-post-request/app.py +++ b/exercises/12-post-request/app.py @@ -1,3 +1,8 @@ import requests -# Your code here \ No newline at end of file +url = "https://assets.breatheco.de/apis/fake/sample/post.php" + +myobj = {"somekey":"somevalue"} + +x = requests.post(url, json = myobj) +print(x.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

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