-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestAPI.py
More file actions
96 lines (84 loc) · 2.79 KB
/
RequestAPI.py
File metadata and controls
96 lines (84 loc) · 2.79 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
import requests
import random
import json
import string
#base url:
base_url = "https://gorest.co.in"
#Auth token:
auth_token = "Bearer <TokenID>"
#get random email id:
def generate_random_email():
domain = "automation.com"
email_length = 10
random_string = ''.join(random.choice(string.ascii_lowercase) for _ in range(email_length))
email = random_string + "@" + domain
return email
#GET Request
def get_request():
url = base_url + "/public/v2/users"
print("get url: " + url)
headers = {"Authorization": auth_token}
response = requests.get(url, headers=headers)
assert response.status_code == 200
json_data = response.json()
json_str = json.dumps(json_data, indent=4)
print("json GET response body: ", json_str)
print(".......GET USER IS DONE.......")
print(".......=====================.......")
#POST Request
def post_request():
url = base_url + "/public/v2/users"
print("post url: " + url)
headers = {"Authorization": auth_token}
data = {
"name": "Naveen Automation",
"email": generate_random_email(),
"gender": "male",
"status": "active"
}
response = requests.post(url, json=data, headers=headers)
json_data = response.json()
json_str = json.dumps(json_data, indent=4)
print("json POST response body: ", json_str)
user_id = json_data["id"]
print("user id ===>", user_id)
assert response.status_code == 201
assert "name" in json_data
assert json_data["name"] == "Naveen Automation"
print(".......POST/Create USER IS DONE.......")
print(".......=====================.......")
return user_id
#PUT Request
def put_request(user_id):
url = base_url + f"/public/v2/users/{user_id}"
print("PUT url: " + url)
headers = {"Authorization": auth_token}
data = {
"name": "Naveen Automation Labs",
"email": generate_random_email(),
"gender": "male",
"status": "inactive"
}
response = requests.put(url, json=data, headers=headers)
assert response.status_code == 200
json_data = response.json()
json_str = json.dumps(json_data, indent=4)
print("json PUT response body: ", json_str)
assert json_data["id"] == user_id
assert json_data["name"] == "Naveen Automation Labs"
print(".......PUT/Update USER IS DONE.......")
print(".......=====================.......")
#DELETE Request
def delete_request(user_id):
url = base_url + f"/public/v2/users/{user_id}"
print("DELETE url: " + url)
headers = {"Authorization": auth_token}
response = requests.delete(url, headers=headers)
assert response.status_code == 204
print(".......DELETE USER IS DONE.......")
print(".......=====================.......")
#call
get_request()
user_id = post_request()
put_request(user_id)
delete_request(user_id)