def __init__(self, netParams, dbPath, url, user, pw, certPath=None): """ Args: netParams (module): The network parameters. dbPath (str or Path): A path to a database file for use by the LocalNode. url (str): The URL for the local node, with protocol. user (str): The user name for authenticating the connection. pw (str): The password for authenticating the connection. certPath (str or Path): A filepath to the dcrd TLS certificate. """ self.netParams = netParams self.db = database.KeyValueDatabase(dbPath) self.mainchainDB = self.db.child( "mainchain", datatypes=("INTEGER", "BLOB"), blobber=ByteArray ) self.headerDB = self.db.child( "headers", blobber=BlockHeader, keyBlobber=ByteArray ) self.socketURL = helpers.makeWebsocketURL(url, "ws") self.rpc = None def connect(): self.close() self.rpc = rpc.WebsocketClient(self.socketURL, user, pw, cert=certPath) self.connect = connect connect()
def __init__(self, url, emitter=None): """ Build the DcrdataPath tree. Args: url (string): the URL to a DCRData server, e.g. http://explorer.dcrdata.org/. emitter (function): a function that accepts incoming subscription messages as JSON-decoded dicts as the only parameter. """ url = urlsplit(url) # Remove any path. self.baseURL = urlunsplit((url.scheme, url.netloc, "/", "", "")) # Add the "/api" path. self.baseApi = urlunsplit((url.scheme, url.netloc, "/api/", "", "")) self.psURL = makeWebsocketURL(self.baseURL, "ps") self.ps = None self.subscribedAddresses = [] self.emitter = emitter if emitter else lambda msg: None atexit.register(self.close) root = self.root = DcrdataPath() self.listEntries = [] # /list returns a json list of enpoints with parameters in template format, # base/A/{param}/B listURL = urljoin(self.baseApi, "list") endpoints = tinyhttp.get(listURL, headers=GET_HEADERS) endpoints += InsightPaths def getParam(part): if part.startswith("{") and part.endswith("}"): return part[1:-1] return None pathlog = [] for path in endpoints: path = path.rstrip("/") if path in pathlog or path == "": continue pathlog.append(path) baseURL = self.baseURL if "insight" in path else self.baseApi params = [] pathSequence = [] templateParts = [] # split the path into an array for nodes and an array for parameters for i, part in enumerate(path.strip("/").split("/")): param = getParam(part) if param: params.append(param) templateParts.append("%s") else: pathSequence.append(part) templateParts.append(part) pathPointer = root for pathPart in pathSequence: pathPointer = pathPointer.getSubpath(pathPart) pathPointer.addCallsign(params, baseURL + "/".join(templateParts)) if len(pathSequence) == 1: continue self.listEntries.append( ("%s(%s)" % (".".join(pathSequence), ", ".join(params)), path))
def test_makeWebsocketURL(): """ Tests are 3-tuples: baseURL (str): The base URL. path (str): The websocket endpoint path. want (str): The expected result. """ tests = [ ("https://localhost:19556", "ws", "wss://localhost:19556/ws"), ("http://localhost:8337", "ws1", "ws://localhost:8337/ws1"), ("ws://localhost:9999", "ws2", "ws://localhost:9999/ws2"), ("wss://localhost", "ws3", "wss://localhost/ws3"), ("localhost:1234", "ws4", "wss://localhost:1234/ws4"), ] for baseURL, path, want in tests: assert helpers.makeWebsocketURL(baseURL, path) == want