def run(private_key: str, password_path: str, resource: str, channel_manager_address: str = None, web3: Web3 = None, retry_interval: float = 5, endpoint_url: str = 'http://localhost:5000'): # Create the client session. session = Session(endpoint_url=endpoint_url, private_key=private_key, key_password_path=password_path, channel_manager_address=channel_manager_address, web3=web3, retry_interval=retry_interval) # Get the resource. If payment is required, client will attempt to create # a channel or will use existing one. response = session.get('{}/{}'.format(endpoint_url, resource)) if response.status_code == requests.codes.OK: 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)) return response
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 request(method: str, url: str, **kwargs) -> Response: session_kwargs = pop_function_kwargs(kwargs, Session.__init__) client_kwargs = pop_function_kwargs(kwargs, Client.__init__) session_kwargs.update(client_kwargs) with Session(**session_kwargs) as session: return session.request(method, url, **kwargs)
def close_channels(private_key: str, password_path: str, resource: str, channel_manager_address: str = None, web3: Web3 = None, retry_interval: float = 5, endpoint_url: str = 'http://0.0.0.0:5010', close_channel: bool = False): # Create the client session. session = Session(endpoint_url=endpoint_url, private_key=private_key, key_password_path=password_path, channel_manager_address=channel_manager_address, web3=web3, retry_interval=retry_interval, close_channel_on_exit=close_channel) #conn = Client(monitor_address) #conn.send('share') ##print(conn.recv()) #conn.close() print("Private Key:", private_key) addr = privkey_to_addr(private_key) print("Address:", addr) # response = requests.get('http://0.0.0.0:5000/api/1/channels/{}'.format(addr)) #response = session.get('{}/api/1/channels/{}'.format('http://0.0.0.0:5000', addr)) #print(response) response = session.get('{}/{}'.format('http://0.0.0.0:5010', resource)) print(response) print(response.text) print(response.content) print(response.headers) # time.sleep(4) response = session.get('{}/{}'.format('http://0.0.0.0:5010', resource)) print(response) print(response.text) print(response.content) print(response.headers) time.sleep(4) response = session.get('{}/{}'.format('http://0.0.0.0:5010', resource)) print(response) print(response.text) print(response.content) print(response.headers) # time.sleep(10) session.channel.close(balance=session.channel.balance - 1)
def run(api_endpoint, api_port, **kwargs): exclude_kwargs = {'close_channels'} kwargs_client = { key: value for key, value in kwargs.items() if value and key not in exclude_kwargs } with Client(**kwargs_client) as client: m2mclient = Session(client, api_endpoint, api_port) resource = m2mclient.run('doggo.jpg') log.info('Response: {}'.format(resource)) if kwargs['close_channels'] is True: for channel in client.channels: if channel.state == Channel.State.open: channel.close_channel()
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 run(key_path, key_password_path, resource): # create the client with Client(key_path=key_path, key_password_path=key_password_path) as client: m2mclient = Session(client, 'localhost', 5000) # Get the resource. If payment is required, client will attempt to create # a channel or will use existing one. status, headers, body = m2mclient.run(resource) if status == requests.codes.OK: if re.match('^text\/', headers['Content-Type']): logging.info( "got the resource %s type=%s\n%s" % (resource, headers.get('Content-Type', '???'), body)) else: logging.info("got the resource %s type=%s" % (resource, headers.get('Content-Type', '???'))) else: logging.error("error getting the resource. code=%d body=%s" % (status, body.decode().strip()))
def run(private_key: str, password_path: str, resource: str, channel_manager_address: str = None, web3: Web3 = None, retry_interval: float = 5, endpoint_url: str = 'http://localhost:5000'): session = Session(endpoint_url=endpoint_url, private_key=private_key, key_password_path=password_path, channel_manager_address=channel_manager_address, web3=web3, retry_interval=retry_interval, initial_deposit=lambda x: x) stream_url = '{}/{}'.format(endpoint_url, resource) logging.info("Calling={}".format(stream_url)) with session.get(stream_url, stream=True) as response: if response.status_code == requests.codes.OK: for line in response.iter_lines(decode_unicode=True): if line: print(line) else: logging.error("Error getting the resource. Code={} body={}".format( response.status_code, response.text))
def run( private_key: str, password_path: str, command: str, rpc_provider: str, channel_manager_address: str = None, web3: Web3 = None, retry_interval: float = 5, endpoint_url: str = 'http://localhost:5000' ): if channel_manager_address is None: web3 = Web3(HTTPProvider(rpc_provider)) NETWORK_CFG.set_defaults(int(web3.version.network)) # Create the client session. client = NewClient( private_key, password_path, NETWORK_CFG.CHANNEL_MANAGER_ADDRESS, web3 ) session = Session( client=client, endpoint_url=endpoint_url, private_key=private_key, key_password_path=password_path, channel_manager_address=NETWORK_CFG.CHANNEL_MANAGER_ADDRESS, web3=web3, retry_interval=retry_interval, initial_deposit= lambda price: 5 * TKN_DECIMALS, topup_deposit= lambda price: 5 * TKN_DECIMALS, close_channel_on_exit=False ) # Get the resource. If payment is required, client will attempt to create # a channel or will use existing one. resource = command response = session.get('{}/{}'.format(endpoint_url, resource)) if response.status_code == requests.codes.OK: 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 ) ) return response