-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonplaceholder_client.py
More file actions
73 lines (60 loc) · 2.08 KB
/
jsonplaceholder_client.py
File metadata and controls
73 lines (60 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import requests
class JSONPlaceholderAPI:
BASE_URL = 'https://jsonplaceholder.typicode.com'
def __init__(self):
self.session = requests.Session()
def get_data(self, endpoint):
url = f'{self.BASE_URL}/{endpoint}'
response = self.session.get(url)
return response.json()
def display_users(self):
data = self.get_data('users')
for item in data:
print('----------')
print('Id:', item['id'])
print('Name:', item['name'])
print('Email:', item['email'])
def display_comments(self):
data = self.get_data('comments')
for item in data:
print('----------')
print('Id: ', item['id'])
print('Name: ', item['name'])
print('Email: ', item['email'])
print('Body', item['body'])
def display_albums(self):
data = self.get_data('albums')
for item in data:
print('-----------')
print('Id', item['id'])
print('Title', item['title'])
def display_photos(self):
data = self.get_data('photos')
for item in data:
print('----------')
print('Id: ', item['id'])
print('Title: ', item['title'])
print('Url: ', item['url'])
print('Thumbnail url: ', item['thumbnailUrl'])
def display_todos(self):
data = self.get_data('todos')
for item in data:
print('----------')
print('Id: ', item['id'])
print('Title: ', item['title'])
print('Completed: ', item['completed'])
api = JSONPlaceholderAPI()
print('Chose and endpoint: 1: users | 2: comments | 3: albums | 4: photos | 5: todos')
input_endpoint = input('Enter an endpoint: ')
if input_endpoint == '1':
api.display_users()
elif input_endpoint == '2':
api.display_comments()
elif input_endpoint == '3':
api.display_albums()
elif input_endpoint == '4':
api.display_photos()
elif input_endpoint == '5':
api.display_todos()
else:
print('Invalid input, Please choose a valid endpoint')