diff --git a/matrix_client/api.py b/matrix_client/api.py index 6c17f878..c4fca94d 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -14,7 +14,7 @@ # limitations under the License. import json -import requests +from requests import Session, RequestException from time import time, sleep from .errors import MatrixError, MatrixRequestError, MatrixHttpLibError @@ -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.session = Session() def initial_sync(self, limit=1): """ @@ -645,17 +646,16 @@ 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) - response = None while True: try: - response = requests.request( + response = self.session.request( method, endpoint, params=query_params, data=content, headers=headers, verify=self.validate_cert ) - except requests.exceptions.RequestException as e: + except RequestException as e: raise MatrixHttpLibError(e, method, endpoint) if response.status_code == 429: