-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuapiManager.py
More file actions
232 lines (197 loc) · 6.88 KB
/
uapiManager.py
File metadata and controls
232 lines (197 loc) · 6.88 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import re
class UAPIManager:
APICodePattern = (
'^[a-z]{1}[a-zA-Z0-9]*[a-z]{1}([\.]{1}[a-z]{1}[a-zA-Z0-9]*[a-z]{1})*$'
)
maxArgs = 20
apis = dict()
class Status:
SUCCESS = 'success'
FAILURE = 'failure'
ERROR = 'error'
class Reason:
APICODE_REQUIRED = 'apicode required'
INVALID_PARAMETERS = 'invalid parameters'
UNDEFINED = 'unknown error occurred'
API_UNDEFINED = 'invalid apicode'
INTERNAL_ERROR = 'internal api error'
# define your reasons here ...
MY_REASON = 'my reason'
def registerAPI (apicode, api_function=None, args=None, update=False):
if ((type(apicode).__name__ != 'str')
or ((not update) and (not api_function))
or (update and (args == None) and (not api_function))
or (api_function and (not callable(api_function)))
):
return None
if ((not update) and (args == None)):
args = 0
# Please bear with these if-elif-else statements.
# I know these are in-efficient, but for quick rollout of this app,
# I couldn't find time to change these up.
if ((not update)
and ((type(args).__name__ != 'int')
or (args < 0) or (args > UAPIManager.maxArgs)
)
):
return None
elif (update
and (args)
and ((type(args).__name__ != 'int')
or (args < 0) or (args > UAPIManager.maxArgs)
)
):
return None
elif (update
and (args == None)
and api_function
):
pass
elif (update
and (args or (args == 0))
and (not api_function)
):
pass
elif (update
and (args or (args == 0))
and api_function
):
pass
elif ((not update)
and (args or (args == 0))
and api_function
):
pass
else:
return None
if (not bool(re.fullmatch(UAPIManager.APICodePattern, apicode,))):
return None
apistatus = UAPIManager.getAvailableAPIs(apicode)
if ((not apistatus)
and ((not api_function)
or (api_function and (not callable(api_function)))
)
):
return False
if (apistatus and (not update)):
return False
elif (not apistatus):
args = args or 0
UAPIManager.apis[apicode] = [api_function, args]
return True
elif (apistatus and update):
if (api_function and callable(api_function)):
UAPIManager.apis[apicode][0] = api_function
if (args or (args == 0)):
UAPIManager.apis[apicode][1] = args
return True
else:
return False
def unregisterAPI (apicode):
if (type(apicode).__name__ != 'str'):
return None
apistatus = UAPIManager.getAvailableAPIs(apicode)
if (not apistatus):
return False
elif (apistatus):
UAPIManager.apis.pop(apicode)
return True
else:
return False
def getAvailableAPIs (apicode=None):
if (apicode and (type(apicode).__name__ == 'str')):
return (apicode in UAPIManager.apis.keys())
elif (apicode):
return None
else:
return tuple(set(UAPIManager.apis.keys()))
def getAPI (apicode, getargs=False):
if (apicode
and (type(apicode).__name__ == 'str')
and (UAPIManager.getAvailableAPIs(apicode))
):
if (getargs):
return (UAPIManager.apis.get(apicode)[1] or 0)
else:
return (UAPIManager.apis.get(apicode)[0])
else:
return None
def createResponse (status=None, data=None, reason=None,
maxParameters=None, **kwargs,
):
if (not status):
if (data):
status = UAPIManager.Status.SUCCESS
else:
status = UAPIManager.Status.FAILURE
if (not reason):
reason = UAPIManager.Status.Reason.INTERNAL_ERROR
elif (status):
if (type(status).__name__ != 'str'):
status = UAPIManager.Status.SUCCESS
else:
if ((status == UAPIManager.Status.ERROR) and (not reason)):
reason = UAPIManager.Status.Reason.UNDEFINED
if (not data):
data = dict(kwargs)
elif (data):
if (type(data).__name__ != 'dict'):
odata = data
data = dict(kwargs)
data['responseValue'] = odata
elif (type(data).__name__ == 'dict'):
data.update(dict(kwargs))
data = data or None
response = {
'status': status,
}
if (reason and (type(reason).__name__ == 'str')):
response['reason'] = reason
if (maxParameters and (type(maxParameters).__name__ == 'int')):
response['maxParameters'] = maxParameters
if (data):
response['data'] = data
return response
def fetch (request=None, apicode=None, data=None, path=[],):
path = path or []
if (apicode):
if (UAPIManager.getAvailableAPIs(apicode)):
args = UAPIManager.getAPI(apicode, getargs=True)
if (len(path) > args):
response = UAPIManager.createResponse(
status=UAPIManager.Status.ERROR,
reason=UAPIManager.Status.Reason.INVALID_PARAMETERS,
maxParameters=args,
)
elif (len(path) <= args):
path = path[:args]
try:
response = UAPIManager.getAPI(
apicode, getargs=False,
)(request, data, *path,)
except:
response = UAPIManager.createResponse(
status=UAPIManager.Status.ERROR,
reason=UAPIManager.Status.Reason.INTERNAL_ERROR,
)
if (not response):
response = UAPIManager.createResponse(
status=False,
reason=UAPIManager.Status.Reason.UNDEFINED,
)
else:
response = UAPIManager.createResponse(
status=UAPIManager.Status.ERROR,
reason=UAPIManager.Status.Reason.UNDEFINED,
)
else:
response = UAPIManager.createResponse(
status=UAPIManager.Status.ERROR,
reason=UAPIManager.Status.Reason.API_UNDEFINED,
)
else:
response = UAPIManager.createResponse(
status=UAPIManager.Status.ERROR,
reason=UAPIManager.Status.Reason.APICODE_REQUIRED,
)
return response