def decode(cls, msg): try: obj = json.loads(msg, 'utf-8') except: raise JsonRpcParseError() return cls.fromRawObject(obj)
def send(self, message, destination=stomp.SUBSCRIPTION_ID_RESPONSE): resp = json.loads(message) if not isinstance(resp, dict): raise ValueError( 'Provided message %s failed parsing to dictionary' % message) # pylint: disable=no-member response_id = resp.get("id") try: destination = self._req_dest[response_id] del self._req_dest[response_id] except KeyError: # we could have no reply-to or we could send events (no message id) pass try: connections = self._sub_map[destination] except KeyError: self.log.warn("Attempt to reply to unknown destination %s", destination) return for connection in connections: res = stomp.Frame( stomp.Command.MESSAGE, { stomp.Headers.DESTINATION: destination, stomp.Headers.CONTENT_TYPE: "application/json", stomp.Headers.SUBSCRIPTION: connection.id }, message ) # we need to check whether the channel is not closed if not connection.client.is_closed(): connection.client.send_raw(res)
def _handle_internal(self, dispatcher, req_dest, flow_id, request): """ We need to build response dictionary which maps message id with destination. For legacy mode we use known 3.5 destination or for standard mode we use 'reply-to' header. """ try: self._handle_destination(dispatcher, req_dest, json.loads(request)) except Exception: # let json server process issue pass dispatcher.connection.handleMessage(request, flow_id)
def _handleMessage(self, req): transport, message = req try: mobj = json.loads(message) isResponse = self._isResponse(mobj) except: self.log.exception("Problem parsing message from client") if isResponse: self._processIncomingResponse(mobj) else: self._processEvent(mobj)
def _parseMessage(self, obj): client, server_address, context, msg = obj ctx = _JsonRpcServeRequestContext(client, server_address, context) try: rawRequests = json.loads(msg) except: ctx.addResponse(JsonRpcResponse(None, JsonRpcParseError(), None)) ctx.sendReply() return if isinstance(rawRequests, list): # Empty batch request if len(rawRequests) == 0: ctx.addResponse( JsonRpcResponse(None, JsonRpcInvalidRequestError( rawRequests, "request batch is empty"), None)) ctx.sendReply() return else: # From this point on we know it's always a list rawRequests = [rawRequests] # JSON Parsed handling each request requests = [] for rawRequest in rawRequests: try: req = JsonRpcRequest.fromRawObject(rawRequest) requests.append(req) except exception.VdsmException as err: ctx.addResponse(JsonRpcResponse(None, err, None)) except: ctx.addResponse(JsonRpcResponse(None, JsonRpcInternalError(), None)) ctx.setRequests(requests) # No request was built successfully or is only notifications if ctx.counter == 0: ctx.sendReply() for request in requests: self._runRequest(ctx, request)
def _handleMessage(self, message, event_queue=None): try: mobj = json.loads(message) except ValueError: self.log.warning("Received message is not a valid JSON: %r", message) return try: isResponse = self._isResponse(mobj) except TypeError: self.log.warning( "Received batch contains responses and events, ignoring.") return if isResponse: self._processIncomingResponse(mobj) else: self._processEvent(mobj, event_queue)
def test_compat41(self): expected_conf = json.loads(read_data('vm_compat41.json'))[0] vm_params = recovery._recovery_params(expected_conf['vmId'], read_data('vm_compat41.xml'), False) vm_obj = vm.Vm(fake.ClientIF(), vm_params, recover=True) # TODO: ugly hack, but we don't have APIs to do that vm_obj._devices = vm_obj._make_devices() recovered_conf = vm_obj.status(fullStatus=True) self.assert_conf_equal(recovered_conf, expected_conf, filter_vm_conf_keys) self.assert_devices_conf_equal(recovered_conf['devices'], expected_conf['devices'], IGNORED_DEVICE_TYPES)
def test_compat41(self): expected_conf = json.loads( read_data('vm_compat41.json'))[0] vm_params = recovery._recovery_params( expected_conf['vmId'], read_data('vm_compat41.xml'), False) vm_obj = vm.Vm(fake.ClientIF(), vm_params, recover=True) # TODO: ugly hack, but we don't have APIs to do that vm_obj._devices = vm_obj._make_devices() recovered_conf = vm_obj.status(fullStatus=True) self.assert_conf_equal( recovered_conf, expected_conf, filter_vm_conf_keys) self.assert_devices_conf_equal( recovered_conf['devices'], expected_conf['devices'], IGNORED_DEVICE_TYPES)
def _handleMessage(self, message, event_queue=None): try: mobj = json.loads(message) except ValueError: self.log.warning( "Received message is not a valid JSON: %r", message ) return try: isResponse = self._isResponse(mobj) except TypeError: self.log.warning( "Received batch contains responses and events, ignoring." ) return if isResponse: self._processIncomingResponse(mobj) else: self._processEvent(mobj, event_queue)
def test_full_pool(self): def thread_factory(callable): raise exception.ResourceExhausted("Too many tasks", resource="test", current_tasks=0) ctx = FakeContext() request = JsonRpcRequest.decode( '{"jsonrpc":"2.0","method":"Host.stats","params":{},"id":"943"}') server = JsonRpcServer(None, 0, None, threadFactory=thread_factory) server._runRequest(ctx, request) error = ctx.response.toDict().get('error') self.assertEqual(1100, error.get('code')) msg = error.get('message') self.assertTrue(msg.startswith("Not enough resources:")) # not deterministic order in a dict so we need to parse reason = json.loads(msg[22:].replace("'", '"')) self.assertEqual({"reason": "Too many tasks", "resource": "test", "current_tasks": 0}, reason)
def decode(msg): obj = json.loads(msg) return JsonRpcResponse.fromRawObject(obj)
def decode(msg): obj = json.loads(msg, encoding='utf-8') return JsonRpcResponse.fromRawObject(obj)
def decode(msg): obj = json.loads(msg, 'utf-8') return JsonRpcResponse.fromRawObject(obj)