mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-01-26 02:55:54 -05:00
Convert the SimpleHttpClient to async. (#8016)
This commit is contained in:
parent
e19de43eb5
commit
88a3ff12f0
1
changelog.d/8016.misc
Normal file
1
changelog.d/8016.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Convert various parts of the codebase to async/await.
|
@ -175,7 +175,7 @@ class ApplicationServiceApi(SimpleHttpClient):
|
|||||||
urllib.parse.quote(protocol),
|
urllib.parse.quote(protocol),
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
info = yield self.get_json(uri, {})
|
info = yield defer.ensureDeferred(self.get_json(uri, {}))
|
||||||
|
|
||||||
if not _is_valid_3pe_metadata(info):
|
if not _is_valid_3pe_metadata(info):
|
||||||
logger.warning(
|
logger.warning(
|
||||||
|
@ -284,8 +284,7 @@ class SimpleHttpClient(object):
|
|||||||
ip_blacklist=self._ip_blacklist,
|
ip_blacklist=self._ip_blacklist,
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
async def request(self, method, uri, data=None, headers=None):
|
||||||
def request(self, method, uri, data=None, headers=None):
|
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
method (str): HTTP method to use.
|
method (str): HTTP method to use.
|
||||||
@ -330,7 +329,7 @@ class SimpleHttpClient(object):
|
|||||||
self.hs.get_reactor(),
|
self.hs.get_reactor(),
|
||||||
cancelled_to_request_timed_out_error,
|
cancelled_to_request_timed_out_error,
|
||||||
)
|
)
|
||||||
response = yield make_deferred_yieldable(request_deferred)
|
response = await make_deferred_yieldable(request_deferred)
|
||||||
|
|
||||||
incoming_responses_counter.labels(method, response.code).inc()
|
incoming_responses_counter.labels(method, response.code).inc()
|
||||||
logger.info(
|
logger.info(
|
||||||
@ -353,8 +352,7 @@ class SimpleHttpClient(object):
|
|||||||
set_tag("error_reason", e.args[0])
|
set_tag("error_reason", e.args[0])
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
async def post_urlencoded_get_json(self, uri, args={}, headers=None):
|
||||||
def post_urlencoded_get_json(self, uri, args={}, headers=None):
|
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
uri (str):
|
uri (str):
|
||||||
@ -363,7 +361,7 @@ class SimpleHttpClient(object):
|
|||||||
header name to a list of values for that header
|
header name to a list of values for that header
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Deferred[object]: parsed json
|
object: parsed json
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
HttpResponseException: On a non-2xx HTTP response.
|
HttpResponseException: On a non-2xx HTTP response.
|
||||||
@ -386,11 +384,11 @@ class SimpleHttpClient(object):
|
|||||||
if headers:
|
if headers:
|
||||||
actual_headers.update(headers)
|
actual_headers.update(headers)
|
||||||
|
|
||||||
response = yield self.request(
|
response = await self.request(
|
||||||
"POST", uri, headers=Headers(actual_headers), data=query_bytes
|
"POST", uri, headers=Headers(actual_headers), data=query_bytes
|
||||||
)
|
)
|
||||||
|
|
||||||
body = yield make_deferred_yieldable(readBody(response))
|
body = await make_deferred_yieldable(readBody(response))
|
||||||
|
|
||||||
if 200 <= response.code < 300:
|
if 200 <= response.code < 300:
|
||||||
return json.loads(body.decode("utf-8"))
|
return json.loads(body.decode("utf-8"))
|
||||||
@ -399,8 +397,7 @@ class SimpleHttpClient(object):
|
|||||||
response.code, response.phrase.decode("ascii", errors="replace"), body
|
response.code, response.phrase.decode("ascii", errors="replace"), body
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
async def post_json_get_json(self, uri, post_json, headers=None):
|
||||||
def post_json_get_json(self, uri, post_json, headers=None):
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -410,7 +407,7 @@ class SimpleHttpClient(object):
|
|||||||
header name to a list of values for that header
|
header name to a list of values for that header
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Deferred[object]: parsed json
|
object: parsed json
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
HttpResponseException: On a non-2xx HTTP response.
|
HttpResponseException: On a non-2xx HTTP response.
|
||||||
@ -429,11 +426,11 @@ class SimpleHttpClient(object):
|
|||||||
if headers:
|
if headers:
|
||||||
actual_headers.update(headers)
|
actual_headers.update(headers)
|
||||||
|
|
||||||
response = yield self.request(
|
response = await self.request(
|
||||||
"POST", uri, headers=Headers(actual_headers), data=json_str
|
"POST", uri, headers=Headers(actual_headers), data=json_str
|
||||||
)
|
)
|
||||||
|
|
||||||
body = yield make_deferred_yieldable(readBody(response))
|
body = await make_deferred_yieldable(readBody(response))
|
||||||
|
|
||||||
if 200 <= response.code < 300:
|
if 200 <= response.code < 300:
|
||||||
return json.loads(body.decode("utf-8"))
|
return json.loads(body.decode("utf-8"))
|
||||||
@ -442,8 +439,7 @@ class SimpleHttpClient(object):
|
|||||||
response.code, response.phrase.decode("ascii", errors="replace"), body
|
response.code, response.phrase.decode("ascii", errors="replace"), body
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
async def get_json(self, uri, args={}, headers=None):
|
||||||
def get_json(self, uri, args={}, headers=None):
|
|
||||||
""" Gets some json from the given URI.
|
""" Gets some json from the given URI.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -455,7 +451,7 @@ class SimpleHttpClient(object):
|
|||||||
headers (dict[str|bytes, List[str|bytes]]|None): If not None, a map from
|
headers (dict[str|bytes, List[str|bytes]]|None): If not None, a map from
|
||||||
header name to a list of values for that header
|
header name to a list of values for that header
|
||||||
Returns:
|
Returns:
|
||||||
Deferred: Succeeds when we get *any* 2xx HTTP response, with the
|
Succeeds when we get *any* 2xx HTTP response, with the
|
||||||
HTTP body as JSON.
|
HTTP body as JSON.
|
||||||
Raises:
|
Raises:
|
||||||
HttpResponseException On a non-2xx HTTP response.
|
HttpResponseException On a non-2xx HTTP response.
|
||||||
@ -466,11 +462,10 @@ class SimpleHttpClient(object):
|
|||||||
if headers:
|
if headers:
|
||||||
actual_headers.update(headers)
|
actual_headers.update(headers)
|
||||||
|
|
||||||
body = yield self.get_raw(uri, args, headers=headers)
|
body = await self.get_raw(uri, args, headers=headers)
|
||||||
return json.loads(body.decode("utf-8"))
|
return json.loads(body.decode("utf-8"))
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
async def put_json(self, uri, json_body, args={}, headers=None):
|
||||||
def put_json(self, uri, json_body, args={}, headers=None):
|
|
||||||
""" Puts some json to the given URI.
|
""" Puts some json to the given URI.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -483,7 +478,7 @@ class SimpleHttpClient(object):
|
|||||||
headers (dict[str|bytes, List[str|bytes]]|None): If not None, a map from
|
headers (dict[str|bytes, List[str|bytes]]|None): If not None, a map from
|
||||||
header name to a list of values for that header
|
header name to a list of values for that header
|
||||||
Returns:
|
Returns:
|
||||||
Deferred: Succeeds when we get *any* 2xx HTTP response, with the
|
Succeeds when we get *any* 2xx HTTP response, with the
|
||||||
HTTP body as JSON.
|
HTTP body as JSON.
|
||||||
Raises:
|
Raises:
|
||||||
HttpResponseException On a non-2xx HTTP response.
|
HttpResponseException On a non-2xx HTTP response.
|
||||||
@ -504,11 +499,11 @@ class SimpleHttpClient(object):
|
|||||||
if headers:
|
if headers:
|
||||||
actual_headers.update(headers)
|
actual_headers.update(headers)
|
||||||
|
|
||||||
response = yield self.request(
|
response = await self.request(
|
||||||
"PUT", uri, headers=Headers(actual_headers), data=json_str
|
"PUT", uri, headers=Headers(actual_headers), data=json_str
|
||||||
)
|
)
|
||||||
|
|
||||||
body = yield make_deferred_yieldable(readBody(response))
|
body = await make_deferred_yieldable(readBody(response))
|
||||||
|
|
||||||
if 200 <= response.code < 300:
|
if 200 <= response.code < 300:
|
||||||
return json.loads(body.decode("utf-8"))
|
return json.loads(body.decode("utf-8"))
|
||||||
@ -517,8 +512,7 @@ class SimpleHttpClient(object):
|
|||||||
response.code, response.phrase.decode("ascii", errors="replace"), body
|
response.code, response.phrase.decode("ascii", errors="replace"), body
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
async def get_raw(self, uri, args={}, headers=None):
|
||||||
def get_raw(self, uri, args={}, headers=None):
|
|
||||||
""" Gets raw text from the given URI.
|
""" Gets raw text from the given URI.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -530,7 +524,7 @@ class SimpleHttpClient(object):
|
|||||||
headers (dict[str|bytes, List[str|bytes]]|None): If not None, a map from
|
headers (dict[str|bytes, List[str|bytes]]|None): If not None, a map from
|
||||||
header name to a list of values for that header
|
header name to a list of values for that header
|
||||||
Returns:
|
Returns:
|
||||||
Deferred: Succeeds when we get *any* 2xx HTTP response, with the
|
Succeeds when we get *any* 2xx HTTP response, with the
|
||||||
HTTP body as bytes.
|
HTTP body as bytes.
|
||||||
Raises:
|
Raises:
|
||||||
HttpResponseException on a non-2xx HTTP response.
|
HttpResponseException on a non-2xx HTTP response.
|
||||||
@ -543,9 +537,9 @@ class SimpleHttpClient(object):
|
|||||||
if headers:
|
if headers:
|
||||||
actual_headers.update(headers)
|
actual_headers.update(headers)
|
||||||
|
|
||||||
response = yield self.request("GET", uri, headers=Headers(actual_headers))
|
response = await self.request("GET", uri, headers=Headers(actual_headers))
|
||||||
|
|
||||||
body = yield make_deferred_yieldable(readBody(response))
|
body = await make_deferred_yieldable(readBody(response))
|
||||||
|
|
||||||
if 200 <= response.code < 300:
|
if 200 <= response.code < 300:
|
||||||
return body
|
return body
|
||||||
@ -557,8 +551,7 @@ class SimpleHttpClient(object):
|
|||||||
# XXX: FIXME: This is horribly copy-pasted from matrixfederationclient.
|
# XXX: FIXME: This is horribly copy-pasted from matrixfederationclient.
|
||||||
# The two should be factored out.
|
# The two should be factored out.
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
async def get_file(self, url, output_stream, max_size=None, headers=None):
|
||||||
def get_file(self, url, output_stream, max_size=None, headers=None):
|
|
||||||
"""GETs a file from a given URL
|
"""GETs a file from a given URL
|
||||||
Args:
|
Args:
|
||||||
url (str): The URL to GET
|
url (str): The URL to GET
|
||||||
@ -574,7 +567,7 @@ class SimpleHttpClient(object):
|
|||||||
if headers:
|
if headers:
|
||||||
actual_headers.update(headers)
|
actual_headers.update(headers)
|
||||||
|
|
||||||
response = yield self.request("GET", url, headers=Headers(actual_headers))
|
response = await self.request("GET", url, headers=Headers(actual_headers))
|
||||||
|
|
||||||
resp_headers = dict(response.headers.getAllRawHeaders())
|
resp_headers = dict(response.headers.getAllRawHeaders())
|
||||||
|
|
||||||
@ -598,7 +591,7 @@ class SimpleHttpClient(object):
|
|||||||
# straight back in again
|
# straight back in again
|
||||||
|
|
||||||
try:
|
try:
|
||||||
length = yield make_deferred_yieldable(
|
length = await make_deferred_yieldable(
|
||||||
_readBodyToFile(response, output_stream, max_size)
|
_readBodyToFile(response, output_stream, max_size)
|
||||||
)
|
)
|
||||||
except SynapseError:
|
except SynapseError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user