Read JSON RPC message from buffer. Exceptions raised: ValueError if the body-content can not be serialized to a JSON object.
(self)
| 259 | self.needs_more_data = True |
| 260 | |
| 261 | def read_response(self): |
| 262 | """ |
| 263 | Read JSON RPC message from buffer. |
| 264 | Exceptions raised: |
| 265 | ValueError |
| 266 | if the body-content can not be serialized to a JSON object. |
| 267 | """ |
| 268 | # Using a mutable list to hold the value since a immutable string |
| 269 | # passed by reference won't change the value. |
| 270 | content = [''] |
| 271 | try: |
| 272 | while (not self.needs_more_data or self.read_next_chunk()): |
| 273 | # We should have all the data we need to form a message in the buffer. |
| 274 | # If we need more data to form the next message, this flag will |
| 275 | # be reset by a attempt to form a header or content. |
| 276 | self.needs_more_data = False |
| 277 | # If we can't read a header, read the next chunk. |
| 278 | if self.read_state is ReadState.Header and not self.try_read_headers(): |
| 279 | self.needs_more_data = True |
| 280 | continue |
| 281 | # If we read the header, try the content. If that fails, read |
| 282 | # the next chunk. |
| 283 | if self.read_state is ReadState.Content and not self.try_read_content( |
| 284 | content): |
| 285 | self.needs_more_data = True |
| 286 | continue |
| 287 | # We have the content |
| 288 | break |
| 289 | |
| 290 | # Resize buffer and remove bytes we have read |
| 291 | self.trim_buffer_and_resize(self.read_offset) |
| 292 | return json.loads(content[0]) |
| 293 | except ValueError as ex: |
| 294 | # response has invalid json object. |
| 295 | logger.debug(u'JSON RPC Reader on read_response() encountered exception: %s', ex) |
| 296 | raise |
| 297 | |
| 298 | def read_next_chunk(self): |
| 299 | """ |