def init(http_client: HTTPClient = None, client: Client = None, privkey: str = None, key_path: str = None, key_password_path: str = None, channel_manager_address: str = CHANNEL_MANAGER_ADDRESS, web3: Web3 = None, retry_interval: float = 5, initial_deposit: Callable[[int], int] = lambda price: 10 * price, topup_deposit: Callable[[int], int] = lambda price: 5 * price): """ TODO: document which of these arguments are actually needed in different use cases. """ global _http_client if http_client is None: if client is None: client = Client(private_key=privkey, key_path=key_path, key_password_path=key_password_path, channel_manager_address=channel_manager_address, web3=web3) _http_client = DefaultHTTPClient(client=client, retry_interval=retry_interval, initial_deposit=initial_deposit, topup_deposit=topup_deposit) else: _http_client = http_client
def __init__( self, sender_privkey: str, httpclient: DefaultHTTPClient = None ): 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, 'localhost', 5000, initial_deposit=lambda x: 10 * x, topup_deposit=lambda x: 5 * x ) self.active_query = False self.running = False
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 = DefaultHTTPClient( 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 init(privkey: str = None, key_path: str = None, key_password_path: str = None, channel_manager_address: str = CHANNEL_MANAGER_ADDRESS, web3: Web3 = None, channel_manager_proxy: ChannelContractProxy = None, token_proxy: ContractProxy = None, contract_metadata: dict = CONTRACT_METADATA, retry_interval: float = 5, initial_deposit: Callable[[int], int] = lambda price: 10 * price, topup_deposit: Callable[[int], int] = lambda price: 5 * price): global client global http_client client = Client(privkey=privkey, key_path=key_path, key_password_path=key_password_path, channel_manager_address=channel_manager_address, web3=web3, channel_manager_proxy=channel_manager_proxy, token_proxy=token_proxy, contract_metadata=contract_metadata) http_client = DefaultHTTPClient(client=client, retry_interval=retry_interval, initial_deposit=initial_deposit, topup_deposit=topup_deposit)
def default_http_client(client: Client): # patch request_resource of this instance in order to advance blocks when doing requests def request_patched(self: DefaultHTTPClient, method: str, url: str, **kwargs): if self.client.channel_manager_proxy.tester_mode: log.info('Mining new block.') self.client.web3.testing.mine(1) return DefaultHTTPClient._request_resource(self, method, url, **kwargs) http_client = DefaultHTTPClient(client, retry_interval=0.1) http_client._request_resource = types.MethodType(request_patched, http_client) yield http_client
def default_http_client(client, api_endpoint, api_endpoint_port): # patch request_resource of this instance in order to advance blocks when doing requests x = DefaultHTTPClient._request_resource def request_patched(self: DefaultHTTPClient): if self.client.channel_manager_proxy.tester_mode: log.info('Mining new block.') self.client.web3.testing.mine(1) return x(self) DefaultHTTPClient._request_resource = request_patched http_client = DefaultHTTPClient(client, api_endpoint, api_endpoint_port, retry_interval=0.5) return http_client
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 = DefaultHTTPClient(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()