Skip to content

Commit 96548cf

Browse files
author
Tom Hendrikx
committed
Remove lots of duplicate code and replace it with a dotted path class constant
1 parent b79a639 commit 96548cf

20 files changed

+24
-63
lines changed

mollie/api/resources/base.py

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

33
from ..error import IdentifierError, ResponseError, ResponseHandlingError
44
from ..objects.list import ObjectList, ResultListIterator
5+
from ..utils import get_class_from_dotted_path
56

67
if TYPE_CHECKING:
78
from ..client import Client
@@ -24,14 +25,11 @@ class ResourceBase:
2425
def __init__(self, client: "Client") -> None:
2526
self.client = client
2627

27-
def get_resource_object(self, result: dict) -> Any:
28-
"""
29-
Return an instantiated result class for this resource. Should be overriden by a subclass.
28+
def get_resource_object(self, result: Dict[str, Any]) -> Any:
29+
"""Return an instantiated result class for this resource."""
30+
result_class = get_class_from_dotted_path(self.RESULT_CLASS_PATH)
3031

31-
:param result: The API response that the object should hold.
32-
:type result: dict
33-
"""
34-
raise NotImplementedError() # pragma: no cover
32+
return result_class(result, self.client)
3533

3634
def get_resource_path(self) -> str:
3735
"""Return the base URL path in the API for this resource."""

mollie/api/resources/captures.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
class CapturesBase(ResourceBase):
1818
RESOURCE_ID_PREFIX: str = "cpt_"
19-
20-
def get_resource_object(self, result: dict) -> Capture:
21-
return Capture(result, self.client)
19+
RESULT_CLASS_PATH: str = "mollie.api.objects.capture.Capture"
2220

2321

2422
class PaymentCaptures(CapturesBase, ResourceGetMixin, ResourceListMixin):

mollie/api/resources/chargebacks.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
class ChargebacksBase(ResourceBase):
2222
RESOURCE_ID_PREFIX: str = "chb_"
23-
24-
def get_resource_object(self, result: dict) -> Chargeback:
25-
return Chargeback(result, self.client)
23+
RESULT_CLASS_PATH: str = "mollie.api.objects.chargeback.Chargeback"
2624

2725

2826
class Chargebacks(ChargebacksBase, ResourceListMixin):

mollie/api/resources/clients.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class Clients(ResourceListMixin, ResourceGetMixin):
1616
"""
1717

1818
RESOURCE_ID_PREFIX: str = "org_"
19-
20-
def get_resource_object(self, result: dict) -> Client:
21-
return Client(result, self.client)
19+
RESULT_CLASS_PATH: str = "mollie.api.objects.client.Client"
2220

2321
def get(self, resource_id: str, **params: Any) -> Client:
2422
"""Retrieve a single client, linked to your partner account, by its ID."""

mollie/api/resources/customers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ class Customers(ResourceCreateMixin, ResourceDeleteMixin, ResourceGetMixin, Reso
88
"""Resource handler for the `/customers` endpoint."""
99

1010
RESOURCE_ID_PREFIX: str = "cst_"
11-
12-
def get_resource_object(self, result: dict) -> Customer:
13-
return Customer(result, self.client)
11+
RESULT_CLASS_PATH: str = "mollie.api.objects.customer.Customer"
1412

1513
def get(self, resource_id: str, **params: Any) -> Customer:
1614
self.validate_resource_id(resource_id, "customer ID")

mollie/api/resources/invoices.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ class Invoices(ResourceGetMixin, ResourceListMixin):
1212
"""Resource handler for the `/invoices` endpoint."""
1313

1414
RESOURCE_ID_PREFIX: str = "inv_"
15-
16-
def get_resource_object(self, result: dict) -> Invoice:
17-
return Invoice(result, self.client)
15+
RESULT_CLASS_PATH: str = "mollie.api.objects.invoice.Invoice"
1816

1917
def get(self, resource_id: str, **params: Any) -> Invoice:
2018
self.validate_resource_id(resource_id, "invoice ID")

mollie/api/resources/mandates.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class CustomerMandates(ResourceCreateMixin, ResourceDeleteMixin, ResourceGetMixi
1717
"""Resource handler for the `/customers/:customer_id:/mandates` endpoint."""
1818

1919
RESOURCE_ID_PREFIX = "mdt_"
20+
RESULT_CLASS_PATH: str = "mollie.api.objects.mandate.Mandate"
2021

2122
_customer: Customer
2223

@@ -27,9 +28,6 @@ def __init__(self, client: "Client", customer: Customer) -> None:
2728
def get_resource_path(self) -> str:
2829
return f"customers/{self._customer.id}/mandates"
2930

30-
def get_resource_object(self, result: dict) -> Mandate:
31-
return Mandate(result, self.client)
32-
3331
def get(self, resource_id: str, **params: Any) -> Mandate:
3432
self.validate_resource_id(resource_id, "mandate ID")
3533
return super().get(resource_id, **params)

mollie/api/resources/methods.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919

2020
class MethodsBase(ResourceBase):
21-
def get_resource_object(self, result: dict) -> Method:
22-
return Method(result, self.client)
21+
22+
RESULT_CLASS_PATH: str = "mollie.api.objects.method.Method"
2323

2424

2525
class Methods(MethodsBase, ResourceGetMixin, ResourceListMixin):

mollie/api/resources/onboarding.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
class Onboarding(ResourceGetMixin):
1313
"""Resource handler for the `/onboarding` endpoint."""
1414

15-
def get_resource_object(self, result: dict) -> OnboardingObject:
16-
return OnboardingObject(result, self.client)
15+
RESULT_CLASS_PATH: str = "mollie.api.objects.onboarding.Onboarding"
1716

1817
def get(self, resource_id: str, **params: Any) -> OnboardingObject:
1918
if resource_id != "me":

mollie/api/resources/order_lines.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class OrderLines(ResourceBase):
2525
"""
2626

2727
RESOURCE_ID_PREFIX: str = "odl_"
28+
RESULT_CLASS_PATH: str = "mollie.api.objects.order_line.OrderLine"
2829

2930
_order: "Order"
3031

@@ -35,9 +36,6 @@ def __init__(self, client: "Client", order: "Order") -> None:
3536
def get_resource_path(self) -> str:
3637
return f"orders/{self._order.id}/lines"
3738

38-
def get_resource_object(self, result: dict) -> OrderLine:
39-
return OrderLine(result, self.client)
40-
4139
def delete_lines(self, data: Optional[Dict[str, Any]] = None, **params: Any) -> dict:
4240
"""
4341
Cancel multiple orderlines.

0 commit comments

Comments
 (0)