diff --git a/matrix_client/api.py b/matrix_client/api.py index 6c17f878..ee4e95fd 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -16,7 +16,8 @@ import json import requests from time import time, sleep -from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError +from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError, \ + MatrixTimeoutError try: from urllib import quote @@ -50,6 +51,7 @@ def __init__(self, base_url, token=None, identity=None): self.identity = identity self.txn_id = 0 self.validate_cert = True + self.default_timeout = 30 def initial_sync(self, limit=1): """ @@ -645,6 +647,11 @@ def _send(self, method, path, content=None, query_params={}, headers={}, if headers["Content-Type"] == "application/json" and content is not None: content = json.dumps(content) + if "timeout" in query_params: + request_timeout = 5 + query_params["timeout"] / 1000 + else: + request_timeout = self.default_timeout + response = None while True: try: @@ -653,8 +660,13 @@ def _send(self, method, path, content=None, query_params={}, headers={}, params=query_params, data=content, headers=headers, - verify=self.validate_cert + verify=self.validate_cert, + timeout=request_timeout ) + + except requests.exceptions.Timeout as e: + raise MatrixTimeoutError(e, method, endpoint) + except requests.exceptions.RequestException as e: raise MatrixHttpLibError(e, method, endpoint) diff --git a/matrix_client/client.py b/matrix_client/client.py index 3ea1dc03..3ab0c977 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -410,7 +410,7 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): function which can be used to handle exceptions in the caller thread. """ - bad_sync_timeout = 5000 + bad_sync_timeout = 5 self.should_listen = True while (self.should_listen): try: diff --git a/matrix_client/errors.py b/matrix_client/errors.py index ea604304..ee88812a 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -4,7 +4,7 @@ class MatrixError(Exception): class MatrixUnexpectedResponse(MatrixError): - """The home server gave an unexpected response. """ + """The home server gave an unexpected response.""" def __init__(self, content=""): super(MatrixError, self).__init__(content) @@ -12,7 +12,7 @@ def __init__(self, content=""): class MatrixRequestError(MatrixError): - """ The home server returned an error response. """ + """The home server returned an error response.""" def __init__(self, code=0, content=""): super(MatrixRequestError, self).__init__("%d: %s" % (code, content)) @@ -30,3 +30,15 @@ def __init__(self, original_exception, method, endpoint): original_exception) ) self.original_exception = original_exception + + +class MatrixTimeoutError(MatrixHttpLibError): + """Client-side timeout in a request.""" + + def __init__(self, original_exception, method, endpoint): + super(MatrixHttpLibError, self).__init__( + "Timeout in {} requesting {}: {}".format(method, + endpoint, + original_exception) + ) + self.original_exception = original_exception