summary refs log tree commit diff
path: root/synapse/http
diff options
context:
space:
mode:
Diffstat (limited to 'synapse/http')
-rw-r--r--synapse/http/servlet.py33
1 files changed, 31 insertions, 2 deletions
diff --git a/synapse/http/servlet.py b/synapse/http/servlet.py

index d61563d39b..89991e7127 100644 --- a/synapse/http/servlet.py +++ b/synapse/http/servlet.py
@@ -13,9 +13,8 @@ # limitations under the License. """ This module contains base REST classes for constructing REST servlets. """ - import logging -from typing import Dict, Iterable, List, Optional, overload +from typing import Dict, Iterable, List, Optional, Union, overload from typing_extensions import Literal @@ -201,6 +200,36 @@ def parse_string( args, name, default, required, allowed_values, encoding ) +def parse_list_from_args( + args: Dict[bytes, List[bytes]], + name: Union[bytes, str], + encoding: Optional[str] = "ascii", +): + """Parse and optionally decode a list of values from request query parameters. + + Args: + args: A dictionary of query parameters from a request. + name: The name of the query parameter to extract values from. If given as bytes, + will be decoded as "ascii". + encoding: An optional encoding that is used to decode each parameter value with. + + Raises: + KeyError: If the given `name` does not exist in `args`. + SynapseError: If an argument was not encoded with the specified `encoding`. + """ + if not isinstance(name, bytes): + name = name.encode("ascii") + args_list = args[name] + + if encoding: + # Decode each argument value + try: + args_list = [value.decode(encoding) for value in args_list] + except ValueError: + raise SynapseError(400, "Query parameter %r must be %s" % (name, encoding)) + + return args_list + def _parse_string_value( value: bytes,