Skip to content

Commit 42ac0a3

Browse files
committed
#1 - Included exercises solutions
1 parent 5792b06 commit 42ac0a3

File tree

26 files changed

+138
-9
lines changed

26 files changed

+138
-9
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import requests
2+
3+
url = "https://assets.breatheco.de/apis/fake/sample/404-example.php"
4+
# url = "https://assets.breatheco.de/apis/fake/sample/hello.php"
5+
response = requests.get(url)
6+
7+
print("The response status is: "+str(response.status_code))

.learn/resets/02-random-status/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php")

.learn/resets/03-response-body/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import requests
2+
3+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php")
4+
print(response.text)

.learn/resets/05-project-name/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here

.learn/resets/06-project-list/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import requests
2+
3+
def get_titles():
4+
# Your code here
5+
return None
6+
7+
8+
print(get_titles())

.learn/resets/10-get-post-tags/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import requests
2+
3+
def get_post_tags(post_id):
4+
# Your code here
5+
return None
6+
7+
8+
print(get_post_tags(146))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import requests
2+
3+
def get_attachment_by_id(attachment_id):
4+
# Your code here
5+
return None
6+
7+
print(get_attachment_by_id(137))

.learn/resets/12-post-request/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# Your code here
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import requests
2+
3+
response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php")
4+
print(response.text)

exercises/01-creating-a-request/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests
22

3-
url = "https://assets.breatheco.de/apis/fake/sample/404-example.php"
3+
url = "https://assets.breatheco.de/apis/fake/sample/hello.php"
44
# url = "https://assets.breatheco.de/apis/fake/sample/hello.php"
55
response = requests.get(url)
66

exercises/02-random-status/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
import requests
22

33
response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php")
4+
5+
if response.status_code == 200:
6+
print("Everything went perfect")
7+
elif response.status_code == 404:
8+
print("The URL you asked for is not found")
9+
elif response.status_code == 400:
10+
print("Something is wrong with the request params")
11+
elif response.status_code == 503:
12+
print("Unavailable right now")
13+
else:
14+
print("Unknown status code")

exercises/03-response-body/app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import requests
22

33
url = "https://assets.breatheco.de/apis/fake/sample/random-status.php"
4+
5+
response = requests.get(url)
6+
7+
if response.status_code == 200:
8+
print(response.text)
9+
else:
10+
print("Something went wrong")
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import requests
22

33
response = requests.get("https://assets.breatheco.de/apis/fake/sample/time.php")
4-
print(response.text)
4+
5+
body = response.json()
6+
7+
print(f"Current time: {body['hours']} hrs {body['minutes']} min and {body['seconds']} sec")

exercises/05-project-name/app.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
import requests
22

3-
# Your code here
3+
# Your code here
4+
5+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/project1.php")
6+
7+
body = response.json()
8+
9+
print(body['name'])

exercises/06-project-list/app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
import requests
22

3-
# Your code here
3+
# Your code here
4+
5+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/project_list.php")
6+
body = response.json()
7+
8+
print(body[1]["name"])
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import requests
22

3-
# Your code here
3+
# Your code here
4+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/project_list.php")
5+
body = response.json()
6+
7+
print(body[-1]['images'][-1])

exercises/08-blog-post-author/app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
import requests
22

3-
# Your code here
3+
# Your code here
4+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php")
5+
6+
body = response.json()
7+
8+
print(body['posts'][0]['author']['name'])

exercises/09-list-of-blog-titles/app.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
def get_titles():
44
# Your code here
5-
return None
5+
titles = [] # Empty list
6+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php")
7+
body = response.json()
8+
for post in body['posts']:
9+
titles.append(post['title'])
10+
11+
return titles
612

713

814
print(get_titles())

exercises/10-get-post-tags/app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

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

712

exercises/11-get-attachment-by-id/app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
def get_attachment_by_id(attachment_id):
44
# Your code here
5+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/weird_portfolio.php")
6+
body = response.json()
7+
for post in body['posts']:
8+
for attachment in post['attachments']:
9+
if attachment_id == attachment['id']:
10+
return attachment['title']
511
return None
612

713
print(get_attachment_by_id(137))

exercises/12-post-request/app.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
import requests
22

3-
# Your code here
3+
# Your code here
4+
5+
headers = {'Content-Type': 'application/json'}
6+
body_json = {}
7+
8+
response = requests.post("https://assets.breatheco.de/apis/fake/sample/post.php", data=body_json, headers=headers, verify=False)
9+
print(response.text)

exercises/13-post-request-body/app.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import requests
22

3-
response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php")
3+
headers = {'Content-Type': 'application/json'}
4+
body_json = {
5+
"id": 2323,
6+
"title": "Very big project"
7+
}
8+
9+
response = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php", json=body_json, headers=headers)
410
print(response.text)

0 commit comments

Comments
 (0)