class ETHTickerClient(ttk.Frame): def __init__( self, sender_privkey: str, session: Session = None, poll_interval: float = 5 ) -> None: self.poll_interval = poll_interval self.root = tkinter.Tk() ttk.Frame.__init__(self, self.root) self.root.title('µRaiden ETH Ticker') self.root.protocol('WM_DELETE_WINDOW', self.close) self.pack() self.pricevar = tkinter.StringVar(value='0.00 USD') ttk.Label(self, textvariable=self.pricevar, font=('Helvetica', '72')).pack() if session is None: self.session = Session( private_key=sender_privkey, close_channel_on_exit=True, endpoint_url='http://localhost:5000' ) else: self.session = session self.active_query = False self.running = False def run(self): self.running = True self.root.after(0, self.query_price) self.root.mainloop() def query_price(self): if not self.running: return self.active_query = True response = self.session.get('http://localhost:5000/ETHUSD') if response: price = float(response.json()['last_price']) log.info('New price received: {:.2f} USD'.format(price)) self.pricevar.set('{:.2f} USD'.format(price)) else: log.warning('No response.') if self.running: self.root.after(int(self.poll_interval * 1000), self.query_price) self.active_query = False def close(self): log.info('Shutting down gracefully.') self.running = False self.root.destroy() # Sloppy handling of thread joining but works for this small demo. while self.active_query: gevent.sleep(1) self.session.close()
def run(private_key: str, password_path: str, channel_manager_address: str = None, reqs: int = 20, web3: Web3 = None, retry_interval: float = 5, endpoint_url: str = 'http://localhost:5000'): # Create the client session. global session session = Session(endpoint_url=endpoint_url, private_key=private_key, key_password_path=password_path, channel_manager_address=channel_manager_address, web3=web3, initial_deposit=lambda price: 10 * 1000 * 1000 * price, topup_deposit=lambda price: 10 * 1000 * 1000 * price, close_channel_on_exit=True, retry_interval=retry_interval) resource = "/test/1" # Get the resource. If payment is required, client will attempt to create # a channel or will use existing one. last_time = time.time() global rqs last_reqs = 0 for i in range(0, reqs): response = session.get('{}/{}'.format(endpoint_url, resource)) if response.status_code == requests.codes.OK: rqs = rqs + 1 t = time.time() if (t - last_time) >= 1: print("transaction rate: ", rqs - last_reqs, " req/sec") last_time = t last_reqs = rqs continue if re.match('^text/', response.headers['Content-Type']): logging.info("Got the resource {} type={}:\n{}".format( resource, response.headers.get('Content-Type', '???'), response.text)) else: logging.info("Got the resource {} type={} (not echoed)".format( resource, response.headers.get('Content-Type', '???'))) else: logging.error("Error getting the resource. Code={} body={}".format( response.status_code, response.text)) print(rqs, " requests processed, closing session.") session.close() return response
def session(client: Client, use_tester: bool, api_endpoint_address: str): # patch request_resource of this instance in order to advance blocks when doing requests def request_patched(self: Session, method: str, url: str, **kwargs): if use_tester: log.info('Mining new block.') self.client.context.web3.testing.mine(1) return Session._request_resource(self, method, url, **kwargs) kwargs = {} if use_tester: kwargs['retry_interval'] = 0.1 session = Session(client, endpoint_url='http://' + api_endpoint_address, **kwargs) session._request_resource = types.MethodType(request_patched, session) yield session session.close()
def session(client: Client, use_tester: bool, api_endpoint_address: str): # patch request_resource of this instance in order to advance blocks when doing requests def request_patched(self: Session, method: str, url: str, **kwargs): if use_tester: log.info('Mining new block.') self.client.context.web3.testing.mine(1) return Session._request_resource(self, method, url, **kwargs) kwargs = {} if use_tester: kwargs['retry_interval'] = 0.1 session = Session( client, endpoint_url='http://' + api_endpoint_address, **kwargs ) session._request_resource = types.MethodType(request_patched, session) yield session session.close()