summary refs log tree commit diff
path: root/synapse/http/client.py
diff options
context:
space:
mode:
authorDavid Robertson <davidr@element.io>2022-04-08 13:06:51 +0100
committerGitHub <noreply@github.com>2022-04-08 13:06:51 +0100
commit95a038c1069de6c0507eb2c2d9a783c5033a70ec (patch)
tree782b33d85db7ef0b54e9e49a9d629b7675a52334 /synapse/http/client.py
parentUpdate the server notices user profile in room if changed. (#12115) (diff)
downloadsynapse-95a038c1069de6c0507eb2c2d9a783c5033a70ec.tar.xz
Unify HTTP query parameter type hints (#12415)
* Pull out query param types to `synapse.http.types`
* Use QueryParams everywhere
* Simplify `encode_query_args`
* Add annotation which would have caught #12410
Diffstat (limited to 'synapse/http/client.py')
-rw-r--r--synapse/http/client.py16
1 files changed, 3 insertions, 13 deletions
diff --git a/synapse/http/client.py b/synapse/http/client.py
index c01d2326cf..8310fb466a 100644
--- a/synapse/http/client.py
+++ b/synapse/http/client.py
@@ -22,7 +22,6 @@ from typing import (
     BinaryIO,
     Callable,
     Dict,
-    Iterable,
     List,
     Mapping,
     Optional,
@@ -72,6 +71,7 @@ from twisted.web.iweb import (
 from synapse.api.errors import Codes, HttpResponseException, SynapseError
 from synapse.http import QuieterFileBodyProducer, RequestTimedOutError, redact_uri
 from synapse.http.proxyagent import ProxyAgent
+from synapse.http.types import QueryParams
 from synapse.logging.context import make_deferred_yieldable
 from synapse.logging.opentracing import set_tag, start_active_span, tags
 from synapse.types import ISynapseReactor
@@ -97,10 +97,6 @@ RawHeaders = Union[Mapping[str, "RawHeaderValue"], Mapping[bytes, "RawHeaderValu
 # the entries can either be Lists or bytes.
 RawHeaderValue = Sequence[Union[str, bytes]]
 
-# the type of the query params, to be passed into `urlencode`
-QueryParamValue = Union[str, bytes, Iterable[Union[str, bytes]]]
-QueryParams = Union[Mapping[str, QueryParamValue], Mapping[bytes, QueryParamValue]]
-
 
 def check_against_blacklist(
     ip_address: IPAddress, ip_whitelist: Optional[IPSet], ip_blacklist: IPSet
@@ -911,7 +907,7 @@ def read_body_with_max_size(
     return d
 
 
-def encode_query_args(args: Optional[Mapping[str, Union[str, List[str]]]]) -> bytes:
+def encode_query_args(args: Optional[QueryParams]) -> bytes:
     """
     Encodes a map of query arguments to bytes which can be appended to a URL.
 
@@ -924,13 +920,7 @@ def encode_query_args(args: Optional[Mapping[str, Union[str, List[str]]]]) -> by
     if args is None:
         return b""
 
-    encoded_args = {}
-    for k, vs in args.items():
-        if isinstance(vs, str):
-            vs = [vs]
-        encoded_args[k] = [v.encode("utf8") for v in vs]
-
-    query_str = urllib.parse.urlencode(encoded_args, True)
+    query_str = urllib.parse.urlencode(args, True)
 
     return query_str.encode("utf8")