Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout parameter for auth client #466

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions cbpro/authenticated_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(AuthenticatedClient, self).__init__(api_url)
super().__init__(api_url, timeouts)
self.auth = CBProAuth(key, b64secret, passphrase)
self.session = requests.Session()

Expand Down
11 changes: 7 additions & 4 deletions cbpro/public_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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