-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_module_connector.py
123 lines (74 loc) · 3.41 KB
/
python_module_connector.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
import json
ERROR_DOUBLE_DATA = 'message and reply cannot be dict or list at the same time'
class Moduleconnector:
def __init__(self, objectDefaultData={}):
self.success = False
self.message = "module_connector"
self.code = 500
self.response = []
if isinstance(objectDefaultData, str):
self.message = objectDefaultData;
def for_list():
self.response = []
def for_dict():
self.response = {}
def writeMessageAndReponse(self, response=[], message=""):
# INVERSE DATA CASE .ok('success_load', {data:1})
if isinstance(response, str) and isinstance(message, (list, dict)):
self.response = message
self.message = response
# ONLY TEXT ONE CASE .ok('success_load')
elif isinstance(response, str) and isinstance(message, str):
self.response = []
self.message = response
# NORMAL DATA CASE .ok({data:1}, 'success_load') or ONLY OBJECT CASE
elif isinstance(response, (list, dict)) and isinstance(message, str):
self.response = response
self.message = message
# IF TWO OBJECT CASE ERROR
elif isinstance(response, (list, dict)) and isinstance(message, (list, dict)):
self.response = []
self.message = ERROR_DOUBLE_DATA
return self
def successFunctions(self, response, message, code):
self.code = code
self.success = True
return self.writeMessageAndReponse(response, message)
def failedFunctions(self, response, message, code):
self.code = code;
self.success = False;
return self.writeMessageAndReponse(response, message)
def getHTTPMessage(self):
sw = {
200: 'ok',
201: 'created',
202: 'accepted',
203: 'nonAuthoritative',
204: 'noContent',
400: 'badRequest',
401: 'unauthorized',
403: 'forbidden',
404: 'notFound',
409: 'conflict',
}
return sw.get(self.code)
def ok(self, response=[], message='ok'):
return self.successFunctions(response, message, 200)
def created(self, response=[], message='created'):
return self.successFunctions(response, message, 201)
def accepted(self, response=[], message='accepted'):
return self.successFunctions(response, message, 202)
def nonAuthoritative(self, response=[], message='nonAuthoritative'):
return self.successFunctions(response, message, 203)
def noContent(self, response=[], message='noContent'):
return self.successFunctions(response, message, 204)
def badRequest(self, response=[], message='badRequest'):
return self.failedFunctions(response, message, 400)
def unauthorized(self, response=[], message='unauthorized'):
return self.failedFunctions(response, message, 401)
def forbidden(self, response=[], message='forbidden'):
return self.failedFunctions(response, message, 403)
def notFound(self, response=[], message='notFound'):
return self.failedFunctions(response, message, 404)
def conflict(self, response=[], message='conflict'):
return self.failedFunctions(response, message, 409)