async def fetch_transaction(request): id = request.params['id'] client = AccountClient() try: return await client.fetch_transaction(id) except KeyNotFound: raise KeyNotFound(f'Transaction with id "{id}" not found')
def _handle_response(self, msg_type, resp_proto, req): self._stream.wait_for_ready() future = self._stream.send(message_type=msg_type, content=req.SerializeToString()) resp = resp_proto() try: resp.ParseFromString(future.result().content) except (DecodeError, AttributeError): raise ClientException( 'Failed to parse "content" string from validator') except ValidatorConnectionError as vce: LOGGER.error('Error: %s' % vce) raise ClientException( 'Failed with ZMQ interaction: {0}'.format(vce)) data = message_to_dict(resp) # NOTE: Not all protos have this status with suppress(AttributeError): if resp.status == resp_proto.NO_RESOURCE: raise KeyNotFound("404") if resp.status != resp_proto.OK: raise ClientException("Error: %s" % data) return data
def _send_request(self, suffix, data=None, content_type=None): if self.url.startswith("http://"): url = "{}/{}".format(self.url, suffix) else: url = "http://{}/{}".format(self.url, suffix) headers = {} if content_type is not None: headers['Content-Type'] = content_type try: if data is not None: result = requests.post(url, headers=headers, data=data) else: result = requests.get(url, headers=headers) if result.status_code == 404: raise KeyNotFound("404") elif not result.ok: raise ClientException("Error {}: {}".format( result.status_code, result.reason)) except requests.ConnectionError as err: raise ClientException( 'Failed to connect to REST API: {}'.format(err)) return result.text
async def fetch_block(request): id = request.params['id'] client = BlockInfoClient() try: return await client.fetch_block(id) except KeyNotFound: raise KeyNotFound(f'Block with id "{id}" not found')
def _handle_response(self, msg_type, resp_proto, req): self._stream.wait_for_ready() future = self._stream.send(message_type=msg_type, content=req.SerializeToString()) resp = resp_proto() try: resp.ParseFromString(future.result(ZMQ_CONNECTION_TIMEOUT).content) except (DecodeError, AttributeError): raise ClientException( 'Failed to parse "content" string from validator') except ValidatorConnectionError as vce: raise ClientException( 'Failed with ZMQ interaction: {0}'.format(vce)) except (asyncio.TimeoutError, FutureTimeoutError): raise ClientException('Validator connection timeout') except Exception as e: LOGGER.exception(e) raise ClientException('Unexpected validator error') data = message_to_dict(resp) with suppress(AttributeError): LOGGER.debug(f'The response parsed data: {data}') if resp.status == resp_proto.NO_RESOURCE: raise KeyNotFound('Resource not found') elif resp.status == resp_proto.NOT_READY: raise ValidatorNotReadyException('Validator is not ready yet') elif resp.status != resp_proto.OK: raise ClientException('Error occured') return data
def _text_request(self, suffix, data=None, content_type=None): url = f"{self.url}/{suffix}" if not url.startswith("http://"): url = f"http://{url}" headers = {} if content_type is not None: headers['Content-Type'] = content_type try: if data is not None: with suppress(AttributeError): data = data.SerializeToString() result = requests.post(url, headers=headers, data=data) else: result = requests.get(url, headers=headers) if result.status_code == 404: raise KeyNotFound("404") elif not result.ok: raise ClientException("Error {}: {}".format( result.status_code, result.reason)) except requests.ConnectionError as err: raise ClientException( 'Failed to connect to REST API: {}'.format(err)) return json.loads(result.text)
async def get_public_key_info(request): public_key_address = request.params['public_key_address'] client = PubKeyClient() try: pub_key_data = await client.get_status(public_key_address) if not pub_key_data.payload.ByteSize(): raise KeyNotFound conf_name = pub_key_data.payload.WhichOneof('configuration') conf_payload = getattr(pub_key_data.payload, conf_name) now = time.time() valid_from = pub_key_data.payload.valid_from valid_to = pub_key_data.payload.valid_to return {'is_revoked': pub_key_data.is_revoked, 'owner_public_key': pub_key_data.owner, 'type': conf_name, 'is_valid': (not pub_key_data.is_revoked and valid_from < now and now < valid_to), 'valid_from': valid_from, 'valid_to': valid_to, 'public_key': binascii.hexlify(conf_payload.key).decode('utf-8'), 'entity_hash': pub_key_data.payload.entity_hash.decode('utf-8'), 'entity_hash_signature': binascii.hexlify(pub_key_data.payload.entity_hash_signature).decode('utf-8')} except KeyNotFound: raise KeyNotFound('Public key info not found')
async def list_receipts(request): ids = request.params['ids'] client = AccountClient() try: return await client.list_receipts(ids) except KeyNotFound: raise KeyNotFound(f'Transactions with ids "{ids}" not found')
async def get_blocks(request): start = request.params.get('start', 0) limit = request.params.get('limit', 0) try: return BlockInfoClient().get_blocks_info(start, limit) except KeyNotFound: raise KeyNotFound('Blocks not found')
async def fetch_state(request): address = request.params['address'] head = request.params.get('head') client = BasicClient() try: return await client.fetch_state(address, head) except KeyNotFound: raise KeyNotFound(f'Block with id "{id}" not found')
async def list_receipts(request): client = AccountClient() try: ids = request.params['ids'] except KeyError: raise RpcInvalidParamsError(message='Missed ids') try: return client.list_receipts(ids) except KeyNotFound: raise KeyNotFound(f'Transactions with ids "{ids}" not found')
async def fetch_transaction(request): try: id = request.params['id'] except KeyError: raise RpcInvalidParamsError(message='Missed id') client = AccountClient() try: return client.fetch_transaction(id) except KeyNotFound: raise KeyNotFound(f'Transaction with id "{id}" not found')
async def fetch_block(request): try: id = request.params['id'] except KeyError: raise RpcInvalidParamsError(message='Missed id') client = BlockInfoClient() try: return client.fetch_block(id) except KeyNotFound: raise KeyNotFound(f'Block with id "{id}" not found')
async def fetch_state(request): try: address = request.params['address'] except KeyError: raise RpcInvalidParamsError(message='Missed address') head = request.params.get('head') client = BasicClient() try: return client.fetch_state(address, head) except KeyNotFound: raise KeyNotFound(f'Block with id "{id}" not found')
async def get_atomic_swap_info(request): client = AtomicSwapClient() try: swap_id = request.params['swap_id'] except KeyError as e: raise RpcInvalidParamsError(message='Missed swap_id') try: swap_info = client.swap_get(swap_id) except KeyNotFound as e: raise KeyNotFound(f'Atomic swap with id "{swap_id}" not found') LOGGER.info(f'Get swap info {swap_info}') data = MessageToJson(swap_info, preserving_proto_field_name=True, including_default_value_fields=True) return json.loads(data)
async def get_public_key_info(request): request.params = request.params or {} try: public_key_address = request.params['public_key_address'] except KeyError: raise RpcInvalidParamsError(message='Missed public_key_address') client = PubKeyClient() try: pub_key_data = client.get_status(public_key_address) now = time.time() valid_from = pub_key_data.payload.valid_from valid_to = pub_key_data.payload.valid_to return {'is_revoked': pub_key_data.revoked, 'owner_public_key': pub_key_data.owner, 'is_valid': (not pub_key_data.revoked and valid_from < now and now < valid_to), 'valid_from': valid_from, 'valid_to': valid_to, 'entity_hash': pub_key_data.payload.entity_hash, 'entity_hash_signature': pub_key_data.payload.entity_hash_signature} except KeyNotFound: raise KeyNotFound('Public key info not found')
async def get_atomic_swap_public_key(request): client = AtomicSwapClient() try: return client.get_pub_key_encryption() except KeyNotFound: raise KeyNotFound('Public key for atomic swap not set')