From 36578b9accecce91d8e21f3285faea176f19536e Mon Sep 17 00:00:00 2001 From: davidMkCb <159589283+davidMkCb@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:34:27 -0700 Subject: [PATCH] v1.2.1 (#38) * Release v1.2.1 * Update CHANGELOG.md --- CHANGELOG.md | 9 +++++++ coinbase/__version__.py | 2 +- coinbase/rest/orders.py | 38 ++++++++++++++++++++++++++++ coinbase/rest/rest_base.py | 3 ++- coinbase/websocket/websocket_base.py | 1 - tests/rest/test_orders.py | 2 ++ 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e67649..9560e84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## [1.2.1] - 2024-MAR-27 + +### Added +- `retail_portfolio_id` to all `preview_order` methods + +### Changed +- Requests now made via request.Sessions() to reduce latency by reusing existing HTTP connection +- Timestamp no longer needed for websocket signing + ## [1.2.0] - 2024-MAR-11 ### Added diff --git a/coinbase/__version__.py b/coinbase/__version__.py index c68196d..a955fda 100644 --- a/coinbase/__version__.py +++ b/coinbase/__version__.py @@ -1 +1 @@ -__version__ = "1.2.0" +__version__ = "1.2.1" diff --git a/coinbase/rest/orders.py b/coinbase/rest/orders.py index 33163e7..f3d5040 100644 --- a/coinbase/rest/orders.py +++ b/coinbase/rest/orders.py @@ -1171,6 +1171,7 @@ def preview_order( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1205,6 +1206,7 @@ def preview_order( "skip_fcm_risk_check": skip_fcm_risk_check, "leverage": leverage, "margin_type": margin_type, + "retail_portfolio_id": retail_portfolio_id, } return self.post(endpoint, data=data, **kwargs) @@ -1223,6 +1225,7 @@ def preview_market_order( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1261,6 +1264,7 @@ def preview_market_order( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1275,6 +1279,7 @@ def preview_market_order_buy( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1305,6 +1310,7 @@ def preview_market_order_buy( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1319,6 +1325,7 @@ def preview_market_order_sell( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1349,6 +1356,7 @@ def preview_market_order_sell( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1366,6 +1374,7 @@ def preview_limit_order_ioc( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1400,6 +1409,7 @@ def preview_limit_order_ioc( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1415,6 +1425,7 @@ def preview_limit_order_ioc_buy( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1446,6 +1457,7 @@ def preview_limit_order_ioc_buy( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1461,6 +1473,7 @@ def preview_limit_order_ioc_sell( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1492,6 +1505,7 @@ def preview_limit_order_ioc_sell( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1510,6 +1524,7 @@ def preview_limit_order_gtc( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1548,6 +1563,7 @@ def preview_limit_order_gtc( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1564,6 +1580,7 @@ def preview_limit_order_gtc_buy( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1596,6 +1613,7 @@ def preview_limit_order_gtc_buy( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1612,6 +1630,7 @@ def preview_limit_order_gtc_sell( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1644,6 +1663,7 @@ def preview_limit_order_gtc_sell( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1663,6 +1683,7 @@ def preview_limit_order_gtd( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1702,6 +1723,7 @@ def preview_limit_order_gtd( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1719,6 +1741,7 @@ def preview_limit_order_gtd_buy( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1752,6 +1775,7 @@ def preview_limit_order_gtd_buy( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1769,6 +1793,7 @@ def preview_limit_order_gtd_sell( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1802,6 +1827,7 @@ def preview_limit_order_gtd_sell( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1821,6 +1847,7 @@ def preview_stop_limit_order_gtc( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1860,6 +1887,7 @@ def preview_stop_limit_order_gtc( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1877,6 +1905,7 @@ def preview_stop_limit_order_gtc_buy( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1910,6 +1939,7 @@ def preview_stop_limit_order_gtc_buy( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1927,6 +1957,7 @@ def preview_stop_limit_order_gtc_sell( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -1960,6 +1991,7 @@ def preview_stop_limit_order_gtc_sell( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -1980,6 +2012,7 @@ def preview_stop_limit_order_gtd( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -2020,6 +2053,7 @@ def preview_stop_limit_order_gtd( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -2038,6 +2072,7 @@ def preview_stop_limit_order_gtd_buy( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -2072,6 +2107,7 @@ def preview_stop_limit_order_gtd_buy( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) @@ -2090,6 +2126,7 @@ def preview_stop_limit_order_gtd_sell( skip_fcm_risk_check: Optional[bool] = False, leverage: Optional[str] = None, margin_type: Optional[str] = None, + retail_portfolio_id: Optional[str] = None, **kwargs, ) -> Dict[str, Any]: """ @@ -2124,5 +2161,6 @@ def preview_stop_limit_order_gtd_sell( skip_fcm_risk_check=skip_fcm_risk_check, leverage=leverage, margin_type=margin_type, + retail_portfolio_id=retail_portfolio_id, **kwargs, ) diff --git a/coinbase/rest/rest_base.py b/coinbase/rest/rest_base.py index 06691e2..fd21fe4 100644 --- a/coinbase/rest/rest_base.py +++ b/coinbase/rest/rest_base.py @@ -78,6 +78,7 @@ def __init__( timeout=timeout, verbose=verbose, ) + self.session = requests.Session() if verbose: logger.setLevel(logging.DEBUG) @@ -218,7 +219,7 @@ def send_request(self, http_method, url_path, params, headers, data=None): logger.debug(f"Sending {http_method} request to {url}") - response = requests.request( + response = self.session.request( http_method, url, params=params, diff --git a/coinbase/websocket/websocket_base.py b/coinbase/websocket/websocket_base.py index e7db666..224b31d 100644 --- a/coinbase/websocket/websocket_base.py +++ b/coinbase/websocket/websocket_base.py @@ -563,7 +563,6 @@ def _build_subscription_message( "product_ids": product_ids, "channel": channel, "jwt": jwt_generator.build_ws_jwt(self.api_key, self.api_secret), - "timestamp": int(time.time()), } def _ensure_websocket_not_open(self): diff --git a/tests/rest/test_orders.py b/tests/rest/test_orders.py index 265c0e6..1ca2fe6 100644 --- a/tests/rest/test_orders.py +++ b/tests/rest/test_orders.py @@ -888,6 +888,7 @@ def test_preview_order(self): skip_fcm_risk_check=False, leverage="5", margin_type="CROSS", + retail_portfolio_id="portfolio_id_1", ) captured_request = m.request_history[0] @@ -906,6 +907,7 @@ def test_preview_order(self): "skip_fcm_risk_check": False, "leverage": "5", "margin_type": "CROSS", + "retail_portfolio_id": "portfolio_id_1", }, ) self.assertEqual(preview, expected_response)