Skip to content

Commit 95be789

Browse files
author
Tom Hendrikx
committed
WIP Refactor all nested endpoints to use an injected resource path
1 parent 7a42d2f commit 95be789

30 files changed

+680
-113
lines changed

mollie/api/objects/balance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def pending_amount(self):
6363
def get_report(self, **params: Any) -> BalanceReport:
6464
from ..resources import BalanceReports
6565

66-
return BalanceReports(self.client, self).get_report(params=params)
66+
return BalanceReports(self.client, resource_path=f"balances/{self.id}/report").get_report(params=params)
6767

6868
def get_transactions(self, **params: Any) -> ObjectList:
6969
from ..resources import BalanceTransactions
7070

71-
return BalanceTransactions(self.client, self).list(params=params)
71+
return BalanceTransactions(self.client, resource_path=f"balances/{self.id}/transactions").list(params=params)

mollie/api/objects/balance_report.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33

44
class BalanceReport(ObjectBase):
5-
@classmethod
6-
def get_resource_class(cls, client):
7-
from ..resources import BalanceReports
8-
9-
return BalanceReports(client)
10-
115
@property
126
def resource(self):
137
return self._get_property("resource")

mollie/api/objects/payment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ def chargebacks(self):
193193
"""Return the chargebacks related to this payment."""
194194
from ..resources import PaymentChargebacks
195195

196-
return PaymentChargebacks(self.client, self)
196+
return PaymentChargebacks(self.client, resource_path=f"payments/{self.id}/chargebacks")
197197

198198
@property
199199
def captures(self):
200200
"""Return the captures related to this payment"""
201201
from ..resources import PaymentCaptures
202202

203-
return PaymentCaptures(self.client, self)
203+
return PaymentCaptures(self.client, resource_path=f"payments/{self.id}/captures")
204204

205205
def get_settlement(self):
206206
"""Return the settlement for this payment."""

mollie/api/objects/profile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def created_at(self):
7171
def chargebacks(self):
7272
from ..resources import ProfileChargebacks
7373

74-
return ProfileChargebacks(self.client, self)
74+
return ProfileChargebacks(self.client, resource_path=f"chargebacks?profileId={self.id}")
7575

7676
@property
7777
def methods(self):

mollie/api/objects/settlement.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ def chargebacks(self):
8686
"""Return the chargebacks related to this settlement."""
8787
from ..resources import SettlementChargebacks
8888

89-
return SettlementChargebacks(self.client, self)
89+
return SettlementChargebacks(self.client, resource_path=f"settlements/{self.id}/chargebacks")
9090

9191
@property
9292
def captures(self):
9393
"""Return the captures related to this settlement."""
9494
from ..resources import SettlementCaptures
9595

96-
return SettlementCaptures(self.client, self)
96+
return SettlementCaptures(self.client, resource_path=f"settlements/{self.id}/captures")
9797

9898
def get_invoice(self):
9999
"""Return the invoice related to this settlement."""

mollie/api/resources/balances.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
from typing import TYPE_CHECKING, Any
1+
from typing import Any
22

33
from mollie.api.objects.balance import Balance
44
from mollie.api.objects.balance_report import BalanceReport
55
from mollie.api.objects.balance_transaction import BalanceTransaction
66
from mollie.api.resources.base import ResourceBase, ResourceGetMixin, ResourceListMixin
77

8-
if TYPE_CHECKING:
9-
from ..client import Client
10-
118

129
class Balances(ResourceGetMixin, ResourceListMixin):
1310
RESOURCE_ID_PREFIX: str = "bal_"
@@ -42,38 +39,19 @@ def get(self, resource_id: str, **params: Any) -> Balance:
4239

4340

4441
class BalanceReports(ResourceBase):
45-
46-
_balance: "Balance"
47-
48-
def __init__(self, client: "Client", balance: "Balance") -> None:
49-
self._balance = balance
50-
super().__init__(client)
51-
5242
def get_resource_object(self, result: dict) -> BalanceReport:
5343
from ..objects.balance_report import BalanceReport
5444

5545
return BalanceReport(result, self.client)
5646

57-
def get_resource_path(self) -> str:
58-
return f"balances/{self._balance.id}/report"
59-
6047
def get_report(self, **params: Any) -> BalanceReport:
6148
path = self.get_resource_path()
6249
result = self.perform_api_call(self.REST_READ, path, params=params)
6350
return self.get_resource_object(result)
6451

6552

6653
class BalanceTransactions(ResourceListMixin):
67-
_balance: "Balance"
68-
69-
def __init__(self, client: "Client", balance: "Balance") -> None:
70-
self._balance = balance
71-
super().__init__(client)
72-
7354
def get_resource_object(self, result: dict) -> BalanceTransaction:
7455
from ..objects.balance_transaction import BalanceTransaction
7556

7657
return BalanceTransaction(result, self.client)
77-
78-
def get_resource_path(self) -> str:
79-
return f"balances/{self._balance.id}/transactions"

mollie/api/resources/captures.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
from typing import TYPE_CHECKING, Any
1+
from typing import Any
22

33
from ..objects.capture import Capture
44
from .base import ResourceBase, ResourceGetMixin, ResourceListMixin
55

