diff --git a/mollie/api/resources/base.py b/mollie/api/resources/base.py index 086c411c..1caafa0a 100644 --- a/mollie/api/resources/base.py +++ b/mollie/api/resources/base.py @@ -2,6 +2,7 @@ from ..error import IdentifierError, ResponseError, ResponseHandlingError from ..objects.list import ObjectList, ResultListIterator +from ..utils import get_class_from_dotted_path if TYPE_CHECKING: from ..client import Client @@ -24,14 +25,11 @@ class ResourceBase: def __init__(self, client: "Client") -> None: self.client = client - def get_resource_object(self, result: dict) -> Any: - """ - Return an instantiated result class for this resource. Should be overriden by a subclass. + def get_resource_object(self, result: Dict[str, Any]) -> Any: + """Return an instantiated result class for this resource.""" + result_class = get_class_from_dotted_path(self.RESULT_CLASS_PATH) - :param result: The API response that the object should hold. - :type result: dict - """ - raise NotImplementedError() # pragma: no cover + return result_class(result, self.client) def get_resource_path(self) -> str: """Return the base URL path in the API for this resource.""" diff --git a/mollie/api/resources/captures.py b/mollie/api/resources/captures.py index cd1c7417..6d1de2e3 100644 --- a/mollie/api/resources/captures.py +++ b/mollie/api/resources/captures.py @@ -16,9 +16,7 @@ class CapturesBase(ResourceBase): RESOURCE_ID_PREFIX: str = "cpt_" - - def get_resource_object(self, result: dict) -> Capture: - return Capture(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.capture.Capture" class PaymentCaptures(CapturesBase, ResourceGetMixin, ResourceListMixin): diff --git a/mollie/api/resources/chargebacks.py b/mollie/api/resources/chargebacks.py index 6895bf6e..a0a5c15e 100644 --- a/mollie/api/resources/chargebacks.py +++ b/mollie/api/resources/chargebacks.py @@ -20,9 +20,7 @@ class ChargebacksBase(ResourceBase): RESOURCE_ID_PREFIX: str = "chb_" - - def get_resource_object(self, result: dict) -> Chargeback: - return Chargeback(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.chargeback.Chargeback" class Chargebacks(ChargebacksBase, ResourceListMixin): diff --git a/mollie/api/resources/clients.py b/mollie/api/resources/clients.py index f48c466f..8b669a78 100644 --- a/mollie/api/resources/clients.py +++ b/mollie/api/resources/clients.py @@ -16,9 +16,7 @@ class Clients(ResourceListMixin, ResourceGetMixin): """ RESOURCE_ID_PREFIX: str = "org_" - - def get_resource_object(self, result: dict) -> Client: - return Client(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.client.Client" def get(self, resource_id: str, **params: Any) -> Client: """Retrieve a single client, linked to your partner account, by its ID.""" diff --git a/mollie/api/resources/customers.py b/mollie/api/resources/customers.py index 4fb5783a..e664dce7 100644 --- a/mollie/api/resources/customers.py +++ b/mollie/api/resources/customers.py @@ -8,9 +8,7 @@ class Customers(ResourceCreateMixin, ResourceDeleteMixin, ResourceGetMixin, Reso """Resource handler for the `/customers` endpoint.""" RESOURCE_ID_PREFIX: str = "cst_" - - def get_resource_object(self, result: dict) -> Customer: - return Customer(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.customer.Customer" def get(self, resource_id: str, **params: Any) -> Customer: self.validate_resource_id(resource_id, "customer ID") diff --git a/mollie/api/resources/invoices.py b/mollie/api/resources/invoices.py index 69bec70b..fb2271f9 100644 --- a/mollie/api/resources/invoices.py +++ b/mollie/api/resources/invoices.py @@ -12,9 +12,7 @@ class Invoices(ResourceGetMixin, ResourceListMixin): """Resource handler for the `/invoices` endpoint.""" RESOURCE_ID_PREFIX: str = "inv_" - - def get_resource_object(self, result: dict) -> Invoice: - return Invoice(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.invoice.Invoice" def get(self, resource_id: str, **params: Any) -> Invoice: self.validate_resource_id(resource_id, "invoice ID") diff --git a/mollie/api/resources/mandates.py b/mollie/api/resources/mandates.py index f86c824d..5c7f825c 100644 --- a/mollie/api/resources/mandates.py +++ b/mollie/api/resources/mandates.py @@ -17,6 +17,7 @@ class CustomerMandates(ResourceCreateMixin, ResourceDeleteMixin, ResourceGetMixi """Resource handler for the `/customers/:customer_id:/mandates` endpoint.""" RESOURCE_ID_PREFIX = "mdt_" + RESULT_CLASS_PATH: str = "mollie.api.objects.mandate.Mandate" _customer: Customer @@ -27,9 +28,6 @@ def __init__(self, client: "Client", customer: Customer) -> None: def get_resource_path(self) -> str: return f"customers/{self._customer.id}/mandates" - def get_resource_object(self, result: dict) -> Mandate: - return Mandate(result, self.client) - def get(self, resource_id: str, **params: Any) -> Mandate: self.validate_resource_id(resource_id, "mandate ID") return super().get(resource_id, **params) diff --git a/mollie/api/resources/methods.py b/mollie/api/resources/methods.py index 75dc2a6a..3bed379d 100644 --- a/mollie/api/resources/methods.py +++ b/mollie/api/resources/methods.py @@ -18,8 +18,8 @@ class MethodsBase(ResourceBase): - def get_resource_object(self, result: dict) -> Method: - return Method(result, self.client) + + RESULT_CLASS_PATH: str = "mollie.api.objects.method.Method" class Methods(MethodsBase, ResourceGetMixin, ResourceListMixin): diff --git a/mollie/api/resources/onboarding.py b/mollie/api/resources/onboarding.py index 30058b4b..0b8f0a7a 100644 --- a/mollie/api/resources/onboarding.py +++ b/mollie/api/resources/onboarding.py @@ -12,8 +12,7 @@ class Onboarding(ResourceGetMixin): """Resource handler for the `/onboarding` endpoint.""" - def get_resource_object(self, result: dict) -> OnboardingObject: - return OnboardingObject(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.onboarding.Onboarding" def get(self, resource_id: str, **params: Any) -> OnboardingObject: if resource_id != "me": diff --git a/mollie/api/resources/order_lines.py b/mollie/api/resources/order_lines.py index e99ab010..29f95d2c 100644 --- a/mollie/api/resources/order_lines.py +++ b/mollie/api/resources/order_lines.py @@ -25,6 +25,7 @@ class OrderLines(ResourceBase): """ RESOURCE_ID_PREFIX: str = "odl_" + RESULT_CLASS_PATH: str = "mollie.api.objects.order_line.OrderLine" _order: "Order" @@ -35,9 +36,6 @@ def __init__(self, client: "Client", order: "Order") -> None: def get_resource_path(self) -> str: return f"orders/{self._order.id}/lines" - def get_resource_object(self, result: dict) -> OrderLine: - return OrderLine(result, self.client) - def delete_lines(self, data: Optional[Dict[str, Any]] = None, **params: Any) -> dict: """ Cancel multiple orderlines. diff --git a/mollie/api/resources/orders.py b/mollie/api/resources/orders.py index adb2a3dd..c51b8ca2 100644 --- a/mollie/api/resources/orders.py +++ b/mollie/api/resources/orders.py @@ -12,9 +12,7 @@ class Orders(ResourceCreateMixin, ResourceDeleteMixin, ResourceGetMixin, Resourc """Resource handler for the `/orders` endpoint.""" RESOURCE_ID_PREFIX: str = "ord_" - - def get_resource_object(self, result: dict) -> Order: - return Order(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.order.Order" def get(self, resource_id: str, **params: Any) -> Order: self.validate_resource_id(resource_id, "order ID") diff --git a/mollie/api/resources/organizations.py b/mollie/api/resources/organizations.py index a64d9eba..7728d74f 100644 --- a/mollie/api/resources/organizations.py +++ b/mollie/api/resources/organizations.py @@ -12,9 +12,7 @@ class Organizations(ResourceGetMixin): """Resource handler for the `/organizations` endpoint.""" RESOURCE_ID_PREFIX: str = "org_" - - def get_resource_object(self, result: dict) -> Organization: - return Organization(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.organization.Organization" def get(self, resource_id: str, **params: Any) -> Organization: if resource_id != "me": diff --git a/mollie/api/resources/payment_links.py b/mollie/api/resources/payment_links.py index 94579e8d..77cafb47 100644 --- a/mollie/api/resources/payment_links.py +++ b/mollie/api/resources/payment_links.py @@ -12,13 +12,11 @@ class PaymentLinks(ResourceCreateMixin, ResourceGetMixin, ResourceListMixin): """Resource handler for the `/payment_links` endpoint.""" RESOURCE_ID_PREFIX: str = "pl_" + RESULT_CLASS_PATH: str = "mollie.api.objects.payment_link.PaymentLink" def get_resource_path(self) -> str: return "payment-links" - def get_resource_object(self, result: dict) -> PaymentLink: - return PaymentLink(result, self.client) - def get(self, resource_id: str, **params: Any) -> PaymentLink: self.validate_resource_id(resource_id, "payment link ID") return super().get(resource_id, **params) diff --git a/mollie/api/resources/payments.py b/mollie/api/resources/payments.py index f30cf942..34b862f5 100644 --- a/mollie/api/resources/payments.py +++ b/mollie/api/resources/payments.py @@ -33,11 +33,6 @@ class PaymentsBase(ResourceBase): RESOURCE_ID_PREFIX: str = "tr_" RESULT_CLASS_PATH: str = "mollie.api.objects.payment.Payment" - def get_resource_object(self, result: dict) -> Payment: - from ..objects.payment import Payment - - return Payment(result, self.client) - class Payments( PaymentsBase, ResourceCreateMixin, ResourceDeleteMixin, ResourceGetMixin, ResourceListMixin, ResourceUpdateMixin diff --git a/mollie/api/resources/permissions.py b/mollie/api/resources/permissions.py index 0ba84425..fa08e8d3 100644 --- a/mollie/api/resources/permissions.py +++ b/mollie/api/resources/permissions.py @@ -13,8 +13,7 @@ class Permissions(ResourceGetMixin, ResourceListMixin): """Resource handler for the `/permissions` endpoint.""" - def get_resource_object(self, result: dict) -> Permission: - return Permission(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.permission.Permission" @staticmethod def validate_permission_id(permission_id: str) -> None: diff --git a/mollie/api/resources/profiles.py b/mollie/api/resources/profiles.py index cfa75b80..d50f1612 100644 --- a/mollie/api/resources/profiles.py +++ b/mollie/api/resources/profiles.py @@ -12,9 +12,7 @@ class Profiles(ResourceCreateMixin, ResourceDeleteMixin, ResourceGetMixin, Resou """Resource handler for the `/profiles` endpoint.""" RESOURCE_ID_PREFIX: str = "pfl_" - - def get_resource_object(self, result: dict) -> Profile: - return Profile(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.profile.Profile" def get(self, resource_id: str, **params: Any) -> Profile: if resource_id != "me": diff --git a/mollie/api/resources/refunds.py b/mollie/api/resources/refunds.py index 54ff100d..c2f69372 100644 --- a/mollie/api/resources/refunds.py +++ b/mollie/api/resources/refunds.py @@ -22,9 +22,7 @@ class RefundsBase(ResourceBase): RESOURCE_ID_PREFIX: str = "re_" - - def get_resource_object(self, result: dict) -> Refund: - return Refund(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.refund.Refund" class Refunds(RefundsBase, ResourceListMixin): diff --git a/mollie/api/resources/settlements.py b/mollie/api/resources/settlements.py index 03f1605a..8cf2adbc 100644 --- a/mollie/api/resources/settlements.py +++ b/mollie/api/resources/settlements.py @@ -10,6 +10,7 @@ class Settlements(ResourceGetMixin, ResourceListMixin): """Resource handler for the `/settlements` endpoint.""" RESOURCE_ID_PREFIX: str = "stl_" + RESULT_CLASS_PATH: str = "mollie.api.objects.settlement.Settlement" # According to Mollie, the bank reference is formatted as: # - The Mollie customer ID, 4 to 7 digits. @@ -18,9 +19,6 @@ class Settlements(ResourceGetMixin, ResourceListMixin): # The components are separated by a dot. BANK_REFERENCE_REGEX: Pattern[str] = re.compile(r"^\d{4,7}\.\d{4}\.\d{2}$", re.ASCII) - def get_resource_object(self, result: dict) -> Settlement: - return Settlement(result, self.client) - @classmethod def validate_resource_id(cls, resource_id: str, name: str = "", message: str = "") -> None: """ diff --git a/mollie/api/resources/shipments.py b/mollie/api/resources/shipments.py index fcd3f456..db86d110 100644 --- a/mollie/api/resources/shipments.py +++ b/mollie/api/resources/shipments.py @@ -16,6 +16,7 @@ class OrderShipments(ResourceCreateMixin, ResourceGetMixin, ResourceListMixin, R """Resource handler for the `/orders/:order_id:/shipments` endpoint.""" RESOURCE_ID_PREFIX: str = "shp_" + RESULT_CLASS_PATH: str = "mollie.api.objects.shipment.Shipment" _order: Order @@ -23,9 +24,6 @@ def __init__(self, client: "Client", order: Order) -> None: self._order = order super().__init__(client) - def get_resource_object(self, result: dict) -> Shipment: - return Shipment(result, self.client) - def get_resource_path(self) -> str: return f"orders/{self._order.id}/shipments" diff --git a/mollie/api/resources/subscriptions.py b/mollie/api/resources/subscriptions.py index a8fd5b53..0be28282 100644 --- a/mollie/api/resources/subscriptions.py +++ b/mollie/api/resources/subscriptions.py @@ -22,9 +22,7 @@ class SubscriptionsBase(ResourceBase): RESOURCE_ID_PREFIX: str = "sub_" - - def get_resource_object(self, result: dict) -> Subscription: - return Subscription(result, self.client) + RESULT_CLASS_PATH: str = "mollie.api.objects.subscription.Subscription" class Subscriptions(SubscriptionsBase, ResourceListMixin):