summary refs log tree commit diff
path: root/synapse/streams/config.py
diff options
context:
space:
mode:
authorKrombel <krombel@krombel.de>2018-07-13 21:40:14 +0200
committerKrombel <krombel@krombel.de>2018-07-13 21:40:14 +0200
commit32fd6910d0175f14d9be756ac2241da683af83bb (patch)
tree40ee591606c439c8309eb0aa25a01d1fef8f76d8 /synapse/streams/config.py
parentMerge pull request #3533 from matrix-org/rav/fix_federation_ratelimite_queue (diff)
downloadsynapse-32fd6910d0175f14d9be756ac2241da683af83bb.tar.xz
Use parse_{int,str} and assert from http.servlet
parse_integer and parse_string can take a request and raise errors
in case we have wrong or missing params.
This PR tries to use them more to deduplicate some code and make it
better readable
Diffstat (limited to 'synapse/streams/config.py')
-rw-r--r--synapse/streams/config.py29
1 files changed, 6 insertions, 23 deletions
diff --git a/synapse/streams/config.py b/synapse/streams/config.py
index 46ccbbda7d..22b0b830f7 100644
--- a/synapse/streams/config.py
+++ b/synapse/streams/config.py
@@ -16,6 +16,7 @@
 import logging
 
 from synapse.api.errors import SynapseError
+from synapse.http.servlet import parse_integer, parse_string
 from synapse.types import StreamToken
 
 logger = logging.getLogger(__name__)
@@ -56,23 +57,10 @@ class PaginationConfig(object):
     @classmethod
     def from_request(cls, request, raise_invalid_params=True,
                      default_limit=None):
-        def get_param(name, default=None):
-            lst = request.args.get(name, [])
-            if len(lst) > 1:
-                raise SynapseError(
-                    400, "%s must be specified only once" % (name,)
-                )
-            elif len(lst) == 1:
-                return lst[0]
-            else:
-                return default
-
-        direction = get_param("dir", 'f')
-        if direction not in ['f', 'b']:
-            raise SynapseError(400, "'dir' parameter is invalid.")
-
-        from_tok = get_param("from")
-        to_tok = get_param("to")
+        direction = parse_string(request, "dir", default='f', allowed_values=['f', 'b'])
+
+        from_tok = parse_string(request, "from")
+        to_tok = parse_string(request, "to")
 
         try:
             if from_tok == "END":
@@ -88,12 +76,7 @@ class PaginationConfig(object):
         except Exception:
             raise SynapseError(400, "'to' paramater is invalid")
 
-        limit = get_param("limit", None)
-        if limit is not None and not limit.isdigit():
-            raise SynapseError(400, "'limit' parameter must be an integer.")
-
-        if limit is None:
-            limit = default_limit
+        limit = parse_integer(request, "limit", default=default_limit)
 
         try:
             return PaginationConfig(from_tok, to_tok, direction, limit)