def send_request(self, method, relative_url, data=None): network = Network.get_instance() if not network: raise ErrorConnectingServer('You are offline.') url = urljoin(self.base_url, relative_url) if self.debug: self.print_error(f'<-- {method} {url} {data}') headers = {} if self.user_agent: headers['user-agent'] = self.user_agent try: if method == 'get': response = Network.send_http_on_proxy(method, url, params=data, headers=headers, on_finish=self.handle_response) elif method == 'post': response = Network.send_http_on_proxy(method, url, json=data, headers=headers, on_finish=self.handle_response) else: assert False except TrustedCoinException: raise except Exception as e: raise ErrorConnectingServer(e) else: if self.debug: self.print_error(f'--> {response}') return response
async def do_post(self, url = "/labels", data=None): url = 'https://' + self.target_host + url network = Network.get_instance() proxy = network.proxy if network else None async with make_aiohttp_session(proxy) as session: async with session.post(url, json=data) as result: try: return await result.json() except Exception as e: raise Exception('Could not decode: ' + await result.text()) from e
def __init__(self, config: 'SimpleConfig', exctype, value, tb): BaseCrashReporter.__init__(self, exctype, value, tb) self.network = Network.get_instance() self.config = config QWidget.__init__(self) self.setWindowTitle('Electrum - ' + _('An Error Occurred')) self.setMinimumSize(600, 300) Logger.__init__(self) main_box = QVBoxLayout() heading = QLabel('<h2>' + BaseCrashReporter.CRASH_TITLE + '</h2>') main_box.addWidget(heading) main_box.addWidget(QLabel(BaseCrashReporter.CRASH_MESSAGE)) main_box.addWidget(QLabel(BaseCrashReporter.REQUEST_HELP_MESSAGE)) collapse_info = QPushButton(_("Show report contents")) collapse_info.clicked.connect( lambda: self.msg_box(QMessageBox.NoIcon, self, _("Report contents"), self.get_report_string(), rich_text=True)) main_box.addWidget(collapse_info) main_box.addWidget(QLabel(BaseCrashReporter.DESCRIBE_ERROR_MESSAGE)) self.description_textfield = QTextEdit() self.description_textfield.setFixedHeight(50) self.description_textfield.setPlaceholderText(self.USER_COMMENT_PLACEHOLDER) main_box.addWidget(self.description_textfield) # main_box.addWidget(QLabel(BaseCrashReporter.ASK_CONFIRM_SEND)) buttons = QHBoxLayout() # report_button = QPushButton(_('Send Bug Report')) # report_button.clicked.connect(self.send_report) # report_button.setIcon(read_QIcon("tab_send.png")) # buttons.addWidget(report_button) # never_button = QPushButton(_('Never')) # never_button.clicked.connect(self.show_never) # buttons.addWidget(never_button) close_button = QPushButton(_('Not Now')) close_button.clicked.connect(self.close) buttons.addWidget(close_button) main_box.addLayout(buttons) self.setLayout(main_box) self.show()
def send_request(self, method, relative_url, data=None, *, timeout=None, server_address=None): network = Network.get_instance() if not network: raise ErrorConnectingServer('You are offline.') url = urljoin(server_address, relative_url) if self.debug: self.logger.info(f'<-- {method} {url} {data}') headers = {} if self.user_agent: headers['user-agent'] = self.user_agent try: if method == 'get': response = Network.send_http_on_proxy( method, url, params=data, headers=headers, on_finish=self.handle_response, timeout=timeout) elif method == 'post': response = Network.send_http_on_proxy( method, url, json=data, headers=headers, on_finish=self.handle_response, timeout=timeout) else: assert False except TrustedCoinException: raise except Exception as e: raise ErrorConnectingServer(e) else: if self.debug: self.logger.info(f'--> {response}') return response
def send_request(self, method, relative_url, data=None): network = Network.get_instance() if not network: raise ErrorConnectingServer('You are offline.') url = urljoin(self.base_url, relative_url) if self.debug: print('%s %s %s' % (method, url, data)) headers = {} if self.user_agent: headers['user-agent'] = self.user_agent try: if method == 'get': return Network.send_http_on_proxy(method, url, params=data, headers=headers, on_finish=self.handle_response) elif method == 'post': return Network.send_http_on_proxy(method, url, json=data, headers=headers, on_finish=self.handle_response) else: assert False except TrustedCoinException: raise except Exception as e: raise ErrorConnectingServer(e)
async def _send_request(self, method, relative_url, data): url = urljoin(self.base_url, relative_url) if self.debug: print('%s %s %s' % (method, url, data)) headers = {} if self.user_agent: headers['user-agent'] = self.user_agent try: proxy = Network.get_instance().proxy async with make_aiohttp_session(proxy) as session: if method == 'get': async with session.get(url, params=data, headers=headers) as resp: return await self.handle_response(resp) elif method == 'post': async with session.post(url, json=data, headers=headers) as resp: return await self.handle_response(resp) else: assert False except TrustedCoinException: raise except Exception as e: raise ErrorConnectingServer(e)
async def _send_request(self, method, relative_url, data): url = urljoin(self.base_url, relative_url) if self.debug: print('%s %s %s' % (method, url, data)) headers = {} if self.user_agent: headers['user-agent'] = self.user_agent try: proxy = Network.get_instance().proxy async with make_aiohttp_session(proxy) as session: if method == 'get': async with session.get(url, params=data, headers=headers) as resp: return await self.handle_response(resp) elif method == 'post': async with session.post(url, json=data, headers=headers) as resp: return await self.handle_response(resp) else: assert False except TrustedCoinException: raise except Exception as e: raise ErrorConnectingServer(e)
def set_server(self): _logger.info('Configuring server to 127.0.0.1:%s', self.rpc_port) # first, remove the `server` config to allow `set_parameters()` below to update it and trigger the connection mechanism del self.config.cmdline_options['server'] network = Network.get_instance() net_params = network.get_parameters() try: # Electrum v4 server = ServerAddr('127.0.0.1', self.rpc_port, protocol='t') net_params = net_params._replace(server=server, oneserver=True) except: # Electrum v3 net_params = net_params._replace( host='127.0.0.1', port=self.rpc_port, protocol='t', oneserver=True, ) network.run_from_another_thread(network.set_parameters(net_params)) # now set the server in `cmdline_options` to lock it in self.config.cmdline_options['server'] = '127.0.0.1:%s:t' % self.rpc_port
def __init__(self): QThread.__init__(self) Logger.__init__(self) self.network = Network.get_instance()
def send_request(self, method, relative_url, data=None): network = Network.get_instance() if network: return asyncio.run_coroutine_threadsafe(self._send_request(method, relative_url, data), network.asyncio_loop).result() else: raise ErrorConnectingServer('You are offline.')
def recovery(self): network = Network.get_instance() coroutine = account_discovery(network, self.get_account_xpub) return network.run_from_another_thread(coroutine)