-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.py
180 lines (163 loc) · 6.36 KB
/
api.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import requests
session = requests.Session()
def handle_thing(ip: str, action: str, user: str, passw: str, body=None):
try:
if action == 'update':
endpoint = thing[action]['link']+body['UID']
else:
endpoint = thing[action]['link']
except Exception as e:
print(e)
return 'handle_thing:wrong action used.'
url = ip+endpoint
r = thing[action]['method'](url, u=user, p=passw, data=body)
print(r)
if hasattr(r, 'status_code'):
if str(r.status_code) in thing[action]['response']:
#return 'HTTP Response:' + str(r.status_code) + ' ' + thing[action]['response'][str(r.status_code)]
return r.status_code, thing[action]['response'][str(r.status_code)]
else:
#return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
return r.status_code, str(r.text)
else:
return r
def handle_item(ip: str, action: str, user: str, passw: str, body=None):
try:
if action == 'update':
endpoint = item[action]['link']+body['UID']
else:
endpoint = item[action]['link']
except Exception as e:
print(e)
return 'handle_item:wrong action used.'
url = ip + endpoint
r = item[action]['method'](url, u=user, p=passw, data=body)
print(r)
if hasattr(r, 'status_code'):
if str(r.status_code) in item[action]['response']:
#return 'HTTP Response:' + str(r.status_code) + ' ' + item[action]['response'][str(r.status_code)]
return r.status_code, item[action]['response'][str(r.status_code)]
else:
#return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
return r.status_code, str(r.text)
else:
return r
def handle_link(ip: str, action: str, user: str, passw: str, body=None):
try:
url_add = requests.utils.quote(body['itemName']+'/'+body['channelUID'])
endpoint = links[action]['link']+'/'+url_add
except Exception as e:
print(e)
return 'handle_link:wrong action used or keyerror in data'
url = ip + endpoint
r = links[action]['method'](url, u=user, p=passw, data=body)
print(r)
if hasattr(r, 'status_code'):
if str(r.status_code) in links[action]['response']:
#return 'HTTP Response:' + str(r.status_code) + ' ' + links[action]['response'][str(r.status_code)]
return r.status_code,links[action]['response'][str(r.status_code)]
else:
#return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
return r.status_code, str(r.text)
else:
return r
def http_get(url, u, p, data=None):
try:
headers = {'Content-type': 'application/json'}
session.auth = (u, p)
r = session.get(url, json=data, headers=headers, verify=False, timeout=2)
return r
except requests.exceptions.Timeout as e:
return 'Connection to '+ url +' timed out. Please verify the url and retry, please.'
except requests.exceptions.TooManyRedirects as e:
return 'Bad URL. Please verify the url and retry, please.'
except requests.exceptions.RequestException as e:
# catastrophic error. bail.
return e
def http_post(url, u, p, data=None):
try:
headers = {'Content-type': 'application/json'}
session.auth = (u, p)
r = session.post(url, json=data, headers=headers, verify=False, timeout=2)
return r
except requests.exceptions.Timeout as e:
return 'Connection to '+ url +' timed out. Please verify the url and retry, please.'
except requests.exceptions.TooManyRedirects as e:
return 'Bad URL. Please verify the url and retry, please.'
except requests.exceptions.RequestException as e:
# catastrophic error. bail.
return e
def http_put(url, u, p, data=None):
try:
headers = {'Content-type': 'application/json'}
session.auth = (u, p)
r = session.put(url, json=data, headers=headers, verify=False, timeout=2)
return r
except requests.exceptions.Timeout as e:
return 'Connection to '+ url +' timed out. Please verify the url and retry, please.'
except requests.exceptions.TooManyRedirects as e:
return 'Bad URL. Please verify the url and retry, please.'
except requests.exceptions.RequestException as e:
# catastrophic error. bail.
return e
item = {'list':{
'link':'/rest/items?recursive=false',
'type':http_get,
'response':{
'200':'OK'}
},
'create':{
'link': '/rest/items',
'method': http_put,
'response':{
'200': 'OK',
'400': 'Payload is invalid'}
},
'update':{
'link':'/rest/items',
'method':http_put,
'response':{
'200':'OK',
'400':'Payload is invalid'}
}
}
thing = {'list':{
'link':'/rest/things',
'method':http_get,
'response':{
'200':'OK'}
},
'create':{
'link':'/rest/things',
'method':http_post,
'response':{
'201':'Thing created',
'400':'A uid must be provided, if no binding can create a thing of this type',
'409':'A thing with the same uid already exists',
'500':'UID must have at least 3 segments'}
},
'update':{
'link':'/rest/things/',
'method':http_put,
'response':{
'200':'OK',
'404':'Thing not found',
'405':'Method not allowed',
'409':'Thing could not be updated as it is not editable'}
}
}
links = {'list':{
'link':'/rest/links',
'type':http_get,
'response':{
'200':'OK'}
},
'create':{
'link': '/rest/links',
'method': http_put,
'response':{
'200': 'OK',
'400': 'Content does not match the path',
'405': 'Link is not editable'}
}
}