Add 'args' param to post_json

This commit is contained in:
Erik Johnston 2017-07-18 16:40:21 +01:00
parent 94ecd871a0
commit 05c13f6c22
1 changed files with 15 additions and 9 deletions

View File

@ -347,7 +347,7 @@ class MatrixFederationHttpClient(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def post_json(self, destination, path, data={}, long_retries=False, def post_json(self, destination, path, data={}, long_retries=False,
timeout=None, ignore_backoff=False): timeout=None, ignore_backoff=False, args={}):
""" Sends the specifed json data using POST """ Sends the specifed json data using POST
Args: Args:
@ -383,6 +383,7 @@ class MatrixFederationHttpClient(object):
destination, destination,
"POST", "POST",
path, path,
query_bytes=encode_query_args(args),
body_callback=body_callback, body_callback=body_callback,
headers_dict={"Content-Type": ["application/json"]}, headers_dict={"Content-Type": ["application/json"]},
long_retries=long_retries, long_retries=long_retries,
@ -427,13 +428,6 @@ class MatrixFederationHttpClient(object):
""" """
logger.debug("get_json args: %s", args) logger.debug("get_json args: %s", args)
encoded_args = {}
for k, vs in args.items():
if isinstance(vs, basestring):
vs = [vs]
encoded_args[k] = [v.encode("UTF-8") for v in vs]
query_bytes = urllib.urlencode(encoded_args, True)
logger.debug("Query bytes: %s Retry DNS: %s", args, retry_on_dns_fail) logger.debug("Query bytes: %s Retry DNS: %s", args, retry_on_dns_fail)
def body_callback(method, url_bytes, headers_dict): def body_callback(method, url_bytes, headers_dict):
@ -444,7 +438,7 @@ class MatrixFederationHttpClient(object):
destination, destination,
"GET", "GET",
path, path,
query_bytes=query_bytes, query_bytes=encode_query_args(args),
body_callback=body_callback, body_callback=body_callback,
retry_on_dns_fail=retry_on_dns_fail, retry_on_dns_fail=retry_on_dns_fail,
timeout=timeout, timeout=timeout,
@ -610,3 +604,15 @@ def check_content_type_is_json(headers):
raise RuntimeError( raise RuntimeError(
"Content-Type not application/json: was '%s'" % c_type "Content-Type not application/json: was '%s'" % c_type
) )
def encode_query_args(args):
encoded_args = {}
for k, vs in args.items():
if isinstance(vs, basestring):
vs = [vs]
encoded_args[k] = [v.encode("UTF-8") for v in vs]
query_bytes = urllib.urlencode(encoded_args, True)
return query_bytes