Skip to content

Commit

Permalink
utils create_date_string added
Browse files Browse the repository at this point in the history
missing vars added to order output
  • Loading branch information
liampauling committed Nov 27, 2020
1 parent ec1f319 commit 568e67c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 53 deletions.
6 changes: 6 additions & 0 deletions betfairlightweight/resources/bettingresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@ def __init__(
customerStrategyRef: str = None,
customerOrderRef: str = None,
regulatorAuthCode: str = None,
lapsedDate: str = None,
lapseStatusReasonCode: str = None,
cancelledDate: str = None,
):
self.bet_id = betId
self.average_price_matched = averagePriceMatched
Expand All @@ -671,6 +674,9 @@ def __init__(
self.customer_strategy_ref = customerStrategyRef
self.customer_order_ref = customerOrderRef
self.price_size = PriceSize(**priceSize)
self.lapsed_date = BaseResource.strip_datetime(lapsedDate)
self.lapse_status_reason_code = lapseStatusReasonCode
self.cancelled_date = BaseResource.strip_datetime(cancelledDate)


class CurrentOrders(BaseResource):
Expand Down
31 changes: 13 additions & 18 deletions betfairlightweight/streaming/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
StreamingStatus,
)
from ..exceptions import CacheError
from ..utils import create_date_string


class Available:
Expand Down Expand Up @@ -327,9 +328,9 @@ def __init__(
self.persistence_type = pt
self.order_type = ot
self.placed_date = BaseResource.strip_datetime(pd)
self.placed_date_string = self.create_placed_date_string()
self._placed_date_string = create_date_string(self.placed_date)
self.matched_date = BaseResource.strip_datetime(md)
self.matched_date_string = self.create_matched_date_string()
self._matched_date_string = create_date_string(self.matched_date)
self.average_price_matched = avp
self.size_matched = sm
self.size_remaining = sr
Expand All @@ -341,20 +342,10 @@ def __init__(
self.reference_order = rfo
self.reference_strategy = rfs
self.lapsed_date = BaseResource.strip_datetime(ld)
self.lapse_status_reason_code = (
lsrc # todo add to output? + CurrentOrder resource
)
self.cancelled_date = BaseResource.strip_datetime(
cd
) # todo add to output? + CurrentOrder resource

def create_placed_date_string(self) -> str:
if self.placed_date:
return self.placed_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ")

def create_matched_date_string(self) -> str:
if self.matched_date:
return self.matched_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
self._lapsed_date_string = create_date_string(self.lapsed_date)
self.lapse_status_reason_code = lsrc
self.cancelled_date = BaseResource.strip_datetime(cd)
self._cancelled_date_string = create_date_string(self.cancelled_date)

def serialise(self, market_id: str, selection_id: int, handicap: int) -> dict:
return {
Expand All @@ -363,13 +354,14 @@ def serialise(self, market_id: str, selection_id: int, handicap: int) -> dict:
"bspLiability": self.bsp_liability,
"handicap": handicap,
"marketId": market_id,
"matchedDate": self.matched_date_string,
"matchedDate": self._matched_date_string,
"orderType": StreamingOrderType[self.order_type].value,
"persistenceType": StreamingPersistenceType[self.persistence_type].value
if self.persistence_type
else None,
"placedDate": self.placed_date_string,
"placedDate": self._placed_date_string,
"priceSize": {"price": self.price, "size": self.size},
"regulatorAuthCode": self.regulator_auth_code,
"regulatorCode": self.regulator_code,
"selectionId": selection_id,
"side": StreamingSide[self.side].value,
Expand All @@ -381,6 +373,9 @@ def serialise(self, market_id: str, selection_id: int, handicap: int) -> dict:
"status": StreamingStatus[self.status].value,
"customerStrategyRef": self.reference_strategy,
"customerOrderRef": self.reference_order,
"lapsedDate": self._lapsed_date_string,
"lapseStatusReasonCode": self.lapse_status_reason_code,
"cancelledDate": self._cancelled_date_string,
}


Expand Down
11 changes: 11 additions & 0 deletions betfairlightweight/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import requests
import datetime
from typing import Optional

from .exceptions import StatusCodeError
from .__version__ import __title__, __version__
Expand Down Expand Up @@ -62,3 +64,12 @@ def to_camel_case(snake_str: str) -> str:

def default_user_agent():
return "{0}/{1}".format(__title__, __version__)


def create_date_string(date: datetime.datetime) -> Optional[str]:
"""
Convert datetime to betfair
date string.
"""
if date:
return date.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
65 changes: 30 additions & 35 deletions tests/unit/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,39 +541,34 @@ def test_init(self):
assert self.unmatched_order.lapse_status_reason_code == 17
assert self.unmatched_order.cancelled_date == BaseResource.strip_datetime(18)

def test_placed_date_string(self):
now = BaseResource.strip_datetime(8)
assert self.unmatched_order.placed_date_string == now.strftime(
"%Y-%m-%dT%H:%M:%S.%fZ"
)

def test_matched_date_string(self):
now = BaseResource.strip_datetime(4)
assert self.unmatched_order.matched_date_string == now.strftime(
"%Y-%m-%dT%H:%M:%S.%fZ"
)

def test_serialise(self):
assert self.unmatched_order.serialise("1.23", 12345, 0.0) == {
"sizeLapsed": 11,
"persistenceType": "LAPSE",
"sizeRemaining": 10,
"placedDate": "1970-01-01T00:00:00.008000Z",
"sizeVoided": 13,
"sizeCancelled": 12,
"betId": 1,
"customerOrderRef": 14,
"orderType": "LIMIT",
"marketId": "1.23",
"side": "LAY",
"selectionId": 12345,
"bspLiability": None,
"sizeMatched": 9,
"handicap": 0.0,
"averagePriceMatched": 0.0,
"status": "EXECUTABLE",
"customerStrategyRef": 15,
"regulatorCode": None,
"priceSize": {"price": 2, "size": 3},
"matchedDate": "1970-01-01T00:00:00.004000Z",
}
self.assertEqual(
self.unmatched_order.serialise("1.23", 12345, 0.0),
{
"sizeLapsed": 11,
"persistenceType": "LAPSE",
"sizeRemaining": 10,
"placedDate": "1970-01-01T00:00:00.008000Z",
"sizeVoided": 13,
"sizeCancelled": 12,
"betId": 1,
"customerOrderRef": 14,
"orderType": "LIMIT",
"marketId": "1.23",
"side": "LAY",
"selectionId": 12345,
"bspLiability": None,
"sizeMatched": 9,
"handicap": 0.0,
"averagePriceMatched": 0.0,
"status": "EXECUTABLE",
"customerStrategyRef": 15,
"regulatorCode": None,
"regulatorAuthCode": None,
"priceSize": {"price": 2, "size": 3},
"matchedDate": "1970-01-01T00:00:00.004000Z",
"lapsedDate": "1970-01-01T00:00:00.016000Z",
"lapseStatusReasonCode": 17,
"cancelledDate": "1970-01-01T00:00:00.018000Z",
},
)
8 changes: 8 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import datetime
from unittest import mock

from betfairlightweight import utils
Expand Down Expand Up @@ -31,3 +32,10 @@ def test_convert_to_camel_case(self):

def test_default_user_agent(self):
assert utils.default_user_agent()

def test_create_date_string(self):
self.assertIsNone(utils.create_date_string(None))
self.assertEqual(
utils.create_date_string(datetime.datetime(2020, 11, 27)),
"2020-11-27T00:00:00.000000Z",
)

0 comments on commit 568e67c

Please sign in to comment.