Skip to content

#01 - Included exercises solutions #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .learn/resets/01-creating-a-request/app.py
Original file line number Diff line number Diff line change
@@ -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))
3 changes: 3 additions & 0 deletions .learn/resets/02-random-status/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import requests

response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php")
3 changes: 3 additions & 0 deletions .learn/resets/03-response-body/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import requests

url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
4 changes: 4 additions & 0 deletions .learn/resets/04-response-body-json/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import requests

response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php")
print(response.text)
3 changes: 3 additions & 0 deletions .learn/resets/05-project-name/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import requests

# Your code here
3 changes: 3 additions & 0 deletions .learn/resets/06-project-list/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import requests

# Your code here
3 changes: 3 additions & 0 deletions .learn/resets/07-project-list-image/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import requests

# Your code here
3 changes: 3 additions & 0 deletions .learn/resets/08-blog-post-author/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import requests

# Your code here
8 changes: 8 additions & 0 deletions .learn/resets/09-list-of-blog-titles/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import requests

def get_titles():
# Your code here
return None


print(get_titles())
8 changes: 8 additions & 0 deletions .learn/resets/10-get-post-tags/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import requests

def get_post_tags(post_id):
# Your code here
return None


print(get_post_tags(146))
7 changes: 7 additions & 0 deletions .learn/resets/11-get-attachment-by-id/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import requests

def get_attachment_by_id(attachment_id):
# Your code here
return None

print(get_attachment_by_id(137))
3 changes: 3 additions & 0 deletions .learn/resets/12-post-request/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import requests

# Your code here
4 changes: 4 additions & 0 deletions .learn/resets/13-post-request-body/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import requests

response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php")
print(response.text)
2 changes: 1 addition & 1 deletion exercises/01-creating-a-request/app.py
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
11 changes: 11 additions & 0 deletions exercises/02-random-status/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import requests

response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php")

if response.status_code == 200:
print("Everything went perfect")
elif response.status_code == 404:
print("The URL you asked for is not found")
elif response.status_code == 400:
print("Something is wrong with the request params")
elif response.status_code == 503:
print("Unavailable right now")
else:
print("Unknown status code")
7 changes: 7 additions & 0 deletions exercises/03-response-body/app.py
Original file line number Diff line number Diff line change
@@ -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")
5 changes: 4 additions & 1 deletion exercises/04-response-body-json/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import requests

response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php")
print(response.text)

body = response.json()

print(f"Current time: {body['hours']} hrs {body['minutes']} min and {body['seconds']} sec")
8 changes: 7 additions & 1 deletion exercises/05-project-name/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import requests

# Your code here
# Your code here

response = requests.get("https://assets.breatheco.de/apis/fake/sample/project1.php")

body = response.json()

print(body['name'])
7 changes: 6 additions & 1 deletion exercises/06-project-list/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import requests

# Your code here
# Your code here

response = requests.get("https://assets.breatheco.de/apis/fake/sample/project_list.php")
body = response.json()

print(body[1]["name"])
6 changes: 5 additions & 1 deletion exercises/07-project-list-image/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import requests

# Your code here
# Your code here
response = requests.get("https://assets.breatheco.de/apis/fake/sample/project_list.php")
body = response.json()

print(body[-1]['images'][-1])
7 changes: 6 additions & 1 deletion exercises/08-blog-post-author/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import requests

# Your code here
# Your code here
response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php")

body = response.json()

print(body['posts'][0]['author']['name'])
8 changes: 7 additions & 1 deletion exercises/09-list-of-blog-titles/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

def get_titles():
# Your code here
return None
titles = [] # Empty list
response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php")
body = response.json()
for post in body['posts']:
titles.append(post['title'])

return titles


print(get_titles())
5 changes: 5 additions & 0 deletions exercises/10-get-post-tags/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

def get_post_tags(post_id):
# Your code here
response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php")
body = response.json()
for post in body['posts']:
if post['id'] == post_id:
return post['tags']
return None


Expand Down
6 changes: 6 additions & 0 deletions exercises/11-get-attachment-by-id/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

def get_attachment_by_id(attachment_id):
# Your code here
response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php")
body = response.json()
for post in body['posts']:
for attachment in post['attachments']:
if attachment_id == attachment['id']:
return attachment['title']
return None

print(get_attachment_by_id(137))
8 changes: 7 additions & 1 deletion exercises/12-post-request/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import requests

# Your code here
# Your code here

headers = {'Content-Type': 'application/json'}
body_json = {}

response = requests.post("https://assets.breatheco.de/apis/fake/sample/post.php", data=body_json, headers=headers, verify=False)
print(response.text)
8 changes: 7 additions & 1 deletion exercises/13-post-request-body/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import requests

response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php")
headers = {'Content-Type': 'application/json'}
body_json = {
"id": 2323,
"title": "Very big project"
}

response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php", json=body_json, headers=headers)
print(response.text)