6-
if TYPE_CHECKING:
7-
from ..client import Client
8-
from ..objects.payment import Payment
9-
from ..objects.settlement import Settlement
10-
116
__all__ = [
127
"PaymentCaptures",
138
"SettlementCaptures",
@@ -24,15 +19,6 @@ def get_resource_object(self, result: dict) -> Capture:
2419
class PaymentCaptures(CapturesBase, ResourceGetMixin, ResourceListMixin):
2520
"""Resource handler for the `/payments/:payment_id:/captures` endpoint."""
2621

27-
_payment: "Payment"
28-
29-
def __init__(self, client: "Client", payment: "Payment") -> None:
30-
self._payment = payment
31-
super().__init__(client)
32-
33-
def get_resource_path(self) -> str:
34-
return f"payments/{self._payment.id}/captures"
35-
3622
def get(self, resource_id: str, **params: Any) -> Capture:
3723
self.validate_resource_id(resource_id, "capture ID")
3824
return super().get(resource_id, **params)
@@ -41,11 +27,4 @@ def get(self, resource_id: str, **params: Any) -> Capture:
4127
class SettlementCaptures(CapturesBase, ResourceListMixin):
4228
"""Resource handler for the `/settlements/:settlement_id:/captures` endpoint."""
4329

44-
_settlement: "Settlement"
45-
46-
def __init__(self, client: "Client", settlement: "Settlement") -> None:
47-
self._settlement = settlement
48-
super().__init__(client)
49-
50-
def get_resource_path(self) -> str:
51-
return f"settlements/{self._settlement.id}/captures"
30+
pass
Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
from typing import TYPE_CHECKING, Any
1+
from typing import Any
22

33
from ..objects.chargeback import Chargeback
4-
from ..objects.list import ObjectList
54
from .base import ResourceBase, ResourceGetMixin, ResourceListMixin
65

7-
if TYPE_CHECKING:
8-
from ..client import Client
9-
from ..objects.payment import Payment
10-
from ..objects.profile import Profile
11-
from ..objects.settlement import Settlement
12-
136
__all__ = [
147
"Chargebacks",
158
"PaymentChargebacks",
@@ -34,15 +27,6 @@ class Chargebacks(ChargebacksBase, ResourceListMixin):
3427
class PaymentChargebacks(ChargebacksBase, ResourceGetMixin, ResourceListMixin):
3528
"""Resource handler for the `/payments/:payment_id:/chargebacks` endpoint."""
3629

37-
_payment: "Payment"
38-
39-
def __init__(self, client: "Client", payment: "Payment") -> None:
40-
self._payment = payment
41-
super().__init__(client)
42-
43-
def get_resource_path(self) -> str:
44-
return f"payments/{self._payment.id}/chargebacks"
45-
4630
def get(self, resource_id: str, **params: Any) -> Chargeback:
4731
self.validate_resource_id(resource_id, "chargeback ID")
4832
return super().get(resource_id, **params)
@@ -51,30 +35,14 @@ def get(self, resource_id: str, **params: Any) -> Chargeback:
5135
class SettlementChargebacks(ChargebacksBase, ResourceListMixin):
5236
"""Resource handler for the `/settlements/:settlement_id:/chargebacks` endpoint."""
5337

54-
_settlement: "Settlement"
55-
56-
def __init__(self, client: "Client", settlement: "Settlement") -> None:
57-
self._settlement = settlement
58-
super().__init__(client)
59-
60-
def get_resource_path(self) -> str:
61-
return f"settlements/{self._settlement.id}/chargebacks"
38+
pass
6239

6340

64-
class ProfileChargebacks(ChargebacksBase):
41+
class ProfileChargebacks(Chargebacks):
6542
"""
6643
Resource handler for the `/chargebacks?profileId=:profile_id:` endpoint.
6744
68-
This is separate from the `Chargebacks` resource handler to make it easier to inject the profileId.
45+
This is completely equal to Chargebacks, just here for completeness.
6946
"""
7047

71-
_profile: "Profile"
72-
73-
def __init__(self, client: "Client", profile: "Profile") -> None:
74-
self._profile = profile
75-
super().__init__(client)
76-
77-
def list(self, **params: Any) -> ObjectList:
78-
# Set the profileId in the query params
79-
params.update({"profileId": self._profile.id})
80-
return Chargebacks(self.client).list(**params)
48+
pass

tests/responses/balance_transactions_list.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
"type": "text/html"
3333
},
3434
"self": {
35-
"href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH/transactions?limit=5",
35+
"href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH/transactions?limit=1",
3636
"type": "application/hal+json"
3737
},
3838
"previous": null,
39-
"next": null
39+
"next": {
40+
"href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH/transactions?limit=1&from=baltr_QM24QwzUWR4ev4Xfgyt29B"
41+
}
4042
}
41-
}
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"count": 1,
3+
"_embedded": {
4+
"balance_transactions": [
5+
{
6+
"resource": "balance_transaction",
7+
"id": "baltr_QM24QwzUWR4ev4Xfgyt29B",
8+
"type": "refund",
9+
"resultAmount": {
10+
"value": "-10.33",
11+
"currency": "EUR"
12+
},
13+
"initialAmount": {
14+
"value": "-10.00",
15+
"currency": "EUR"
16+
},
17+
"deductions": {
18+
"value": "-0.25",
19+
"currency": "EUR"
20+
},
21+
"createdAt": "2021-01-10T12:06:28+00:00",
22+
"context": {
23+
"paymentId": "tr_7UhSN1zuXT",
24+
"refundId": "re_4qqhO89gsX"
25+
}
26+
}
27+
]
28+
},
29+
"_links": {
30+
"documentation": {
31+
"href": "https://docs.mollie.com/reference/v2/balances-api/list-balance-transactions",
32+
"type": "text/html"
33+
},
34+
"self": {
35+
"href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH/transactions?limit=1&from=baltr_QM24QwzUWR4ev4Xfgyt29B",
36+
"type": "application/hal+json"
37+
},
38+
"previous": null,
39+
"next": null
40+
}
41+
}

0 commit comments

Comments
 (0)