Example #1
0
 async def create_rpc_request(self, method, params=None):
     if params is None:
         params = {}
     data = encode_request(method, id=int(time.time()), params=params)
     LOGGER.info(f'JSON RPC request payload: {data}')
     return await self.client.request(
         'POST',
         '/',
         data=data,
         headers={'Content-Type': 'application/json'})
    def send_request(self, client, method, params=[], timeout=0):
        client_host = client.host

        request_id = self.requests_ids[client_host]
        self.requests_ids[client_host] += 1

        self.waiting_requests[client_host].append(request_id)

        log.debug("Sending request to client {} ({}, {}) id: {}".format(
            client_host, method, params, request_id))
        future = asyncio.run_coroutine_threadsafe(client.ws.send_str(
            encode_request(method, request_id, params)),
                                                  loop=self.loop)
        result = future.result()

        not_found = object()
        response = not_found
        start = time.time()
        while True:
            if client.ws.closed:
                return None

            for _response in self.responses[client_host]:
                _id = _response.get("id")
                if _id == request_id:
                    response = _response
                    break

            if response is not not_found:
                break

            if timeout > 0 and (time.time() - start) > timeout:
                raise Exception("Timeout passed")
                return

            time.sleep(0.1)

        if response is not_found:
            raise Exception("Connection closed")

        self.responses[client_host].remove(response)

        error = response.get("error")
        result = response.get("result")
        if error:
            raise Exception("Error happened: {}".format(error))
        return result
 def send_notification(self, client, method, params=[]):
     asyncio.run_coroutine_threadsafe(client.ws.send_str(
         encode_request(method, params=params)),
                                      loop=self.loop)
def test_datetime_serialization():
    encode_request('abc', params={'p': datetime.now()})
Example #5
0
 async def _send_notification(self, ws, topic, data):
     msg = encode_request(topic, params=[data])
     await ws.send_str(msg)