55class JsonRpcException (Exception ):
66 """Base class for exceptions in this module."""
77 pass
8-
9-
8+
9+
1010class JsonRpcParseError (Exception ):
1111 """Raised if Parse JSON-RPC 2.0 string failed.
1212 Params:
1313 rpcError - `JsonRpcError` """
1414 def __init__ (self , rpcError ):
1515 Exception .__init__ (self , rpcError )
1616 self .rpcError = rpcError
17-
18-
17+
18+
1919def defaultJsonEncode (o ):
2020 return o .__dict__
21-
22-
21+
22+
2323class JsonRpcMessage (object ):
2424
2525 @classmethod
@@ -33,7 +33,7 @@ def Notification(cls, method, params = None):
3333 @classmethod
3434 def Success (cls , id , result ):
3535 return JsonRpcSuccessResponse (id , result )
36-
36+
3737 @classmethod
3838 def Error (cls , id , errorobj ):
3939 return JsonRpcErrorResponse (id , errorobj )
@@ -104,14 +104,14 @@ def Parse(cls, jsonstr):
104104
105105 def SubHasId (jsondict ):
106106 return 'id' in jsondict
107-
107+
108108 def SubHasValidId (jsondict ):
109109 '''Checks if Id message field present and has valid value'''
110-
110+
111111 isIdValid = SubHasId (jsondict ) and (not jsondict ['id' ] is None ) \
112112 and (jsondict ['id' ] <> '' )
113113 return isIdValid
114-
114+
115115 def SubHasMethod (jsondict ):
116116 return 'method' in jsondict
117117
@@ -121,22 +121,22 @@ def SubIsMethodCorrect(jsondict):
121121 methodValid = SubHasMethod (jsondict ) and (not jsondict ['method' ] is None ) \
122122 and len (jsondict ['method' ]) > 0
123123 return methodValid
124-
124+
125125 def SubValidateHeader (jsondict ):
126126 '''Parses header and validate values.
127127 Raises `JsonRpcException` in case of error'''
128128 if not 'jsonrpc' in jsondict :
129129 raise JsonRpcException ('Message have no "jsonrpc" field' )
130130 if jsondict ['jsonrpc' ] <> '2.0' :
131131 raise JsonRpcException ('"jsonrpc" field value should be 2.0' )
132-
132+
133133 def SubValidateMethod (jsondict ):
134134 '''Checks if method field exists and has correct value'''
135135 if not SubHasMethod (jsondict ):
136136 raise JsonRpcException ('No "method" field' )
137137 if not SubIsMethodCorrect (jsondict ):
138138 raise JsonRpcException ('Invalid "method" field value' )
139-
139+
140140 def SubValidateErrorObj (errdict ):
141141 '''Checks if Error object has JSON-RPC 2.0 required fields. '''
142142 hasCode = 'code' in errdict
@@ -148,7 +148,7 @@ def SubValidateErrorObj(errdict):
148148 allowedCodes = allowedCodes + [x for x in range (- 32099 ,- 31999 )]
149149 if not errdict ['code' ] in allowedCodes :
150150 raise JsonRpcException ('Invalid JSON-RPC 2.0 Error code' )
151-
151+
152152 def SubParseNotification (jsondict ):
153153 '''Parses jsondict, validates JSON-RPC 2.0 Notification structure and values.
154154 Doesn't check JSON-RPC 2.0 "jsonrpc" and "id".
@@ -160,7 +160,7 @@ def SubParseNotification(jsondict):
160160 params = jsondict .get ('params' , None )
161161 payload = JsonRpcMessage .Notification (jsondict ['method' ], params )
162162 return JsonRpcParsed (JsonRpcParsedType .NOTIFICATION , payload )
163-
163+
164164 def SubParseRequest (jsondict ):
165165 '''Parses jsondict, validates JSON-RPC 2.0 Request structure and values.
166166 Doesn't check JSON-RPC 2.0 "jsonrpc","id", "method".
@@ -170,8 +170,7 @@ def SubParseRequest(jsondict):
170170 params = jsondict .get ('params' , None )
171171 payload = JsonRpcMessage .Request (id , method , params )
172172 return JsonRpcParsed (JsonRpcParsedType .REQUEST , payload )
173-
174-
173+
175174 def SubParseSuccessResponse (jsondict ):
176175 '''Parses jsondict, validates JSON-RPC 2.0 Response Success structure and values.
177176 Doesn't check JSON-RPC 2.0 "jsonrpc","id".
@@ -181,7 +180,7 @@ def SubParseSuccessResponse(jsondict):
181180 '''
182181 payload = JsonRpcMessage .Success (jsondict ['id' ], jsondict ['result' ])
183182 return JsonRpcParsed (JsonRpcParsedType .SUCCESS , payload )
184-
183+
185184 def SubParseErrorResponse (jsondict ):
186185 '''Parses jsondict, validates JSON-RPC 2.0 Response Error structure and values.
187186 Doesn't check JSON-RPC 2.0 "jsonrpc","id".
@@ -198,7 +197,7 @@ def SubParseErrorResponse(jsondict):
198197 return JsonRpcParsed (JsonRpcParsedType .ERROR , payload )
199198 except JsonRpcException as e :
200199 raise JsonRpcParseError (JsonRpcError .InvalidParams (str (e )))
201-
200+
202201 def SubParseJsonRpcObject (jsondict ):
203202 '''Check if jsondict is valid JSON-RPC 2.0 object.
204203 Raises `JsonRpcParseError` if parse/validation failed.
@@ -207,21 +206,21 @@ def SubParseJsonRpcObject(jsondict):
207206 SubValidateHeader (jsondict )
208207 except JsonRpcException as e :
209208 raise JsonRpcParseError (JsonRpcError .InvalidRequest (str (e )))
210-
209+
211210 isNotification = not SubHasValidId (jsondict )
212211 if isNotification :
213212 return SubParseNotification (jsondict )
214-
213+
215214 #else it has Id so it may be: request, success, error message
216215 isRequest = SubIsMethodCorrect (jsondict )
217216 if isRequest :
218217 return SubParseRequest (jsondict )
219-
218+
220219 # no METHOD field so it may be: success, error message
221- isSuccessMsg = 'result' in jsondict
222- if isSuccessMsg :
220+ isSuccessMsg = 'result' in jsondict
221+ if isSuccessMsg :
223222 return SubParseSuccessResponse (jsondict )
224-
223+
225224 isErrorMsg = 'error' in jsondict
226225 if isErrorMsg :
227226 return SubParseErrorResponse (jsondict )
@@ -241,13 +240,13 @@ def SubParseJsonRpcObject(jsondict):
241240 return parsedObjInfo
242241
243242class JsonRpcError (object ):
244- '''Class implements JSON-RPC 2.0 Error Object.
243+ '''Class implements JSON-RPC 2.0 Error Object.
245244 Params:
246245 code -- negative int number JSON-RPC 2.0 error code,
247246 message -- string, error message,
248247 data -- any type, extra info (may be ommited)
249248 '''
250-
249+
251250 def __init__ (self , code , message , data = None ):
252251 self .code = code
253252 self .message = message
0 commit comments