Skip to content

Commit

Permalink
Remove lots of duplicate code and replace it with a dotted path class…
Browse files Browse the repository at this point in the history
… constant
  • Loading branch information
Tom Hendrikx committed Dec 4, 2022
1 parent b79a639 commit 96548cf
Show file tree
Hide file tree
Showing 20 changed files with 24 additions and 63 deletions.
12 changes: 5 additions & 7 deletions mollie/api/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/captures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/chargebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/mandates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions mollie/api/resources/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions mollie/api/resources/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/order_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class OrderLines(ResourceBase):
"""

RESOURCE_ID_PREFIX: str = "odl_"
RESULT_CLASS_PATH: str = "mollie.api.objects.order_line.OrderLine"

_order: "Order"

Expand All @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/payment_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 0 additions & 5 deletions mollie/api/resources/payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions mollie/api/resources/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/refunds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/settlements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
"""
Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/shipments.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ 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

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"

Expand Down
4 changes: 1 addition & 3 deletions mollie/api/resources/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 96548cf

Please sign in to comment.