From 99799fe017c7fa18e4141aacf7ba58f03bdf3d5d Mon Sep 17 00:00:00 2001 From: eri Date: Wed, 4 Feb 2026 06:03:18 -0300 Subject: [PATCH] Extract date formatting into a method --- ws_api/wealthsimple_api.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/ws_api/wealthsimple_api.py b/ws_api/wealthsimple_api.py index de313e9..d79ce5d 100644 --- a/ws_api/wealthsimple_api.py +++ b/ws_api/wealthsimple_api.py @@ -404,6 +404,10 @@ def __init__(self, sess: WSAPISession | None = None) -> None: super().__init__(sess) self.account_cache = {} + @staticmethod + def _iso_z(dt: datetime | None) -> str | None: + return dt.strftime("%Y-%m-%dT%H:%M:%S.%fZ") if dt else None + def get_accounts(self, open_only=True, use_cache=True): cache_key = "open" if open_only else "all" if not use_cache or cache_key not in self.account_cache: @@ -462,12 +466,8 @@ def get_account_historical_financials( { "id": account_id, "currency": currency, - "startDate": start_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ") - if start_date - else None, - "endDate": end_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ") - if end_date - else None, + "startDate": self._iso_z(start_date), + "endDate": self._iso_z(end_date), "resolution": resolution, "first": first, "cursor": cursor, @@ -490,12 +490,8 @@ def get_identity_historical_financials( { "identityId": self.get_token_info().get("identity_canonical_id"), "currency": currency, - "startDate": start_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ") - if start_date - else None, - "endDate": end_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ") - if end_date - else None, + "startDate": self._iso_z(start_date), + "endDate": self._iso_z(end_date), "first": first, "cursor": cursor, "accountIds": account_ids or [], @@ -557,10 +553,8 @@ def filter_fn(activity): "orderBy": order_by, "first": how_many, "condition": { - "startDate": start_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ") - if start_date - else None, - "endDate": end_date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), + "startDate": self._iso_z(start_date), + "endDate": self._iso_z(end_date), "accountIds": account_id, }, },