def test_request_urlopen_error(monkeypatch): def urlopen_error(req, context=None): raise DecredError("test error") monkeypatch.setattr(urlrequest, "urlopen", urlopen_error) with pytest.raises(DecredError): tinyhttp.get("http://example.org/")
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 getStats(self): """ Get the stats from the stake pool API. Returns: Poolstats: The PoolStats object. """ res = tinyhttp.get(self.apiPath("stats"), headers=self.headers()) if resultIsSuccess(res): self.stats = PoolStats(res["data"]) return self.stats raise DecredError("unexpected response from 'stats': %s" % repr(res))
def providers(netParams): """ A static method to get the current Decred VSP list. Args: netParams (module): The network parameters. Returns: list(object): The vsp list. """ vsps = tinyhttp.get(API_URL) network = nets.normalizeName(netParams.Name) return [vsp for vsp in vsps.values() if vsp["Network"] == network]
def getPurchaseInfo(self): """ Get the purchase info from the stake pool API. Returns: PurchaseInfo: The PurchaseInfo object. """ self.err = None res = tinyhttp.get(self.apiPath("getpurchaseinfo"), headers=self.headers()) if resultIsSuccess(res): pi = PurchaseInfo.parse(res["data"]) # check the script hash self.purchaseInfo = pi return self.purchaseInfo self.err = res raise DecredError("unexpected response from 'getpurchaseinfo': %r" % (res, ))
def __call__(self, *args, **kwargs): return tinyhttp.get(self.getCallsignPath(*args, **kwargs), headers=GET_HEADERS)
def test_get(urlopen): urlopen("http://example.org/", None, "[0, 1, 2]") assert tinyhttp.get("http://example.org/") == [0, 1, 2]