Skip to content

Commit

Permalink
Merge 568e67c into b8de319
Browse files Browse the repository at this point in the history
  • Loading branch information
liampauling committed Nov 27, 2020
2 parents b8de319 + 568e67c commit 40a6b82
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 50 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
27 changes: 14 additions & 13 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 @@ -315,6 +316,7 @@ def __init__(
rac: str = None,
rc: str = None,
lsrc: str = None,
cd: int = None,
**kwargs
):
self.bet_id = id
Expand All @@ -326,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 @@ -340,15 +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?

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 @@ -357,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 @@ -375,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")
2 changes: 1 addition & 1 deletion tests/resources/streaming_ocm_FULL_IMAGE.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"op":"ocm","id":3,"clk":"AMYkAJokAJAnAIk2ANYk","pt":1566480793554,"oc":[{"id":"1.161613698","orc":[{"fullImage":true,"id":7017905,"hc":8.5,"uo":[{"id":"175706685825","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]},{"fullImage":true,"id":7017905,"hc":7.5,"uo":[{"id":"175706685826","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]},{"fullImage":true,"id":7017905,"hc":6.5,"uo":[{"id":"175706685827","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]},{"fullImage":true,"id":7017905,"hc":5.5,"uo":[{"id":"175706685828","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]},{"fullImage":true,"id":1017905,"uo":[{"id":"175706685828","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]}]}]}
{"op":"ocm","id":3,"clk":"AMYkAJokAJAnAIk2ANYk","pt":1566480793554,"oc":[{"id":"1.161613698","orc":[{"fullImage":true,"id":7017905,"hc":8.5,"uo":[{"id":"175706685825","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"cd":1478546670000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]},{"fullImage":true,"id":7017905,"hc":7.5,"uo":[{"id":"175706685826","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]},{"fullImage":true,"id":7017905,"hc":6.5,"uo":[{"id":"175706685827","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]},{"fullImage":true,"id":7017905,"hc":5.5,"uo":[{"id":"175706685828","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]},{"fullImage":true,"id":1017905,"uo":[{"id":"175706685828","p":2,"s":2,"side":"B","status":"EC","pt":"L","ot":"L","pd":1566480793000,"md":1566480793000,"avp":2,"sm":2,"sr":0,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_LGA","rfo":"","rfs":""}],"mb":[[2,2]]}]}]}
2 changes: 1 addition & 1 deletion tests/resources/streaming_ocm_UPDATE.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"op":"ocm","id":12345,"clk":"AOkVAIMZALsQAKsQAO8M","pt":1478546671115,"oc":[{"id":"1.128126331","orc":[{"id":10895629,"uo":[{"id":"78996704480","p":1.02,"s":2,"side":"L","status":"E","pt":"L","ot":"L","pd":1478546670000,"sm":0,"sr":2,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_GGC","rfo":"","rfs":""}]}]}]}
{"op":"ocm","id":12345,"clk":"AOkVAIMZALsQAKsQAO8M","pt":1478546671115,"oc":[{"id":"1.128126331","orc":[{"id":10895629,"uo":[{"id":"78996704480","p":1.02,"s":2,"side":"L","status":"E","pt":"L","ot":"L","pd":1478546670000,"cd":1478546670001,"sm":0,"sr":2,"sl":0,"sc":0,"sv":0,"rac":"","rc":"REG_GGC","rfo":"","rfs":""}]}]}]}
67 changes: 32 additions & 35 deletions tests/unit/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ def setUp(self):
"lsrc": 17,
"error": "test",
"md": 4,
"cd": 18,
}
self.unmatched_order = UnmatchedOrder(**order)

Expand All @@ -538,40 +539,36 @@ def test_init(self):
assert self.unmatched_order.reference_strategy == 15
assert self.unmatched_order.lapsed_date == BaseResource.strip_datetime(16)
assert self.unmatched_order.lapse_status_reason_code == 17

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"
)
assert self.unmatched_order.cancelled_date == BaseResource.strip_datetime(18)

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 40a6b82

Please sign in to comment.