From 7dd6a4a3e08b7e800df5c389823d541aa1c9bd43 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Wed, 4 Oct 2017 14:18:00 +0200 Subject: [PATCH 01/24] add timeout to requests.request --- matrix_client/api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index b8431317..0b375249 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -560,6 +560,8 @@ 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) + + request_timeout = 10.0 + query_params.get("timeout", 30000) / 1000 response = None while True: @@ -568,7 +570,8 @@ 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 ) if response.status_code == 429: From 1d915e208a04fb170790bcba1e745e25817c5636 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Wed, 4 Oct 2017 14:25:46 +0200 Subject: [PATCH 02/24] handle requests timeout in listen_forever --- matrix_client/client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/matrix_client/client.py b/matrix_client/client.py index 9e7e6c05..c6371acd 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -365,6 +365,10 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): self.bad_sync_timeout_limit) else: raise e + except requests.exceptions.ReadTimeout as e: + logger.warning("A ReadTimeout exception occured during sync.") + bad_sync_timeout = min(bad_sync_timeout * 2, + self.bad_sync_timeout_limit) except Exception as e: logger.exception("Exception thrown during sync") if exception_handler is not None: From 98f9ce8d4acdb1c7a94ec54b728235d3867eac48 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Fri, 6 Oct 2017 13:01:56 +0200 Subject: [PATCH 03/24] import ReadTimeout from requests.exceptions --- matrix_client/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matrix_client/client.py b/matrix_client/client.py index c6371acd..6c12b33c 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -19,6 +19,7 @@ from threading import Thread from time import sleep from uuid import uuid4 +from requests.exceptions import ReadTimeout import logging import sys @@ -365,7 +366,7 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): self.bad_sync_timeout_limit) else: raise e - except requests.exceptions.ReadTimeout as e: + except ReadTimeout as e: logger.warning("A ReadTimeout exception occured during sync.") bad_sync_timeout = min(bad_sync_timeout * 2, self.bad_sync_timeout_limit) From 83b504791b142e222470959b7a205639240d9e8d Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Thu, 12 Oct 2017 11:22:35 +0200 Subject: [PATCH 04/24] Update client.py requests exception handling in listen_forever --- matrix_client/client.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/matrix_client/client.py b/matrix_client/client.py index 6c12b33c..a0a6a7a8 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -19,7 +19,7 @@ from threading import Thread from time import sleep from uuid import uuid4 -from requests.exceptions import ReadTimeout +import requests import logging import sys @@ -366,8 +366,12 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): self.bad_sync_timeout_limit) else: raise e - except ReadTimeout as e: - logger.warning("A ReadTimeout exception occured during sync.") + except requests.exceptions.ReadTimeout as e: + logger.warning("A ReadTimeout occured during sync.") + bad_sync_timeout = min(bad_sync_timeout * 2, + self.bad_sync_timeout_limit) + except requests.exceptions.ConnectionError as e: + logger.warning("A ConnectionError occured during sync.") bad_sync_timeout = min(bad_sync_timeout * 2, self.bad_sync_timeout_limit) except Exception as e: From 545ade3b1a3daba844975f0eb76ce200e3d8d486 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Mon, 16 Oct 2017 12:13:00 +0200 Subject: [PATCH 05/24] sync timeout correction bad_sync_timeout was initialized with 5000 (seconds), should be only 5 seconds. --- matrix_client/client.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/matrix_client/client.py b/matrix_client/client.py index ebdc6557..df0c76c1 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -350,7 +350,8 @@ 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 + sleep_bad_sync = False self.should_listen = True while (self.should_listen): try: @@ -361,25 +362,26 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): if e.code >= 500: logger.warning("Problem occured serverside. Waiting %i seconds", bad_sync_timeout) - sleep(bad_sync_timeout) - bad_sync_timeout = min(bad_sync_timeout * 2, - self.bad_sync_timeout_limit) + sleep_bad_sync_timeout = True else: raise e except requests.exceptions.ReadTimeout as e: logger.warning("A ReadTimeout occured during sync.") - bad_sync_timeout = min(bad_sync_timeout * 2, - self.bad_sync_timeout_limit) + sleep_bad_sync = True except requests.exceptions.ConnectionError as e: logger.warning("A ConnectionError occured during sync.") - bad_sync_timeout = min(bad_sync_timeout * 2, - self.bad_sync_timeout_limit) + sleep_bad_sync = True except Exception as e: logger.exception("Exception thrown during sync") if exception_handler is not None: exception_handler(e) else: raise + if sleep_bad_sync: + sleep(bad_sync_timeout) + bad_sync_timeout = min(bad_sync_timeout * 2, + self.bad_sync_timeout_limit) + sleep_bad_sync = False def start_listener_thread(self, timeout_ms=30000, exception_handler=None): """ Start a listener thread to listen for events in the background. From 8365a3757776254f52c2a4ee4d6fd50ea05a56c9 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Mon, 16 Oct 2017 12:31:00 +0200 Subject: [PATCH 06/24] Update client.py --- matrix_client/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matrix_client/client.py b/matrix_client/client.py index df0c76c1..6d53ddb2 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -360,9 +360,7 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): except MatrixRequestError as e: logger.warning("A MatrixRequestError occured during sync.") if e.code >= 500: - logger.warning("Problem occured serverside. Waiting %i seconds", - bad_sync_timeout) - sleep_bad_sync_timeout = True + sleep_bad_sync = True else: raise e except requests.exceptions.ReadTimeout as e: @@ -378,6 +376,8 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): else: raise if sleep_bad_sync: + logger.warning("Waiting %i seconds until next sync.", + bad_sync_timeout) sleep(bad_sync_timeout) bad_sync_timeout = min(bad_sync_timeout * 2, self.bad_sync_timeout_limit) From 456ed7fb963bc905bb62fc7d600a52cc45b7e8d9 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Tue, 17 Oct 2017 16:10:02 +0200 Subject: [PATCH 07/24] Update client.py --- matrix_client/client.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/matrix_client/client.py b/matrix_client/client.py index 6d53ddb2..24fd867b 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -19,7 +19,6 @@ from threading import Thread from time import sleep from uuid import uuid4 -import requests import logging import sys @@ -350,8 +349,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 = 5 - sleep_bad_sync = False + bad_sync_timeout = 5000 self.should_listen = True while (self.should_listen): try: @@ -360,28 +358,19 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): except MatrixRequestError as e: logger.warning("A MatrixRequestError occured during sync.") if e.code >= 500: - sleep_bad_sync = True + logger.warning("Problem occured serverside. Waiting %i seconds", + bad_sync_timeout) + sleep(bad_sync_timeout) + bad_sync_timeout = min(bad_sync_timeout * 2, + self.bad_sync_timeout_limit) else: raise e - except requests.exceptions.ReadTimeout as e: - logger.warning("A ReadTimeout occured during sync.") - sleep_bad_sync = True - except requests.exceptions.ConnectionError as e: - logger.warning("A ConnectionError occured during sync.") - sleep_bad_sync = True except Exception as e: logger.exception("Exception thrown during sync") if exception_handler is not None: exception_handler(e) else: raise - if sleep_bad_sync: - logger.warning("Waiting %i seconds until next sync.", - bad_sync_timeout) - sleep(bad_sync_timeout) - bad_sync_timeout = min(bad_sync_timeout * 2, - self.bad_sync_timeout_limit) - sleep_bad_sync = False def start_listener_thread(self, timeout_ms=30000, exception_handler=None): """ Start a listener thread to listen for events in the background. From 25ac7f2430d0f9e65db609b2587ce9aa67bf7037 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Tue, 17 Oct 2017 17:17:35 +0200 Subject: [PATCH 08/24] Update client.py listen_forever with new exception handler --- matrix_client/client.py | 42 ++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/matrix_client/client.py b/matrix_client/client.py index 6d53ddb2..305c5556 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -17,7 +17,7 @@ from .room import Room from .user import User from threading import Thread -from time import sleep +from time import time, sleep from uuid import uuid4 import requests import logging @@ -340,7 +340,8 @@ def listen_for_events(self, timeout_ms=30000): """ self._sync(timeout_ms) - def listen_forever(self, timeout_ms=30000, exception_handler=None): + def listen_forever(self, timeout_ms=30000, + exception_handler=None, exception_handler_kwargs=None): """ Keep listening for events forever. Args: @@ -349,32 +350,35 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None): exception_handler (func(exception)): Optional exception handler function which can be used to handle exceptions in the caller thread. + exception_handler_kwargs (func(**kwargs)): Optional exception handler + function, handles all exceptions if defined. + Possible kwargs: + exc: Exception + last_connection_time: time of the last successful connection + could return whether the the thread should sleep for a while """ - bad_sync_timeout = 5 + bad_sync_timeout = 5 + last_connection_time = time() sleep_bad_sync = False self.should_listen = True while (self.should_listen): try: self._sync(timeout_ms) bad_sync_timeout = 5 - except MatrixRequestError as e: - logger.warning("A MatrixRequestError occured during sync.") - if e.code >= 500: - sleep_bad_sync = True - else: - raise e - except requests.exceptions.ReadTimeout as e: - logger.warning("A ReadTimeout occured during sync.") - sleep_bad_sync = True - except requests.exceptions.ConnectionError as e: - logger.warning("A ConnectionError occured during sync.") - sleep_bad_sync = True + last_connection_time = time() except Exception as e: - logger.exception("Exception thrown during sync") - if exception_handler is not None: - exception_handler(e) + logger.warning("A exception occured during sync: {}".format(repr(e))) + if exception_handler_kwargs is not None: + sleep_bad_sync = exception_handler_kwargs(exc=e, + last_connection_time=last_connection_time) else: - raise + if isinstance(e, MatrixRequestError) and e.code >= 500: + logger.warning("Problem occured serverside") + sleep_bad_sync = True + elif exception_handler is not None: + exception_handler(e) + else: + raise e if sleep_bad_sync: logger.warning("Waiting %i seconds until next sync.", bad_sync_timeout) From 561e59a68b5e3f4c1d0c1642936229c8eded4e76 Mon Sep 17 00:00:00 2001 From: Niklas Tittjung Date: Mon, 23 Oct 2017 18:24:16 +0200 Subject: [PATCH 09/24] Adding MatrixTimeoutError Signed-off-by: Niklas Tittjung --- matrix_client/api.py | 23 ++++++++++++++--------- matrix_client/errors.py | 10 ++++++++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 0717ec69..749cce53 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -583,18 +583,23 @@ 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) - request_timeout = 10.0 + query_params.get("timeout", 30000) / 1000 + request_timeout = 30 + query_params.get("timeout", 30000) / 1000 response = None while True: - response = requests.request( - method, endpoint, - params=query_params, - data=content, - headers=headers, - verify=self.validate_cert, - timeout=request_timeout - ) + try: + response = requests.request( + method, endpoint, + params=query_params, + data=content, + headers=headers, + verify=self.validate_cert, + timeout=request_timeout + ) + except requests.exceptions.ReadTimeout: + raise MatrixTimeoutError( + "A timeout occured while waiting for response" + ) if response.status_code == 429: sleep(response.json()['retry_after_ms'] / 1000) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 10cd039f..4ac09689 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -4,16 +4,22 @@ 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) self.content = 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)) self.code = code self.content = content + +class MatrixTimeoutError(MatrixError): + """A timeout occured while waiting for an answer.""" + + def __init__(self, msg=""): + super(MatrixTimeoutError, self).__init__(msg) \ No newline at end of file From 1be8337706140b5faf9f6dc362f0c6423352c21f Mon Sep 17 00:00:00 2001 From: Niklas Tittjung Date: Mon, 23 Oct 2017 18:55:15 +0200 Subject: [PATCH 10/24] import MatrixTimeoutError Signed-off-by: Niklas Tittjung --- matrix_client/api.py | 2 +- matrix_client/errors.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 749cce53..90b738d5 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -16,7 +16,7 @@ import json import requests from time import time, sleep -from .errors import MatrixError, MatrixRequestError +from .errors import MatrixError, MatrixRequestError, MatrixTimeoutError try: from urllib import quote diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 4ac09689..a2d2b71b 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -18,8 +18,9 @@ def __init__(self, code=0, content=""): self.code = code self.content = content + class MatrixTimeoutError(MatrixError): """A timeout occured while waiting for an answer.""" def __init__(self, msg=""): - super(MatrixTimeoutError, self).__init__(msg) \ No newline at end of file + super(MatrixTimeoutError, self).__init__(msg) From eba1de25d2af1ba25e45d3efb558e688211eb80f Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Tue, 24 Oct 2017 10:40:53 +0200 Subject: [PATCH 11/24] Update client.py Just have to learn some github things... Sorry for changing this file temporary --- matrix_client/client.py | 49 ++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/matrix_client/client.py b/matrix_client/client.py index e9db6d85..24fd867b 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -17,7 +17,7 @@ from .room import Room from .user import User from threading import Thread -from time import time, sleep +from time import sleep from uuid import uuid4 import logging import sys @@ -339,8 +339,7 @@ def listen_for_events(self, timeout_ms=30000): """ self._sync(timeout_ms) - def listen_forever(self, timeout_ms=30000, - exception_handler=None, exception_handler_kwargs=None): + def listen_forever(self, timeout_ms=30000, exception_handler=None): """ Keep listening for events forever. Args: @@ -349,43 +348,29 @@ def listen_forever(self, timeout_ms=30000, exception_handler (func(exception)): Optional exception handler function which can be used to handle exceptions in the caller thread. - exception_handler_kwargs (func(**kwargs)): Optional exception handler - function, handles all exceptions if defined. - Possible kwargs: - exc: Exception - last_connection_time: time of the last successful connection - could return whether the the thread should sleep for a while """ - - bad_sync_timeout = 5 - last_connection_time = time() - sleep_bad_sync = False + bad_sync_timeout = 5000 self.should_listen = True while (self.should_listen): try: self._sync(timeout_ms) bad_sync_timeout = 5 - last_connection_time = time() + except MatrixRequestError as e: + logger.warning("A MatrixRequestError occured during sync.") + if e.code >= 500: + logger.warning("Problem occured serverside. Waiting %i seconds", + bad_sync_timeout) + sleep(bad_sync_timeout) + bad_sync_timeout = min(bad_sync_timeout * 2, + self.bad_sync_timeout_limit) + else: + raise e except Exception as e: - logger.warning("A exception occured during sync: {}".format(repr(e))) - if exception_handler_kwargs is not None: - sleep_bad_sync = exception_handler_kwargs(exc=e, - last_connection_time=last_connection_time) + logger.exception("Exception thrown during sync") + if exception_handler is not None: + exception_handler(e) else: - if isinstance(e, MatrixRequestError) and e.code >= 500: - logger.warning("Problem occured serverside") - sleep_bad_sync = True - elif exception_handler is not None: - exception_handler(e) - else: - raise e - if sleep_bad_sync: - logger.warning("Waiting %i seconds until next sync.", - bad_sync_timeout) - sleep(bad_sync_timeout) - bad_sync_timeout = min(bad_sync_timeout * 2, - self.bad_sync_timeout_limit) - sleep_bad_sync = False + raise def start_listener_thread(self, timeout_ms=30000, exception_handler=None): """ Start a listener thread to listen for events in the background. From 72148514b81f6851639923d658c826d57c3b7821 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Thu, 2 Nov 2017 16:58:26 +0100 Subject: [PATCH 12/24] restore api.py (temp) --- matrix_client/api.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 0717ec69..44b9584a 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -16,7 +16,7 @@ import json import requests from time import time, sleep -from .errors import MatrixError, MatrixRequestError +from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError try: from urllib import quote @@ -582,19 +582,19 @@ 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) - - request_timeout = 10.0 + query_params.get("timeout", 30000) / 1000 response = None while True: - response = requests.request( - method, endpoint, - params=query_params, - data=content, - headers=headers, - verify=self.validate_cert, - timeout=request_timeout - ) + try: + response = requests.request( + method, endpoint, + params=query_params, + data=content, + headers=headers, + verify=self.validate_cert + ) + except requests.exceptions.RequestException as e: + raise MatrixHttpLibError(e, method, endpoint) if response.status_code == 429: sleep(response.json()['retry_after_ms'] / 1000) From ef1621dbd9574b4f92221093a76cb5213e4fe29a Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Thu, 2 Nov 2017 17:42:12 +0100 Subject: [PATCH 13/24] Update errors.py --- matrix_client/errors.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 1662dd3d..9afe9c82 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -28,3 +28,11 @@ def __init__(self, original_exception, method, endpoint): "Something went wrong in {} requesting {}: {}".format(original_exception) ) self.original_exception = original_exception + +class MatrixTimeoutError(MatrixError): + """A timeout occured while waiting for a response.""" + + def __init__(self, content="", endpoint="", original_exception=None): + super(MatrixTimeoutError, self).__init__("{}, Endpoint: {}".format(content, endpoint)) + self.endpoint = endpoint + self.original_exception = original_exception From 9000528ced1795bd45611d63afce3c8e019813a8 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Thu, 2 Nov 2017 17:45:16 +0100 Subject: [PATCH 14/24] Update errors.py --- matrix_client/errors.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 9afe9c82..020b5d2c 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -32,7 +32,6 @@ def __init__(self, original_exception, method, endpoint): class MatrixTimeoutError(MatrixError): """A timeout occured while waiting for a response.""" - def __init__(self, content="", endpoint="", original_exception=None): + def __init__(self, original_exception=None, content="", endpoint=""): super(MatrixTimeoutError, self).__init__("{}, Endpoint: {}".format(content, endpoint)) - self.endpoint = endpoint self.original_exception = original_exception From 1115d0365bf945f9ff84a55e578d84d892793952 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Thu, 2 Nov 2017 17:46:48 +0100 Subject: [PATCH 15/24] Update api.py --- matrix_client/api.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 44b9584a..20a039d8 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -16,7 +16,7 @@ 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 +50,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): """ Deprecated. Use sync instead. @@ -582,6 +583,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: @@ -591,10 +597,18 @@ 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.RequestException as e: raise MatrixHttpLibError(e, method, endpoint) + + except requests.exceptions.Timeout as e: + raise MatrixTimeoutError( + original_exception=e, + content="A timeout occured while _send", + endpoint=endpoint + ) if response.status_code == 429: sleep(response.json()['retry_after_ms'] / 1000) From 1280a9939ed52a66b0213960dc8770db88c2a9db Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Thu, 2 Nov 2017 23:28:10 +0100 Subject: [PATCH 16/24] correct bad_sync_timeout --- matrix_client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix_client/client.py b/matrix_client/client.py index 376aecc9..817541be 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -372,7 +372,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: From c90d401032a3a918ce9afd95c4808c54ddab9c3e Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Thu, 2 Nov 2017 23:53:26 +0100 Subject: [PATCH 17/24] insert empty line --- matrix_client/errors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 020b5d2c..2918b636 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -28,7 +28,8 @@ def __init__(self, original_exception, method, endpoint): "Something went wrong in {} requesting {}: {}".format(original_exception) ) self.original_exception = original_exception - + + class MatrixTimeoutError(MatrixError): """A timeout occured while waiting for a response.""" From c7d57b9295bd018086121d8431df9cd320cc9e58 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Fri, 3 Nov 2017 10:24:57 +0100 Subject: [PATCH 18/24] update MatrixTimeoutError solved bug in MatrixHttpLibError --- matrix_client/errors.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 2918b636..71d46c92 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)) @@ -25,14 +25,16 @@ class MatrixHttpLibError(MatrixError): def __init__(self, original_exception, method, endpoint): super(MatrixHttpLibError, self).__init__( - "Something went wrong in {} requesting {}: {}".format(original_exception) + "Something went wrong in {} requesting {}: {}".format(method, endpoint, original_exception) ) self.original_exception = original_exception -class MatrixTimeoutError(MatrixError): - """A timeout occured while waiting for a response.""" +class MatrixTimeoutError(MatrixHttpLibError): + """Client-side timeout in a request.""" - def __init__(self, original_exception=None, content="", endpoint=""): - super(MatrixTimeoutError, self).__init__("{}, Endpoint: {}".format(content, endpoint)) + def __init__(self, original_exception, method, endpoint): + super(MatrixHttpLibError, self).__init__( + "Timeout in {} requesting {}: {}".format(method, endpoint, original_exception) + ) self.original_exception = original_exception From 0f88e26397d9bc085a5ba4f1400fe83e825271c8 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Fri, 3 Nov 2017 10:26:53 +0100 Subject: [PATCH 19/24] edit MatrixTimeoutError --- matrix_client/api.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 20a039d8..98a4e140 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -600,15 +600,12 @@ def _send(self, method, path, content=None, query_params={}, headers={}, 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) - - except requests.exceptions.Timeout as e: - raise MatrixTimeoutError( - original_exception=e, - content="A timeout occured while _send", - endpoint=endpoint - ) if response.status_code == 429: sleep(response.json()['retry_after_ms'] / 1000) From 73b4341f35da9db1296654948a6245be856d219e Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Fri, 3 Nov 2017 11:37:47 +0100 Subject: [PATCH 20/24] pep corrections --- matrix_client/api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 98a4e140..56cd88eb 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, MatrixTimeoutError +from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError, \ + MatrixTimeoutError try: from urllib import quote @@ -603,7 +604,7 @@ def _send(self, method, path, content=None, query_params={}, headers={}, except requests.exceptions.Timeout as e: raise MatrixTimeoutError(e, method, endpoint) - + except requests.exceptions.RequestException as e: raise MatrixHttpLibError(e, method, endpoint) From 81ea5bdae43736a0f49a38af08f21bf4e5994e8b Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Fri, 3 Nov 2017 11:49:32 +0100 Subject: [PATCH 21/24] pep corrections --- matrix_client/errors.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 71d46c92..af4f6550 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -25,14 +25,16 @@ class MatrixHttpLibError(MatrixError): def __init__(self, original_exception, method, endpoint): super(MatrixHttpLibError, self).__init__( - "Something went wrong in {} requesting {}: {}".format(method, endpoint, original_exception) + "Something went wrong in {} requesting {}: {}".format( + 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) From 93f573cd66d67fbd6c9a93abae6aff3c64438c5a Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Fri, 3 Nov 2017 11:56:34 +0100 Subject: [PATCH 22/24] Update api.py --- matrix_client/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 56cd88eb..78fbd497 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -16,7 +16,7 @@ import json import requests from time import time, sleep -from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError, \ +from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError, \ MatrixTimeoutError try: From 206e2a0edc8b2c0f83403ab7e74726aba7adb26f Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Fri, 3 Nov 2017 11:57:50 +0100 Subject: [PATCH 23/24] removed whitespace --- matrix_client/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index f0fdc26c..a68ce009 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -16,7 +16,7 @@ import json import requests from time import time, sleep -from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError, \ +from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError, \ MatrixTimeoutError try: From 5464f7a9b2efab5693fb1f63f43a0e9d79f3cdd2 Mon Sep 17 00:00:00 2001 From: lugino-emeritus Date: Fri, 3 Nov 2017 12:01:49 +0100 Subject: [PATCH 24/24] remove blank line Signed-off-by: Niklas Tittjung nik_t.01@web.de --- matrix_client/errors.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matrix_client/errors.py b/matrix_client/errors.py index 8ec1c2bd..af4f6550 100644 --- a/matrix_client/errors.py +++ b/matrix_client/errors.py @@ -6,7 +6,6 @@ class MatrixError(Exception): class MatrixUnexpectedResponse(MatrixError): """The home server gave an unexpected response.""" - def __init__(self, content=""): super(MatrixError, self).__init__(content) self.content = content