Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get(self, *args, **kwargs):
kwargs['agent'] = Agent(reactor)
return treq.get(*args, **kwargs)
def get_tenant_ids(token, catalog):
endpoint = public_endpoint_url(catalog, "cloudMetrics", "IAD")
d = treq.get(
append_segments(endpoint, "metrics", "search"),
headers=headers(token), params={"query": "*.*.desired"})
d.addCallback(check_success, [200])
d.addCallback(treq.json_content)
d.addCallback(lambda body: [item["metric"].split(".")[1] for item in body])
return d
def fetch_icon(self, url, dest):
agent = None
if self.use_tor:
tor = yield get_tor(reactor)
if not tor:
raise TorError("Could not connect to a running Tor daemon")
agent = tor.web_agent()
resp = yield treq.get(url, agent=agent)
if resp.code == 200:
content = yield treq.content(resp)
log.debug("Received %i bytes", len(content))
with atomic_write(dest, mode="wb", overwrite=True) as f:
f.write(content)
self.got_icon.emit(dest)
else:
log.warning("Error fetching service icon: %i", resp.code)
def http_call(self, urlpart, cb, **kwargs):
headers = Headers({"User-Agent": [USER_AGENT]})
d = treq.get('%s/%s' % (self.host, urlpart),
params=kwargs, headers=headers)
d.addCallback(lambda response: self.decode_or_error(
response, cb, urlpart, **kwargs))
def download_data(self, src, dst):
f = open(dst, 'wb')
resp = yield treq.get(src, agent=no_verify_agent())
yield treq.collect(resp, f.write)
f.close()
def _download(reactor, url, destination_filename):
destination = open(destination_filename, 'wb')
d = treq.get(url)
d.addCallback(treq.collect, destination.write)
d.addBoth(lambda _: destination.close())
return d
def download_file(url, destination):
d = treq.get(url)
d.addCallback(treq.collect, destination.write)
d.addBoth(destination.close)
return d
async def fetch_async(pid):
print('Fetch async process {} started'.format(pid))
start = time.time()
response = await treq.get(URL)
# unfortunately Twisted's Agent which treq is base on does not expose
# connection headers so we cannot get the Date header in the same way
# https://github.com/twisted/treq/issues/56#issuecomment-36573795
content_type = response.headers.getRawHeaders('Content-Type', '')
print('Process {}: {}, took: {:.2f} seconds'.format(
pid, content_type, time.time() - start))
return datetime
def _make_request(self):
response = yield treq.get(self.url, params=self.params, timeout=self.REQUESTS_TIMEOUT)
defer.returnValue((yield response.content()))
def get_comic_by_id(self, comic_id):
"""
Returns the info for the given comic
:param url: Comic ID number
:return: Deferred that fires with a dict of info
"""
self.logger.debug("Getting comic by ID")
if comic_id in self._comic_cache:
return defer.succeed(dict(self._comic_cache[comic_id]))
else:
d = treq.get("http://xkcd.com/%s/info.0.json" % comic_id)
d.addCallbacks(self._get_comic_result,
self._get_comic_error,
[comic_id])
return d