Skip to content

Commit

Permalink
Merge 34a63eb into f370d11
Browse files Browse the repository at this point in the history
  • Loading branch information
liampauling committed Mar 9, 2021
2 parents f370d11 + 34a63eb commit 59985fd
Show file tree
Hide file tree
Showing 43 changed files with 95 additions and 42 deletions.
18 changes: 18 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
Release History
---------------

2.12.1 (2021-03-09)
+++++++++++++++++++

**Improvements**

- Allow single file (.pem) certificate (@beci)
- Tests directory cleanup

**Bug Fixes**

- #387 default total matched set to 0 (was None)
- #384 make regulator code optional in CurrentOrder
- #380 make name optional in scores

**Dependencies**

- orjson upgraded to 3.5.1

2.12.0 (2021-01-25)
+++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion betfairlightweight/__version__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = "betfairlightweight"
__description__ = "Lightweight python wrapper for Betfair API-NG"
__url__ = "https://github.com/liampauling/betfair"
__version__ = "2.12.0"
__version__ = "2.12.1"
__author__ = "Liam Pauling"
__license__ = "MIT"
7 changes: 5 additions & 2 deletions betfairlightweight/apiclient.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union, Tuple

import requests

from .baseclient import BaseClient
Expand All @@ -12,7 +14,7 @@ def __init__(
app_key: str = None,
certs: str = None,
locale: str = None,
cert_files: list = None,
cert_files: Union[Tuple[str], str, None] = None,
lightweight: bool = False,
session: requests.Session = None,
):
Expand All @@ -24,7 +26,8 @@ def __init__(
:param str app_key: App Key for account, if None will look in .bashprofile
:param str certs: Directory for certificates, if None will look in /certs
:param str locale: Exchange to be used, defaults to international (.com) exchange
:param list cert_files: Certificate and key files. If None will use `self.certs`
:param list cert_files: if String, path to ssl client cert file (.pem).
If Tuple, ('cert', 'key') path pair. If None will use `self.certs`
:param bool lightweight: If True endpoints will return dict not a resource (22x faster)
:param requests.Session session: Pass requests session object, defaults to a new request each request
"""
Expand Down
29 changes: 17 additions & 12 deletions betfairlightweight/baseclient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import time
from typing import Union, Tuple

import requests
import collections

Expand Down Expand Up @@ -54,7 +56,7 @@ def __init__(
app_key: str = None,
certs: str = None,
locale: str = None,
cert_files: list = None,
cert_files: Union[Tuple[str], str, None] = None,
lightweight: bool = False,
session: requests.Session = None,
):
Expand All @@ -66,7 +68,8 @@ def __init__(
:param str app_key: App Key for account, if None will look in .bashprofile
:param str certs: Directory for certificates, if None will look in /certs
:param str locale: Exchange to be used, defaults to international (.com) exchange
:param list cert_files: Certificate and key files. If None will use `self.certs`
:param list cert_files: if String, path to ssl client cert file (.pem).
If Tuple, ('cert', 'key') path pair. If None will use `self.certs`
:param bool lightweight: If True endpoints will return dict not a resource (22x faster)
:param requests.Session session: Pass requests session object, defaults to a new request each request
"""
Expand Down Expand Up @@ -144,12 +147,12 @@ def session_expired(self) -> bool:
return False

@property
def cert(self) -> list:
def cert(self) -> Union[Tuple[str], str]:
"""
The betfair certificates, by default it looks for the
certificates in /certs/.
:return: Path of cert files
:return: Tuple of cert files path or single path.
:rtype: str
"""
if self.cert_files is not None:
Expand All @@ -162,20 +165,22 @@ def cert(self) -> list:
except FileNotFoundError as e:
raise CertsError(str(e))

cert = None
key = None
cert, key, pem = None, None, None
for file in cert_path:
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(
"Certificates not found in directory: '%s' (make sure .crt and .key file is present)"
% ssl_path
)
return [cert, key]
elif ext == ".pem":
pem = os.path.join(ssl_path, file)
if cert and key:
return (cert, key)
if pem:
return pem
msg = "Certificates not found in directory: '%s'" % ssl_path
hint = " (make sure .crt and .key pair or a single .pem is present)"
raise CertsError(msg + hint)

@property
def login_headers(self) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion betfairlightweight/resources/bettingresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ def __init__(
orderType: str,
persistenceType: str,
placedDate: str,
regulatorCode: str,
selectionId: int,
side: str,
sizeCancelled: float,
Expand All @@ -644,6 +643,7 @@ def __init__(
sizeVoided: float,
status: str,
priceSize: dict,
regulatorCode: str = None,
matchedDate: str = None,
customerStrategyRef: str = None,
customerOrderRef: str = None,
Expand Down
2 changes: 1 addition & 1 deletion betfairlightweight/resources/inplayserviceresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(
penaltiesSequence,
halfTimeScore,
fullTimeScore,
name,
name=None,
score=None,
sets=None,
games=None,
Expand Down
4 changes: 2 additions & 2 deletions betfairlightweight/streaming/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(
id: int,
lightweight: bool,
ltp: float = None,
tv: float = None,
tv: float = 0,
trd: list = None,
atb: list = None,
batb: list = None,
Expand Down Expand Up @@ -194,7 +194,7 @@ def __init__(self, market_id: str, publish_time: int, lightweight: bool):
self.market_id = market_id
self.publish_time = publish_time
self.lightweight = lightweight
self.total_matched = None
self.total_matched = 0
self.market_definition = {}
self._market_definition_resource = None
self._definition_bet_delay = None
Expand Down
2 changes: 1 addition & 1 deletion requirements-speed.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ciso8601==2.1.3
orjson==3.4.7
orjson==3.5.1
Empty file removed tests/integration/__init__.py
Empty file.

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 29 additions & 2 deletions tests/unit/test_baseclient.py → tests/test_baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_client_certs(self):
@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"]
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):
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_client_logout(self):


def normpaths(p):
return list(map(os.path.normpath, p))
return tuple(map(os.path.normpath, p))


class BaseClientRelativePathTest(unittest.TestCase):
Expand All @@ -201,6 +201,20 @@ def test_client_certs_mocked(self, mock_listdir):
["../fail/client-2048.crt", "../fail/client-2048.key"]
)

@mock.patch("betfairlightweight.baseclient.os.listdir")
def test_client_single_file_cert_mocked(self, mock_listdir):
mock_listdir.return_value = normpaths([".DS_Store", "client-2048.pem"])
assert self.client.cert == os.path.normpath("../fail/client-2048.pem")

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


class BaseClientCertFilesTest(unittest.TestCase):
def setUp(self):
Expand All @@ -215,3 +229,16 @@ def test_client_cert_files(self):
assert self.client.cert == normpaths(
["/fail/client-2048.crt", "/fail/client-2048.key"]
)


class BaseClientSingleCertFileTest(unittest.TestCase):
def setUp(self):
self.client = APIClient(
"bf_username",
"password",
"app_key",
cert_files=os.path.normpath("/fail/client-2048.pem"),
)

def test_client_cert_files(self):
assert self.client.cert == os.path.normpath("/fail/client-2048.pem")
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from betfairlightweight import APIClient
from betfairlightweight.endpoints.baseendpoint import BaseEndpoint
from betfairlightweight.exceptions import APIError, InvalidResponse
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class BaseEndpointInit(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from betfairlightweight.compat import json
from betfairlightweight.resources.baseresource import BaseResource
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class BaseResourceInit(unittest.TestCase):
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/unit/test_betting.py → tests/test_betting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from betfairlightweight import resources
from betfairlightweight.endpoints.betting import Betting
from betfairlightweight.exceptions import APIError
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class BettingInit(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
PriceSize,
)
from betfairlightweight.compat import BETFAIR_DATE_FORMAT
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class BettingResourcesTest(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_cache.py → tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Available,
RaceCache,
)
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class TestAvailable(unittest.TestCase):
Expand Down Expand Up @@ -228,7 +228,7 @@ def test_init(self):
self.assertEqual(self.market_book_cache.market_id, "1.2345")
self.assertEqual(self.market_book_cache.publish_time, 12345)
self.assertTrue(self.market_book_cache.lightweight)
self.assertIsNone(self.market_book_cache.total_matched)
self.assertEqual(self.market_book_cache.total_matched, 0)
self.assertEqual(self.market_book_cache.market_definition, {})
self.assertIsNone(self.market_book_cache._market_definition_resource)
self.assertIsNone(self.market_book_cache._definition_bet_delay)
Expand Down Expand Up @@ -545,7 +545,7 @@ def test_serialise(self):
"nearPrice": None,
},
"status": "ACTIVE",
"totalMatched": None,
"totalMatched": 0,
},
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from betfairlightweight import resources
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class InPlayServiceTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_keepalive.py → tests/test_keepalive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from betfairlightweight.endpoints.keepalive import KeepAlive, APIError
from betfairlightweight.exceptions import KeepAliveError, InvalidResponse
from betfairlightweight.resources.authresources import KeepAliveResource
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class KeepAliveTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_listener.py → tests/test_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest import mock

from betfairlightweight.streaming.listener import BaseListener, StreamListener
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class BaseListenerTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_login.py → tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from betfairlightweight.endpoints.login import Login
from betfairlightweight.exceptions import LoginError, APIError, InvalidResponse
from betfairlightweight.resources import LoginResource
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class LoginTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from betfairlightweight.endpoints.logininteractive import LoginInteractive
from betfairlightweight.exceptions import LoginError, APIError, InvalidResponse
from betfairlightweight.resources import LoginResource
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class LoginInteractiveTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_logout.py → tests/test_logout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from betfairlightweight.endpoints.logout import Logout
from betfairlightweight.exceptions import LogoutError, APIError, InvalidResponse
from betfairlightweight.resources import LogoutResource
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class LogoutTest(unittest.TestCase):
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/unit/test_navigation.py → tests/test_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from betfairlightweight import APIClient
from betfairlightweight.endpoints.navigation import Navigation
from betfairlightweight.exceptions import APIError, InvalidResponse
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class NavigationInit(unittest.TestCase):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest

from betfairlightweight import resources
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class RaceCardResourcesTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_scores.py → tests/test_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from betfairlightweight import resources
from betfairlightweight.endpoints.scores import Scores
from betfairlightweight.exceptions import APIError
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class ScoresInit(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from betfairlightweight import resources
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class ScoreResourcesTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_stream.py → tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
RaceStream,
MAX_CACHE_AGE,
)
from tests.unit.tools import create_mock_json
from tests.tools import create_mock_json


class BaseStreamTest(unittest.TestCase):
Expand Down
File renamed without changes.

0 comments on commit 59985fd

Please sign in to comment.