class ETHTickerClient(ttk.Frame): def __init__(self, sender_privkey: str, httpclient: DefaultHTTPClient = 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 httpclient: self.httpclient = httpclient self.client = httpclient.client else: self.client = Client(sender_privkey) self.httpclient = DefaultHTTPClient( self.client, initial_deposit=lambda x: 10 * x, topup_deposit=lambda x: 5 * x) 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.httpclient.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.httpclient.close_active_channel('http://localhost:5000')
def test_default_http_client_close(doggo_proxy, default_http_client: DefaultHTTPClient): client = default_http_client.client check_response(default_http_client.run('doggo.jpg')) default_http_client.close_active_channel() open_channels = client.get_open_channels() assert len(open_channels) == 0
def test_default_http_client_close(doggo_proxy, default_http_client: DefaultHTTPClient, http_doggo_url: str): client = default_http_client.client check_response(default_http_client.get(http_doggo_url)) default_http_client.close_active_channel(http_doggo_url) open_channels = client.get_open_channels() assert len(open_channels) == 0
def test_cooperative_close_denied(default_http_client: DefaultHTTPClient, api_endpoint_address: str, token_address: str, channel_manager_address: str, receiver_address: str): cooperative_close_denied_mock = mock.patch.object( default_http_client, 'on_cooperative_close_denied', wraps=default_http_client.on_cooperative_close_denied).start() with requests_mock.mock() as server_mock: headers = { HTTPHeaders.TOKEN_ADDRESS: token_address, HTTPHeaders.CONTRACT_ADDRESS: channel_manager_address, HTTPHeaders.RECEIVER_ADDRESS: receiver_address, HTTPHeaders.PRICE: '3' } headers = [headers.copy() for _ in range(2)] headers[1][HTTPHeaders.COST] = '3' url = 'http://{}/something'.format(api_endpoint_address) server_mock.get(url, [ { 'status_code': 402, 'headers': headers[0] }, { 'status_code': 200, 'headers': headers[1], 'text': 'success' }, ]) channel_url = re.compile( 'http://{}/api/1/channels/0x.{{40}}/\d+'.format( api_endpoint_address)) server_mock.delete(channel_url, [{'status_code': 403}]) response = default_http_client.get(url) default_http_client.close_active_channel('http://' + api_endpoint_address) assert response.text == 'success' assert cooperative_close_denied_mock.call_count == 1 assert default_http_client.client.channels[ 0].state == Channel.State.settling