@@ -70,8 +70,8 @@ class JsonRpcParsedType(object):
7070 REQUEST = 'REQUEST'
7171 NOTIFICATION = 'NOTIFICATION'
7272 SUCCESS = 'SUCCESS'
73- ERROR = 'ERROR'
74-
73+ ERROR = 'ERROR'
74+
7575
7676class JsonRpcParsed (object ):
7777 '''Presents a json string parse result: parsedType and payload'''
@@ -80,93 +80,96 @@ def __init__(self, parsedType, payload):
8080 self .payload = payload
8181
8282 @classmethod
83- def Parse (jsonstr ):
83+ def Parse (cls , jsonstr ):
8484 '''Parses json formatted string.
8585 Returns JsonRpcParsed object containing Parse results.'''
8686
87- def SubCheckHeader (jsonobj ):
87+ def SubCheckHeader (jsondict ):
8888 '''Parses header and validate values.
8989 Returns True or raises JsonRpcException in case of error'''
90- if not hasattr ( jsonobj , 'jsonrpc' ) :
90+ if not 'jsonrpc' in jsondict :
9191 raise JsonRpcException ('Message have no "jsonrpc" field' )
92- if jsonobj . jsonrpc <> '2.0' :
92+ if jsondict [ ' jsonrpc' ] <> '2.0' :
9393 raise JsonRpcException ('"jsonrpc" field value should be 2.0' )
9494 return True
9595
96- def SubHasId (jsonobj ):
97- return hasattr ( jsonobj , 'id' )
96+ def SubHasId (jsondict ):
97+ return 'id' in jsondict
9898
99- def SubHasValidId (jsonobj ):
99+ def SubHasValidId (jsondict ):
100100 '''Checks if Id message field present and has valid value'''
101- isIdValid = SubHasId (jsonobj ) and (not jsonobj .id is None ) \
102- and (jsonobj .id <> '' )
101+
102+ isIdValid = SubHasId (jsondict ) and (not jsondict ['id' ] is None ) \
103+ and (jsondict ['id' ] <> '' )
103104 return isIdValid
104105
105- def SubHasMethod (jsonobj ):
106- return hasattr ( jsonobj , 'method' )
106+ def SubHasMethod (jsondict ):
107+ return 'method' in jsondict
107108
108- def SubIsMethodValid (jsonobj ):
109+ def SubIsMethodValid (jsondict ):
109110 '''Checks if "method" message field has valid value.
110111 Returns boolean'''
111- methodValid = SubHasMethod and (not jsonobj . method is None ) \
112- and len (jsonobj . method ) > 0
112+ methodValid = SubHasMethod ( jsondict ) and (not jsondict [ ' method' ] is None ) \
113+ and len (jsondict [ ' method' ] ) > 0
113114 return methodValid
114115
115- def SubValidateMethod (jsonobj ):
116- if not SubHasMethod (jsonobj ):
116+ def SubValidateMethod (jsondict ):
117+ if not SubHasMethod (jsondict ):
117118 raise JsonRpcException ('No "method" field' )
118- if not SubIsMethodValid (jsonobj ):
119+ if not SubIsMethodValid (jsondict ):
119120 raise JsonRpcException ('Invalid "method" field value' )
120121
121- def SubParseJsonRpcObject (jsonobj ):
122- '''Check if jsonobj is valid JSON-RPC 2.0 object.
122+ def SubParseJsonRpcObject (jsondict ):
123+ '''Check if jsondict is valid JSON-RPC 2.0 object.
123124 Returns JsonRpcParsed object containing Parse results.'''
124125 try :
125- SubCheckHeader (jsonobj )
126+ SubCheckHeader (jsondict )
126127 except JsonRpcException as e :
127128 return JsonRpcParsed (JsonRpcParsedType .INVALID ,
128- JsonRpcError .InvalidRequest (e ))
129+ JsonRpcError .InvalidRequest (str ( e ) ))
129130
130- isNotification = not SubHasValidId (jsonobj )
131+ isNotification = not SubHasValidId (jsondict )
131132 if isNotification :
132133 try :
133- SubValidateMethod (jsonobj )
134- data = JsonRpcMessage .Notification (jsonobj .method , jsonobj .params )
134+ SubValidateMethod (jsondict )
135+ data = JsonRpcMessage .Notification (jsondict ['method' ], \
136+ jsondict ['params' ])
135137 return JsonRpcParsed (JsonRpcParsedType .NOTIFICATION , data )
136138 except JsonRpcException as e :
137139 return JsonRpcParsed (JsonRpcParsedType .INVALID ,
138- JsonRpcError .InvalidRequest (e ))
140+ JsonRpcError .InvalidRequest (str ( e ) ))
139141 #else it has Id so it may be: request, success, error message
140- isRequest = SubIsMethodValid (jsonobj )
142+ isRequest = SubIsMethodValid (jsondict )
141143 if isRequest :
142- data = JsonRpcMessage .Request (jsonobj . id , jsonobj . method ,
143- jsonobj . params )
144+ data = JsonRpcMessage .Request (jsondict [ 'id' ], \
145+ jsondict [ 'method' ], jsondict [ ' params' ] )
144146 return JsonRpcParsed (JsonRpcParsedType .REQUEST , data )
145147 # no METHOD field so it may be: success, error message
146148
147- isResultMsg = hasattr ( jsonobj , 'result' )
148- if isResultMsg :
149- data = JsonRpcMessage .Success (jsonobj . id , jsonobj . result )
149+ isSuccessMsg = 'result' in jsondict
150+ if isSuccessMsg :
151+ data = JsonRpcMessage .Success (jsondict [ 'id' ], jsondict [ ' result' ])
150152 return JsonRpcParsed (JsonRpcParsedType .SUCCESS , data )
151153
152- isErrorMsg = hasattr ( jsonobj , 'error' )
154+ isErrorMsg = 'error' in jsondict
153155 if isErrorMsg :
154- err = jsonobj .error
155- errorobj = JsonRpcErrorObject (err .code , err .message , err .data )
156- data = JsonRpcMessage .Error (jsonobj .id , errorobj )
156+ err = jsondict ['error' ]
157+ errorobj = JsonRpcError (err ['code' ], err ['message' ], \
158+ err ['data' ])
159+ data = JsonRpcMessage .Error (jsondict ['id' ], errorobj )
157160 return JsonRpcParsed (JsonRpcParsedType .ERROR , data )
158161 # no result, no error - id only
159162 return JsonRpcParsed (JsonRpcParsedType .INVALID ,
160- JsonRpcError .InvalidRequest ())
163+ JsonRpcError .InvalidRequest ('No reqired fields' ))
161164 try :
162- jsonObj = json .loads (jsonstr )
163- return SubParseJsonRpcObject (jsonObj )
165+ jsondict = json .loads (jsonstr )
166+ return SubParseJsonRpcObject (jsondict )
164167 except ValueError as e :
165- errorobj = JsonRpcErrorObject (JsonRpcError .ParseError (jsonstr ))
166- return JsonRpcParsed (JsonRpcParsedType .INVALID , errorobj )
167- except Exception as e :
168- errorobj = JsonRpcErrorObject (JsonRpcError .InternalError (e ))
168+ errorobj = JsonRpcErrorObject (None , JsonRpcError .ParseError (jsonstr ))
169169 return JsonRpcParsed (JsonRpcParsedType .INVALID , errorobj )
170+ '''except Exception as e:
171+ errorobj = JsonRpcErrorObject(None, JsonRpcError.InternalError(str(e)))
172+ return JsonRpcParsed(JsonRpcParsedType.INVALID, errorobj) '''
170173
171174
172175class JsonRpcError (object ):
@@ -182,25 +185,25 @@ def Error(cls, code, message, data = None):
182185 return JsonRpcError (code , message , data )
183186
184187 @classmethod
185- def ParseError (cls , code , message , data = None ):
188+ def ParseError (cls , data = None ):
186189 code = - 32700
187190 message = 'Parse Error'
188191 return JsonRpcError (code , message , data )
189192
190193 @classmethod
191- def InvalidRequest (cls , code , message , data = None ):
194+ def InvalidRequest (cls , data = None ):
192195 code = - 32600
193196 message = 'Invalid Request'
194197 return JsonRpcError (code , message , data )
195198
196199 @classmethod
197- def MethodNotFound (cls , code , message , data = None ):
200+ def MethodNotFound (cls , data = None ):
198201 code = - 32601
199202 message = 'Method Not Found'
200203 return JsonRpcError (code , message , data )
201204
202205 @classmethod
203- def InvalidParams (cls , code , message , data = None ):
206+ def InvalidParams (cls , data = None ):
204207 code = - 32602
205208 message = 'Invalid Params'
206209 return JsonRpcError (code , message , data )
0 commit comments