Skip to content

Commit

Permalink
Added missing period paras to 'get_list_of_payments' API call
Browse files Browse the repository at this point in the history
  • Loading branch information
tomazmm committed Nov 6, 2023
1 parent 66d6c98 commit 690db4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/nowpayments_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
A Python wrapper for the NOWPayments API.
"""
import logging
from datetime import datetime
from typing import Any, Dict, Union
import requests
from requests import HTTPError
Expand Down Expand Up @@ -348,6 +349,9 @@ def list_of_payments(
page: int = 0,
sort_by: str = "created_at",
order_by: str = "asc",
date_from: datetime = None,
date_to: datetime = None

) -> Any:
"""
Returns the entire list of all transactions, created with certain API key.
Expand All @@ -359,7 +363,9 @@ def list_of_payments(
actually_paid, pay_currency, order_id, order_description, purchase_id, outcome_amount, outcome_currency)
:param str order_by: Display the list in ascending or descending order. Set to asc by default
(possible values: asc, desc)
:param datetime date_from: Select the displayed period start date
:param datetime date_to: Select the displayed period end date
:returns
"""
available_sort_paras = [
"created_at",
Expand All @@ -377,6 +383,7 @@ def list_of_payments(
"outcome_amount",
"outcome_currency",
]
period = ""
if 1 > limit or limit > 500:
raise NowPaymentsException("Limit must be a number between 1 and 500")
if page < 0:
Expand All @@ -385,10 +392,15 @@ def list_of_payments(
raise NowPaymentsException("Invalid sort parameter")
if order_by not in ["asc", "desc"]:
raise NowPaymentsException("Invalid order parameter")
if date_from:
period += f"dateFrom={date_from.strftime('%Y-%m-%dT%H:%M:%S.%s')}"
if date_to:
period += f"&dateTo={date_to.strftime('%Y-%m-%dT%H:%M:%S.%s')}"

endpoint = (
f"payment?limit={limit}&page={page}&sortBy={sort_by}&orderBy={order_by}"
f"payment?limit={limit}&page={page}&sortBy={sort_by}&orderBy={order_by}&{period}"
)

bearer = self.auth()["token"]
return self._get_request(endpoint, bearer=bearer)

Expand Down
12 changes: 11 additions & 1 deletion tests/test_nowpayments.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ def test_get_list_of_payments(now_payments_email_password: NOWPaymentsAPI) -> No
assert len(payment_list["data"]) <= 10


def test_get_list_of_payments_with_period(now_payments_email_password: NOWPaymentsAPI) -> None:
now = datetime.datetime.now()
week_ago = now - datetime.timedelta(days=7)
payment_list = now_payments_email_password.list_of_payments(
date_from=week_ago,
date_to=now
)
assert type(payment_list["data"]) == list


def test_get_list_of_payments_limit_error(
now_payments_email_password: NOWPaymentsAPI,
) -> None:
Expand Down Expand Up @@ -461,7 +471,7 @@ def test_update_payment_estimate(now_payments_api_key: NOWPaymentsAPI) -> None:
payment = now_payments_api_key.create_payment(
100, price_currency="usd", pay_currency="btc"
)
response = now_payments_api_key.update_payment_estimate(int(payment["payment_id"] ))
response = now_payments_api_key.update_payment_estimate(int(payment["payment_id"]))
assert response["id"] == int(payment["payment_id"])
assert "token_id" in response
assert "pay_amount" in response
Expand Down

0 comments on commit 690db4f

Please sign in to comment.