Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def send_request():
"""
Send an HTTP request in a loop until the request is answered.
"""
response = request(
b"GET", b"http://127.0.0.1:%d" % (port,),
persistent=False)
def check_error(failure):
"""
Catch ConnectionRefused errors and response timeouts and return
False so that loop_until repeats the request.
Other error conditions will be passed down the errback chain.
"""
failure.trap(ConnectionRefusedError, ResponseNeverReceived)
return False
response.addErrback(check_error)
return response
def a_case(fname):
env = treq.load_py(os.path.splitext(fname)[0] + ".py")
expect = env['request']
cfg = env['cfg']
req = treq.request(fname, expect)
for case in req.gen_cases(cfg):
case[0](*case[1:])
def process_queue(self):
while True:
thing = yield self.queue_object.get()
if thing is None:
break
ch, method, properties, body = thing
if body:
path = body
self.log("Purging %s" % path)
try:
response = yield treq.request(
"PURGE", "http://" \
+ self.config.get("nginx-host", "127.0.0.1") + path,
#cookies={"foo": "bar"}, todo add to config
headers={"Host": self.config.get("domain", "actual.host.com")},
timeout=10
)
except Exception as exception:
msg = traceback.format_exc()
self.log("Error purging %s: %s" % (path, msg))
else:
content = yield response.content()
yield ch.basic_ack(delivery_tag=method.delivery_tag)
def perform_request_with_treq(dispatcher, http_request):
"""A performer for :obj:`HTTPRequest` that uses the ``treq`` library."""
headers = (
http_request.headers.copy()
if http_request.headers is not None
else {})
if 'user-agent' not in headers:
headers['user-agent'] = ['Effect example']
d = treq.request(
http_request.method.lower(),
http_request.url,
headers=headers,
data=http_request.data).addCallback(treq.content)
return d
async def do_request(cmd, resp_type, *extra_args):
method, path, body = cmd
resp = await treq.request(method, endpoint + path, data=body.encode("utf8"))
body = await treq.content(resp)
assert resp.code == 200, body
resp_body = await treq.json_content(resp)
matrix_response = resp_type.from_dict(resp_body, *extra_args)
return matrix_response
def http_request(url,method='GET',headers=None,params=None,data=None,callback=None,json=True, code_only=False):
logging.critical('#####################################\n## THIS IS DEPRECATED - DO NOT USE ##\n#####################################')
request = treq.request(url=url,method=method,headers=headers,data=data)
if callback:
if code_only:
request.addCallback(callback)
if json:
request.addCallback(json_callback, callback)
else:
request.addCallback(text_callback, callback)
with start_active_span(
"outgoing-client-request",
tags={
tags.SPAN_KIND: tags.SPAN_KIND_RPC_CLIENT,
tags.HTTP_METHOD: method,
tags.HTTP_URL: uri,
},
finish_on_close=True,
):
try:
body_producer = None
if data is not None:
body_producer = QuieterFileBodyProducer(BytesIO(data))
request_deferred = treq.request(
method,
uri,
agent=self.agent,
data=body_producer,
headers=headers,
**self._extra_treq_args
)
request_deferred = timeout_deferred(
request_deferred,
60,
self.hs.get_reactor(),
cancelled_to_request_timed_out_error,
)
response = yield make_deferred_yieldable(request_deferred)
incoming_responses_counter.labels(method, response.code).inc()
def request(method, url, **kwargs):
"""
Wrapper around :meth:`treq.request` that logs the request.
See :py:func:`treq.request`
"""
return _log_request(treq.request, url, method=method, **kwargs)