-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-tool.py
More file actions
117 lines (103 loc) · 3.39 KB
/
Copy pathpython-tool.py
File metadata and controls
117 lines (103 loc) · 3.39 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
import requests
import sys
from uuid import getnode as get_mac
HOST = "http://localhost:8080"
HEADERS = {'Content-Type': 'application/json'}
PRODUCT_NAME = "productname"
# macアドレスを取得(一意ではない可能性があるらしい)
PRODUCT_NUMBER = str(get_mac())
def create_product_request():
url = f"{HOST}/products/create"
payload = {"name": PRODUCT_NAME, "number": PRODUCT_NUMBER}
response = requests.post(url, headers=HEADERS, json=payload)
print(f"Status code: {response.status_code}")
try:
json_data = response.json()
print(f"Error code: {json_data['code']}")
print(f"Error message: {json_data['message']}")
except ValueError:
print("Product is created")
def create_user_request():
url = f"{HOST}/user/create"
email = input('Email: ')
password = input('Password: ')
payload = {"email": email, "password": password}
response = requests.post(url, headers=HEADERS, json=payload)
print(f"Status code: {response.status_code}")
try:
json_data = response.json()
print(f"Error code: {json_data['code']}")
print(f"Error message: {json_data['message']}")
except ValueError:
print("User is created")
def product_user_request():
url = f"{HOST}/user/products"
email = input('Email: ')
password = input('Password: ')
payload = {
"email": email,
"password": password,
"name": PRODUCT_NAME,
"number": PRODUCT_NUMBER
}
response = requests.post(url, headers=HEADERS, json=payload)
print(f"Status code: {response.status_code}")
try:
json_data = response.json()
print(f"Error code: {json_data['code']}")
print(f"Error message: {json_data['message']}")
except ValueError:
print("User and product are linked")
def auth_request():
url = f"{HOST}/user/auth"
email = input('Email: ')
password = input('Password: ')
payload = {
"email": email,
"password": password,
"name": PRODUCT_NAME,
"number": PRODUCT_NUMBER
}
response = requests.post(url, headers=HEADERS, json=payload)
print(f"Status code: {response.status_code}")
try:
json_data = response.json()
if 'auth' in json_data:
print("Auth is completed")
return True
except ValueError:
pass
# エラー処理を行う
if json_data['code']==4001:
print('User and product are not linked')
# お好みで
print('do you link user and product?')
product_user_request()
elif json_data['code']==4002:
print('user is not exist')
# お好みで
# print('do you create user?')
# create_user_request()
elif json_data['code']==4003:
print('product is not exist')
# お好みで
# print('do you create product?')
# create_product_request()
elif json_data['code']==6002:
print('password is incorrect')
elif json_data['code']==6003:
print('email is incorrect')
print(f"error code: {json_data['code']}")
print(f"error message: {json_data['message']}")
return False
# メイン処理
def main():
print("Hello World")
if __name__ == "__main__":
# create_product_request()
# create_user_request()
# product_user_request()
auth_bool = auth_request()
if auth_bool==False:
sys.exit( )
main()