This repository was archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.py
More file actions
129 lines (98 loc) · 2.96 KB
/
api_test.py
File metadata and controls
129 lines (98 loc) · 2.96 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
119
120
121
122
123
124
125
126
127
128
129
import requests
from colorama import init, Fore, Back, Style
from pprint import pprint
# Headers for all requests in JSON format
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
# Terminal colored text setup using colorama
init()
def cprint(text):
print(Fore.CYAN + text + Fore.RESET)
def mprint(text):
print(Fore.MAGENTA + text + Fore.RESET)
# [=ENDPOINT REQUESTS=]
# GET /fridge
def get_fridges():
url = "http://localhost:5000/fridge"
r = requests.get(url, headers=headers)
return r.json()
# Print endpoint and response
cprint("GET /fridge")
pprint(get_fridges())
# GET /fridge/<name>
def get_fridge(name):
url = f"http://localhost:5000/fridge/{name}"
r = requests.get(url, headers=headers)
return r.json()
name = "Samsung Smart Fridge"
cprint("GET /fridge/<name>")
mprint(f"name: {name}")
pprint(get_fridge(name))
# GET /fridge/<name>/food
def get_fridge_food_list(name):
url = "http://localhost:5000/fridge/" + name + "/food"
r = requests.get(url, headers=headers)
return r.json()
name = "Samsung Smart Fridge"
cprint("GET /fridge/<name>/food")
mprint(f"name: {name}")
pprint(get_fridge_food_list(name))
# POST /fridge/<name>/add
def add_food_to_fridge(name, food):
url = "http://localhost:5000/fridge/" + name + "/add"
data = {
"name": food["name"],
"quantity": food["quantity"],
"color": food["color"],
"shape": food["shape"],
"group": food["group"]
}
r = requests.post(url, headers=headers, json=data)
return r.json()
name = "Samsung Smart Fridge"
data = {
"name": "orange",
"quantity": 1,
"color": "orange",
"shape": "round",
"group": "fruit"
}
cprint("POST /fridge/<name>/add")
mprint(f"name: {name}")
mprint(f"data: {data}")
pprint(add_food_to_fridge(name, data))
# PUT /fridge/<name>/eat
def eat_food_from_fridge(name, fname):
url = f"http://localhost:5000/fridge/{name}/eat"
r = requests.put(url, headers=headers, json={"fname": fname})
return r.json()
name = "Samsung Smart Fridge"
fname = "banana"
cprint("PUT /fridge/<name>/eat")
mprint(f"name: {name}")
mprint(f"fname: {fname}")
pprint(eat_food_from_fridge(name, fname))
# GET /fridge/<name>/food/<fname>/quantity
def get_food_quantity(name, fname):
url = f"http://localhost:5000/fridge/{name}/food/{fname}/quantity"
r = requests.get(url, headers=headers)
return r.json()
name = "Samsung Smart Fridge"
fname = "banana"
cprint("GET /fridge/<name>/food/<fname>/quantity")
mprint(f"name: {name}")
mprint(f"fname: {fname}")
pprint(get_food_quantity(name, fname))
# GET /fridge/<name>/food/<fname>/shape
def get_food_shape(name, fname):
url = f"http://localhost:5000/fridge/{name}/food/{fname}/shape"
r = requests.get(url, headers=headers)
return r.json()
name = "Samsung Smart Fridge"
fname = "banana"
cprint("GET /fridge/<name>/food/<fname>/shape")
mprint(f"name: {name}")
mprint(f"fname: {fname}")
pprint(get_food_shape(name, fname))