Skip to content

Commit

Permalink
Merge branch 'fracek-fix-cert-path' into 0.9.4
Browse files Browse the repository at this point in the history
# Conflicts:
#	betfairlightweight/baseclient.py
  • Loading branch information
liampauling committed Jan 17, 2017
2 parents a3e057d + fd63cd3 commit 3282bc1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
4 changes: 2 additions & 2 deletions betfairlightweight/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class APIClient(BaseClient):

def __init__(self, username, password=None, app_key=None, certs=None, locale=None):
super(APIClient, self).__init__(username, password, app_key=app_key, certs=certs, locale=locale)
def __init__(self, username, password=None, app_key=None, certs=None, locale=None, cert_files=None):
super(APIClient, self).__init__(username, password, app_key=app_key, certs=certs, locale=locale, cert_files=cert_files)

self.login = endpoints.Login(self)
self.keep_alive = endpoints.KeepAlive(self)
Expand Down
34 changes: 23 additions & 11 deletions betfairlightweight/baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BaseClient(object):
italy='https://api.betfair.it/exchange/betting/rest/v1/en/navigation/menu.json'
)

def __init__(self, username, password=None, app_key=None, certs=None, locale=None):
def __init__(self, username, password=None, app_key=None, certs=None, locale=None, cert_files=None):
"""
:param username:
Betfair username.
Expand All @@ -40,12 +40,15 @@ def __init__(self, username, password=None, app_key=None, certs=None, locale=Non
Directory for certificates, if None will look in /certs/
:param locale:
Exchange to be used, defaults to UK for login and global for api.
:param cert_files:
Certificate and key files. If None will look in `certs`
"""
self.username = username
self.password = password
self.app_key = app_key
self.certs = certs
self.locale = locale
self.cert_files = cert_files

self.session = requests
self._login_time = None
Expand Down Expand Up @@ -100,26 +103,35 @@ def session_expired(self):

@property
def cert(self):
"""Looks in /certs/ for betfair certs.
"""The betfair certificates.
By default, it looks for the certificates in /certs/.
:return: Path of cert files
"""
if self.cert_files is not None:
return self.cert_files

certs = self.certs or '/certs/'
cert_paths = []
ssl_path = os.path.join(os.pardir, certs)
try:
cert_path = os.listdir(ssl_path)
except FileNotFoundError:
raise CertsError
raise CertsError(certs)
except OSError: # Python 2 compatability
raise CertsError
raise CertsError(certs)

cert = None
key = None
for file in cert_path:
ext = file.rpartition('.')[2]
if ext in ['key', 'crt', 'pem']:
cert_path = os.path.join(ssl_path, file)
cert_paths.append(cert_path)
cert_paths.sort()
return cert_paths
ext = os.path.splitext(file)[-1]
if ext in ['.crt', '.cert']:
cert = os.path.join(ssl_path, file)
elif ext == '.key':
key = os.path.join(ssl_path, file)
if cert is None or key is None:
raise CertsError(certs)
return [cert, key]

@property
def login_headers(self):
Expand Down
4 changes: 2 additions & 2 deletions betfairlightweight/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def __init__(self, username):
class CertsError(BetfairError):
"""Exception raised if certs folder is not found"""

def __init__(self):
message = 'Certificate folder not found in /certs/'
def __init__(self, path='/certs/'):
message = 'Certificate folder not found in %s' % path
super(CertsError, self).__init__(message)


Expand Down
27 changes: 27 additions & 0 deletions tests/test_baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def test_client_certs_mocked(self, mock_listdir):
mock_listdir.return_value = ['.DS_Store', 'client-2048.crt', 'client-2048.key']
assert self.client.cert == ['/fail/client-2048.crt', '/fail/client-2048.key']

@mock.patch('betfairlightweight.baseclient.os.listdir')
def test_client_certs_missing(self, mock_listdir):
mock_listdir.return_value = ['.DS_Store', 'client-2048.key']
with self.assertRaises(CertsError):
print(self.client.cert)

def test_set_session_token(self):
self.client.set_session_token('session_token')
assert self.client.session_token == 'session_token'
Expand Down Expand Up @@ -122,3 +128,24 @@ def test_client_logout(self):
self.client.client_logout()
assert self.client._login_time is None
assert self.client.session_token is None


class BaseClientRelativePathTest(unittest.TestCase):

def setUp(self):
self.client = APIClient('bf_username', 'password', 'app_key', 'fail/')

@mock.patch('betfairlightweight.baseclient.os.listdir')
def test_client_certs_mocked(self, mock_listdir):
mock_listdir.return_value = ['.DS_Store', 'client-2048.crt', 'client-2048.key']
assert self.client.cert == ['../fail/client-2048.crt', '../fail/client-2048.key']


class BaseClientCertFilesTest(unittest.TestCase):

def setUp(self):
self.client = APIClient('bf_username', 'password', 'app_key',
cert_files=['/fail/client-2048.crt', '/fail/client-2048.key'])

def test_client_cert_files(self):
assert self.client.cert == ['/fail/client-2048.crt', '/fail/client-2048.key']

0 comments on commit 3282bc1

Please sign in to comment.