-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
118 lines (82 loc) · 3.03 KB
/
Copy pathapp.py
File metadata and controls
118 lines (82 loc) · 3.03 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests
# GET HTTP Request
# api_url = 'https://jsonplaceholder.typicode.com/todos'
# response = requests.get(api_url)
# status = response.status_code
# print(response.headers['Content-Type'])
# print(response.json())
class RestAPIExample:
def __init__(self,url):
self.url = url
def get_response(self):
response = requests.get(self.url)
return response
# Helper methods
def get_records(self,response):
return response.json()
def get_headers(self,response):
return response.headers['Content-Type']
def get_status_code(self,response):
return response.status_code
def post_record(self,data):
response = requests.post(self.url,json=data)
print(self.get_records(response))
print(self.get_headers(response))
print(self.get_status_code(response))
print()
def full_update_record(self,index,data):
api_url = self.url + "/" + str(index)
response = requests.get(api_url)
print(self.get_records(response))
print(self.get_headers(response))
print(self.get_status_code(response))
print()
response = requests.put(api_url, json=data)
print('Full Updated Existing Resource')
print(self.get_records(response))
print(self.get_headers(response))
print(self.get_status_code(response))
print()
# response.status_code
def partial_update_record(self,index,data):
api_url = self.url + "/" + str(index)
response = requests.get(api_url)
print(self.get_records(response))
print(self.get_headers(response))
print(self.get_status_code(response))
print()
response = requests.patch(api_url,json=data)
print('Partial Update Existing Resource')
print(self.get_records(response))
print(self.get_headers(response))
print(self.get_status_code(response))
print()
def remove_record(self,index):
api_url = self.url + "/" + str(index)
response = requests.delete(api_url)
print(self.get_records(response))
print(self.get_headers(response))
print(self.get_status_code(response))
print()
# Edge Cases:
if __name__ == "__main__":
api_url = 'https://jsonplaceholder.typicode.com/todos'
myapp = RestAPIExample(api_url)
get_response = myapp.get_response()
# Headers
print('Request Header: ',myapp.get_headers(get_response))
print('Status Code: ',myapp.get_status_code(get_response))
print()
# print('GET Output: ',myapp.get_records(get_response))
print('POST Output: ')
todo = {'userId':1,"title":"Buy Milk","completed":False}
myapp.post_record(todo)
print('PUT Output: ')
todo = {'userId': 1, "title": "Wash Car", "completed": True}
myapp.full_update_record(index=10,data=todo)
print('PATCH Output: ')
todo = {'title':'Mow lawn'}
myapp.partial_update_record(10,todo)
print('DELETE Output: ')
myapp.remove_record(10)
# print(get_response)