Skip to content

Commit

Permalink
Merge ce0dfe8 into ec520f0
Browse files Browse the repository at this point in the history
  • Loading branch information
liampauling committed Oct 14, 2019
2 parents ec520f0 + ce0dfe8 commit da23b2c
Show file tree
Hide file tree
Showing 77 changed files with 3,867 additions and 2,561 deletions.
33 changes: 22 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@ language: python

sudo: false

python:
- "3.5"
- "3.6"
# Enable 3.7 without globally enabling sudo and dist: xenial for other build jobs
matrix:
include:
- python: "3.7"
dist: xenial

install:
- travis_retry python setup.py install
- pip install coveralls

script:
coverage run --source=betfairlightweight setup.py test
jobs:
include:
- name: "Tests: python 3.5"
python: 3.5
script:
- coverage run --source=betfairlightweight setup.py test
- name: "Tests: python 3.6"
python: 3.6
script:
- coverage run --source=betfairlightweight setup.py test
- name: "Tests: python 3.7"
python: 3.7
dist: xenial
script:
- coverage run --source=betfairlightweight setup.py test
- name: "Lint: python 3.7"
python: 3.7
dist: xenial
install:
- pip install black
script:
- black --check --diff .

after_success:
coveralls
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Release History
**Improvements**

- async removed from streaming (force user to handle thread)
- Black formatting on all files

**Bug Fixes**

Expand Down
6 changes: 3 additions & 3 deletions betfairlightweight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from .streaming import StreamListener
from . import filters

__title__ = 'betfairlightweight'
__version__ = '2.0.0b'
__author__ = 'Liam Pauling'
__title__ = "betfairlightweight"
__version__ = "2.0.0b"
__author__ = "Liam Pauling"

# Set default logging handler to avoid "No handler found" warnings.
logging.getLogger(__name__).addHandler(logging.NullHandler())
26 changes: 19 additions & 7 deletions betfairlightweight/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@


class APIClient(BaseClient):

def __init__(self, username, password=None, app_key=None, certs=None, locale=None, cert_files=None,
lightweight=False):
def __init__(
self,
username,
password=None,
app_key=None,
certs=None,
locale=None,
cert_files=None,
lightweight=False,
):
"""
Creates API client for API operations.
Expand All @@ -18,8 +25,13 @@ def __init__(self, username, password=None, app_key=None, certs=None, locale=Non
:param bool lightweight: If True endpoints will return dict not a resource (22x faster)
"""
super(APIClient, self).__init__(
username, password, app_key=app_key, certs=certs, locale=locale, cert_files=cert_files,
lightweight=lightweight
username,
password,
app_key=app_key,
certs=certs,
locale=locale,
cert_files=cert_files,
lightweight=lightweight,
)

