Skip to content

Commit 4eaf710

Browse files
author
Tom Hendrikx
committed
Remove duplicate code and replace it with a generic method and a constant
1 parent 91beaab commit 4eaf710

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
@@ -4,6 +4,7 @@
44

55
from ..error import IdentifierError, ResponseError, ResponseHandlingError
66
from ..objects.list import ObjectList, ResultListIterator
7+
from ..utils import get_class_from_dotted_path
78

89
if TYPE_CHECKING:
910
from ..client import Client
@@ -26,14 +27,11 @@ class ResourceBase:
2627
def __init__(self, client: "Client") -> None:
2728
self.client = client
2829

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

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

3836
def get_resource_path(self) -> str:
3937
"""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
@@ -13,8 +13,7 @@
1313
class Onboarding(ResourceGetMixin):
1414
"""Resource handler for the `/onboarding` endpoint."""
1515

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

1918
def get(self, resource_id: str, **params: Any) -> OnboardingObject:
2019
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)