From e0de58943cb77fd7f7c5bc1f8802dedb48fdfe84 Mon Sep 17 00:00:00 2001 From: Keng Kiat Lim Date: Sat, 23 Jan 2021 01:45:13 -0800 Subject: [PATCH 1/3] Updated requirements.txt --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 1d286529..2661511b 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,7 +11,7 @@ pymongo==3.7.2 pytest==4.0.1 python-dateutil==2.7.5 requests==2.20.1 -six==1.11.0 +six>=1.11.0 sortedcontainers==2.1.0 urllib3==1.24.2 websocket-client==0.54.0 From 1c5d2ea7716e30854c0d2f59fea6ceea7fb2c112 Mon Sep 17 00:00:00 2001 From: Keng Kiat Lim Date: Sun, 24 Jan 2021 16:06:44 -0800 Subject: [PATCH 2/3] Change to new style super() --- cbpro/authenticated_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbpro/authenticated_client.py b/cbpro/authenticated_client.py index f330377f..e7f1ffb7 100644 --- a/cbpro/authenticated_client.py +++ b/cbpro/authenticated_client.py @@ -36,7 +36,7 @@ def __init__(self, key, b64secret, passphrase, passphrase (str): Passphrase chosen when setting up key. api_url (Optional[str]): API URL. Defaults to cbpro API. """ - super(AuthenticatedClient, self).__init__(api_url) + super().__init__(api_url) self.auth = CBProAuth(key, b64secret, passphrase) self.session = requests.Session() From daf4c487d08473073b43dbc117e7fae9b52bae4a Mon Sep 17 00:00:00 2001 From: Mike He Date: Sat, 9 Apr 2022 11:41:12 -0700 Subject: [PATCH 3/3] add timeouts support see https://docs.python-requests.org/en/latest/user/advanced/#timeouts --- cbpro/authenticated_client.py | 5 +++-- cbpro/public_client.py | 11 +++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cbpro/authenticated_client.py b/cbpro/authenticated_client.py index e7f1ffb7..36d0a788 100644 --- a/cbpro/authenticated_client.py +++ b/cbpro/authenticated_client.py @@ -25,9 +25,10 @@ class AuthenticatedClient(PublicClient): url (str): The api url for this client instance to use. auth (CBProAuth): Custom authentication handler for each request. session (requests.Session): Persistent HTTP connection object. + timeout (int): the read timeout for requests, defualt to 30 seconds """ def __init__(self, key, b64secret, passphrase, - api_url="https://api.pro.coinbase.com"): + api_url="https://api.pro.coinbase.com", timeouts=(30,60)): """ Create an instance of the AuthenticatedClient class. Args: @@ -36,7 +37,7 @@ def __init__(self, key, b64secret, passphrase, passphrase (str): Passphrase chosen when setting up key. api_url (Optional[str]): API URL. Defaults to cbpro API. """ - super().__init__(api_url) + super().__init__(api_url, timeouts) self.auth = CBProAuth(key, b64secret, passphrase) self.session = requests.Session() diff --git a/cbpro/public_client.py b/cbpro/public_client.py index e854a163..fb5f7a26 100644 --- a/cbpro/public_client.py +++ b/cbpro/public_client.py @@ -15,10 +15,11 @@ class PublicClient(object): Attributes: url (Optional[str]): API URL. Defaults to cbpro API. - + timeouts (tuple[int,int]): connect and read timeout in seconds """ - def __init__(self, api_url='https://api.pro.coinbase.com', timeout=30): + def __init__(self, api_url='https://api.pro.coinbase.com', + timeouts=(30, 60)): """Create cbpro API public client. Args: @@ -28,6 +29,7 @@ def __init__(self, api_url='https://api.pro.coinbase.com', timeout=30): self.url = api_url.rstrip('/') self.auth = None self.session = requests.Session() + self.timeouts = timeouts def get_products(self): """Get a list of available currency pairs for trading. @@ -266,7 +268,7 @@ def _send_message(self, method, endpoint, params=None, data=None): """ url = self.url + endpoint r = self.session.request(method, url, params=params, data=data, - auth=self.auth, timeout=30) + auth=self.auth, timeout=self.timeouts) return r.json() def _send_paginated_message(self, endpoint, params=None): @@ -296,7 +298,8 @@ def _send_paginated_message(self, endpoint, params=None): params = dict() url = self.url + endpoint while True: - r = self.session.get(url, params=params, auth=self.auth, timeout=30) + r = self.session.get(url, params=params, auth=self.auth, + timeout=self.timeouts) results = r.json() for result in results: yield result