Skip to content

Commit

Permalink
fix reports method for csv and xml
Browse files Browse the repository at this point in the history
  • Loading branch information
rostyq committed Oct 27, 2023
1 parent 8ad7b28 commit 218cb23
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
22 changes: 10 additions & 12 deletions liqpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
30 changes: 29 additions & 1 deletion readme.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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": {
Expand Down

0 comments on commit 218cb23

Please sign in to comment.