self.login = endpoints.Login(self)
Expand All @@ -36,7 +48,7 @@ def __init__(self, username, password=None, app_key=None, certs=None, locale=Non
self.historic = endpoints.Historic(self)

def __repr__(self):
return '<APIClient [%s]>' % self.username
return "<APIClient [%s]>" % self.username

def __str__(self):
return 'APIClient'
return "APIClient"
91 changes: 48 additions & 43 deletions betfairlightweight/baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import datetime
import os

from .exceptions import (
PasswordError,
AppKeyError,
CertsError,
)
from .exceptions import PasswordError, AppKeyError, CertsError


class BaseClient(object):
Expand All @@ -16,34 +12,40 @@ class BaseClient(object):
"""

IDENTITY_URLS = collections.defaultdict(
lambda: 'https://identitysso.betfair.com/api/',
spain='https://identitysso.betfair.es/api/',
italy='https://identitysso.betfair.it/api/',
romania='https://identitysso.betfair.ro/api/',
sweden='https://identitysso.betfair.se/api/',
australia='https://identitysso.betfair.au/api/',
lambda: "https://identitysso.betfair.com/api/",
spain="https://identitysso.betfair.es/api/",
italy="https://identitysso.betfair.it/api/",
romania="https://identitysso.betfair.ro/api/",
sweden="https://identitysso.betfair.se/api/",
australia="https://identitysso.betfair.au/api/",
)

IDENTITY_CERT_URLS = collections.defaultdict(
lambda: 'https://identitysso-cert.betfair.com/api/',
spain='https://identitysso-cert.betfair.es/api/',
italy='https://identitysso-cert.betfair.it/api/',
romania='https://identitysso-cert.betfair.ro/api/',
sweden='https://identitysso-cert.betfair.se/api/',
lambda: "https://identitysso-cert.betfair.com/api/",
spain="https://identitysso-cert.betfair.es/api/",
italy="https://identitysso-cert.betfair.it/api/",
romania="https://identitysso-cert.betfair.ro/api/",
sweden="https://identitysso-cert.betfair.se/api/",
)

API_URLS = collections.defaultdict(
lambda: 'https://api.betfair.com/exchange/'
)
API_URLS = collections.defaultdict(lambda: "https://api.betfair.com/exchange/")

NAVIGATION_URLS = collections.defaultdict(
lambda: 'https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json',
spain='https://api.betfair.es/exchange/betting/rest/v1/en/navigation/menu.json',
italy='https://api.betfair.it/exchange/betting/rest/v1/en/navigation/menu.json'
lambda: "https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json",
spain="https://api.betfair.es/exchange/betting/rest/v1/en/navigation/menu.json",
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, cert_files=None,
lightweight=False):
def __init__(
self,
username,
password=None,
app_key=None,
certs=None,
locale=None,
cert_files=None,
lightweight=False,
):
"""
Creates base client for API operations.
Expand Down Expand Up @@ -89,8 +91,8 @@ def get_password(self):
for username+'password'.
"""
if self.password is None:
if os.environ.get(self.username+'password'):
self.password = os.environ.get(self.username+'password')
if os.environ.get(self.username + "password"):
self.password = os.environ.get(self.username + "password")
else:
raise PasswordError(self.username)

Expand Down Expand Up @@ -118,7 +120,10 @@ def session_expired(self):
Returns True if login_time not set or seconds since
login time is greater than 200 mins.
"""
if not self._login_time or (datetime.datetime.now()-self._login_time).total_seconds() > 12000:
if (
not self._login_time
or (datetime.datetime.now() - self._login_time).total_seconds() > 12000
):
return True

@property
Expand All @@ -133,7 +138,7 @@ def cert(self):
if self.cert_files is not None:
return self.cert_files

certs = self.certs or '/certs/'
certs = self.certs or "/certs/"
ssl_path = os.path.join(os.pardir, certs)
try:
cert_path = os.listdir(ssl_path)
Expand All @@ -144,9 +149,9 @@ def cert(self):
key = None
for file in cert_path:
ext = os.path.splitext(file)[-1]
if ext in ['.crt', '.cert']:
if ext in [".crt", ".cert"]:
cert = os.path.join(ssl_path, file)
elif ext == '.key':
elif ext == ".key":
key = os.path.join(ssl_path, file)
if cert is None or key is None:
raise CertsError(certs)
Expand All @@ -155,27 +160,27 @@ def cert(self):
@property
def login_headers(self):
return {
'Accept': 'application/json',
'X-Application': self.app_key,
'content-type': 'application/x-www-form-urlencoded'
"Accept": "application/json",
"X-Application": self.app_key,
"content-type": "application/x-www-form-urlencoded",
}

@property
def keep_alive_headers(self):
return {
'Accept': 'application/json',
'X-Application': self.app_key,
'X-Authentication': self.session_token,
'content-type': 'application/x-www-form-urlencoded'
"Accept": "application/json",
"X-Application": self.app_key,
"X-Authentication": self.session_token,
"content-type": "application/x-www-form-urlencoded",
}

@property
def request_headers(self):
return {
'X-Application': self.app_key,
'X-Authentication': self.session_token,
'content-type': 'application/json',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
'User-Agent': 'betfairlightweight',
"X-Application": self.app_key,
"X-Authentication": self.session_token,
"content-type": "application/json",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"User-Agent": "betfairlightweight",
}
6 changes: 6 additions & 0 deletions betfairlightweight/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

def json_loads(s, **kwargs):
return json.loads(s, precise_float=True, **kwargs)


except ImportError:
import json

def json_loads(s, **kwargs):
return json.loads(s, **kwargs)


try:
import ciso8601

Expand All @@ -25,7 +28,10 @@ def parse_datetime(datetime_string):
return ciso8601.parse_datetime_as_naive(datetime_string)
except ValueError:
return


except ImportError:

def parse_datetime(datetime_string):
try:
return datetime.datetime.strptime(datetime_string, "%Y-%m-%dT%H:%M:%S.%fZ")
Expand Down
48 changes: 33 additions & 15 deletions betfairlightweight/endpoints/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Account(BaseEndpoint):
Account operations.
"""

URI = 'AccountAPING/v1.0/'
URI = "AccountAPING/v1.0/"
connect_timeout = 6.05

def get_account_funds(self, wallet=None, session=None, lightweight=None):
Expand All @@ -23,9 +23,11 @@ def get_account_funds(self, wallet=None, session=None, lightweight=None):
:rtype: resources.AccountFunds
"""
params = clean_locals(locals())
method = '%s%s' % (self.URI, 'getAccountFunds')
method = "%s%s" % (self.URI, "getAccountFunds")
(response, elapsed_time) = self.request(method, params, session)
return self.process_response(response, resources.AccountFunds, elapsed_time, lightweight)
return self.process_response(
response, resources.AccountFunds, elapsed_time, lightweight
)

def get_account_details(self, session=None, lightweight=None):
"""
Expand All @@ -38,12 +40,23 @@ def get_account_details(self, session=None, lightweight=None):
:rtype: resources.AccountDetails
"""
params = clean_locals(locals())
method = '%s%s' % (self.URI, 'getAccountDetails')
method = "%s%s" % (self.URI, "getAccountDetails")
(response, elapsed_time) = self.request(method, params, session)
return self.process_response(response, resources.AccountDetails, elapsed_time, lightweight)

def get_account_statement(self, locale=None, from_record=None, record_count=None, item_date_range=time_range(),
include_item=None, wallet=None, session=None, lightweight=None):
return self.process_response(
response, resources.AccountDetails, elapsed_time, lightweight
)

def get_account_statement(
self,
locale=None,
from_record=None,
record_count=None,
item_date_range=time_range(),
include_item=None,
wallet=None,
session=None,
lightweight=None,
):
"""
Get account statement.
Expand All @@ -59,9 +72,11 @@ def get_account_statement(self, locale=None, from_record=None, record_count=None
:rtype: resources.AccountStatementResult
"""
params = clean_locals(locals())
method = '%s%s' % (self.URI, 'getAccountStatement')
method = "%s%s" % (self.URI, "getAccountStatement")
(response, elapsed_time) = self.request(method, params, session)
return self.process_response(response, resources.AccountStatementResult, elapsed_time, lightweight)
return self.process_response(
response, resources.AccountStatementResult, elapsed_time, lightweight
)

def list_currency_rates(self, from_currency=None, session=None, lightweight=None):
"""
Expand All @@ -74,9 +89,11 @@ def list_currency_rates(self, from_currency=None, session=None, lightweight=None
:rtype: list[resources.CurrencyRate]
"""
params = clean_locals(locals())
method = '%s%s' % (self.URI, 'listCurrencyRates')
method = "%s%s" % (self.URI, "listCurrencyRates")
(response, elapsed_time) = self.request(method, params, session)
return self.process_response(response, resources.CurrencyRate, elapsed_time, lightweight)
return self.process_response(
response, resources.CurrencyRate, elapsed_time, lightweight
)

def transfer_funds(self, session=None):
"""
Expand All @@ -87,9 +104,10 @@ def transfer_funds(self, session=None):
:rtype: resources.TransferFunds
"""
raise DeprecationWarning(
'As of 20/09/2016 AUS wallet has been removed, function still available for when '
'accounts are added in 2017.')
"As of 20/09/2016 AUS wallet has been removed, function still available for when "
"accounts are added in 2017."
)

@property
def url(self):
return '%s%s' % (self.client.api_uri, 'account/json-rpc/v1')
return "%s%s" % (self.client.api_uri, "account/json-rpc/v1")
Loading

0 comments on commit da23b2c

Please sign in to comment.