From 218cb2343722128099bda3d175d0847fc78d7bf3 Mon Sep 17 00:00:00 2001 From: Rostyslav Bohomaz Date: Fri, 27 Oct 2023 18:20:44 +0300 Subject: [PATCH] fix reports method for csv and xml --- liqpy/client.py | 22 ++++++++++------------ readme.ipynb | 30 +++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/liqpy/client.py b/liqpy/client.py index 9249098..f5e7797 100644 --- a/liqpy/client.py +++ b/liqpy/client.py @@ -192,33 +192,31 @@ def checkout(self, /, action: str, **kwargs) -> str: def reports( self, /, - date_from: Optional[datetime | str] = None, - date_to: Optional[datetime | str] = None, + date_from: datetime | str, + date_to: datetime | str, format: Optional[Literal["json", "csv", "xml"]] = None, - ): + ) -> list[dict[str, str | int | float]] | str: """ Get an archive of recieved payments. [Documentaion](https://www.liqpay.ua/en/documentation/api/information/reports/doc) """ - kwargs = {} - if isinstance(date_from, str): date_from = datetime.fromisoformat(date_from) if isinstance(date_to, str): date_to = datetime.fromisoformat(date_to) - if date_from is not None: - kwargs["date_from"] = to_milliseconds(date_from) + kwargs = {"date_from": to_milliseconds(date_from), "date_to": to_milliseconds(date_to)} - if date_to is not None: - kwargs["date_to"] = to_milliseconds(date_to) - if format is not None: kwargs["resp_format"] = format - - return self.request("reports", **kwargs)["data"] + + match format: + case None | "json": + return self.request("reports", **kwargs)["data"] + case "csv" | "xml": + return self._post_request(*self.encode("reports", **kwargs)).content.decode() def data(self, /, order_id: str, info: str) -> dict: """ diff --git a/readme.ipynb b/readme.ipynb index 7c308e8..371417d 100644 --- a/readme.ipynb +++ b/readme.ipynb @@ -25,7 +25,7 @@ "\n", "from uuid import uuid4\n", "from pprint import pprint\n", - "from datetime import datetime, timedelta\n", + "from datetime import datetime, timedelta, UTC\n", "\n", "from liqpy.client import Client\n", "from liqpy.testing import TestCard\n", @@ -121,6 +121,15 @@ "client.status(order_id)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "client.data(order_id, \"Test info\")" + ] + }, { "cell_type": "code", "execution_count": null, @@ -135,6 +144,25 @@ " currency=\"USD\",\n", ")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "date_to = datetime.now(UTC)\n", + "date_from = date_to - timedelta(days=30)\n", + "\n", + "client.reports(date_from=date_from, date_to=date_to, format=\"json\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {