Skip to content

Commit

Permalink
Merge pull request #266 from mollie/properties-should-not-perform-api…
Browse files Browse the repository at this point in the history
…-requests

Properties should not perform api requests
  • Loading branch information
Tom Hendrikx authored Nov 8, 2022
2 parents 0cae56d + c92fa27 commit 61a2f03
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 113 deletions.
11 changes: 4 additions & 7 deletions mollie/api/objects/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ def settlement_id(self):
def created_at(self):
return self._get_property("createdAt")

@property
def payment(self):
def get_payment(self):
"""Return the payment for this capture."""
# TODO Use the embedded payment data, if avalable.
# TODO Use the embedded payment data, if available.
return self.client.payments.get(self.payment_id)

@property
def shipment(self):
def get_shipment(self):
"""Return the shipment for this capture."""
url = self._get_link("shipment")
if url:
Expand All @@ -56,7 +54,6 @@ def shipment(self):
# We fake the order object here, since it is not used by from_url()
return OrderShipments(self.client, order=None).from_url(url)

@property
def settlement(self):
def get_settlement(self):
"""Return the settlement for this capture."""
return self.client.settlements.get(self.settlement_id)
32 changes: 18 additions & 14 deletions mollie/api/objects/chargeback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re

from ..error import DataConsistencyError
from .base import ObjectBase


Expand Down Expand Up @@ -40,22 +39,27 @@ def payment_id(self):
return self._get_property("paymentId")

@property
def payment(self):
"""Return the Payment object related to this chargeback."""
# TODO Use the embedded payment data, if available.
return self.client.payments.get(self.payment_id)
def settlement_id(self):
"""
Return the settlement ID.
@property
def settlement(self):
"""Return the Settlement object related to this chargeback, if available."""
It is extracted from the settlement link, since the id is not available as a real property.
"""
url = self._get_link("settlement")
if not url:
return None

match = re.search(r"settlements/(stl_[a-zA-Z0-9]+)$", url)
if not match:
# This should never happen
raise DataConsistencyError("Unable to extract the settlementId from settlement URL.") # pragma: no cover
match = re.findall(r"/settlements/(stl_\w+)$", url)
if match:
return match[0]

settlement_id = match.group(1)
return self.client.settlements.get(settlement_id)
def get_payment(self):
"""Return the Payment object related to this chargeback."""
# TODO Use the embedded payment data, if available.
return self.client.payments.get(self.payment_id)

def get_settlement(self):
"""Return the Settlement object related to this chargeback, if available."""
settlement_id = self.settlement_id
if settlement_id is not None:
return self.client.settlements.get(settlement_id)
6 changes: 2 additions & 4 deletions mollie/api/objects/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ def organisation_created_at(self):

# documented _links

@property
def organization(self):
def get_organization(self):
"""Return the client’s organization. Only available when the include could have been used."""
url = self._get_link("organization")
if url:
return self.client.organizations.from_url(url)
else:
return None

@property
def onboarding(self):
def get_onboarding(self):
"""Return the client’s onboarding status. Only available when the include could have been used."""
url = self._get_link("onboarding")
if url:
Expand Down
3 changes: 1 addition & 2 deletions mollie/api/objects/mandate.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def is_invalid(self):
"""Check if the mandate is invalid."""
return self.status == self.STATUS_INVALID

@property
def customer(self):
def get_customer(self):
"""Return the customer for this mandate."""
url = self._get_link("customer")
if url:
Expand Down
13 changes: 6 additions & 7 deletions mollie/api/objects/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ def can_receive_payments(self):
def can_receive_settlements(self):
return self._get_property("canReceiveSettlements")

@property
def organization(self):
"""Retrieve organization for an onboarding."""
url = self._get_link("organization")
if url:
return self.client.organizations.from_url(url)

def is_needs_data(self):
return self.status == self.STATUS_NEEDS_DATA

Expand All @@ -51,3 +44,9 @@ def is_in_review(self):

def is_completed(self):
return self.status == self.STATUS_COMPLETED

def get_organization(self):
"""Retrieve the related organization."""
url = self._get_link("organization")
if url:
return self.client.organizations.from_url(url)
15 changes: 5 additions & 10 deletions mollie/api/objects/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,19 @@ def captures(self):
"""Return the captures related to this payment"""
return PaymentCaptures(self.client, self)

@property
def settlement(self):
def get_settlement(self):
"""Return the settlement for this payment."""
if self.settlement_id:
return self.client.settlements.get(self.settlement_id)

@property
def mandate(self):
def get_mandate(self):
"""Return the mandate for this payment."""
if self.customer_id and self.mandate_id:
# Setup a minimal Customer object without querying the API.
customer = Customer({"id": self.customer_id}, self.client)
return customer.mandates.get(self.mandate_id)

@property
def subscription(self):
def get_subscription(self):
"""
Return the subscription for this payment.
Expand All @@ -218,14 +215,12 @@ def subscription(self):

return CustomerSubscriptions(self.client, customer=None).from_url(url)

@property
def customer(self):
def get_customer(self):
"""Return the customer for this payment."""
if self.customer_id:
return self.client.customers.get(self.customer_id)

@property
def order(self):
def get_order(self):
"""Return the order for this payment."""
url = self._get_link("order")
if url:
Expand Down
9 changes: 3 additions & 6 deletions mollie/api/objects/refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,17 @@ def metadata(self):

# documented _links

@property
def payment(self):
def get_payment(self):
"""Return the payment for this refund."""
return self.client.payments.get(self.payment_id)

@property
def settlement(self):
def get_settlement(self):
"""Return the settlement for this refund."""
if self.settlement_id:
return self.client.settlements.get(self.settlement_id)
return None

@property
def order(self):
def get_order(self):
"""Return the order for this refund."""
if self.order_id:
return self.client.orders.get(self.order_id)
Expand Down
3 changes: 1 addition & 2 deletions mollie/api/objects/settlement.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def captures(self):
"""Return the captures related to this settlement."""
return SettlementCaptures(self.client, self)

@property
def invoice(self):
def get_invoice(self):
"""Return the invoice related to this settlement."""
url = self._get_link("invoice")
return self.client.invoices.from_url(url)
3 changes: 1 addition & 2 deletions mollie/api/objects/shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def lines(self):
}
return ObjectList(result, OrderLine, self.client)

@property
def order(self):
def get_order(self):
"""Return the order of this shipment."""
return self.client.orders.get(self.order_id)

Expand Down
22 changes: 9 additions & 13 deletions mollie/api/objects/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ def metadata(self):
def application_fee(self):
return self._get_property("applicationFee")

@property
def customer(self):
def get_customer(self):
"""Return the customer for this subscription."""
url = self._get_link("customer")
return self.client.customers.from_url(url)
Expand All @@ -124,28 +123,25 @@ def customer_id(self):
if matches:
return matches[0]

@property
def profile(self):
def get_profile(self):
"""Return the profile related to this subscription."""
url = self._get_link("profile")
if not url:
return None
return self.client.profiles.from_url(url)

def get_mandate(self):
if self.mandate_id and self.customer_id:
from ..resources import CustomerMandates

customer = Customer({"id": self.customer_id}, self.client)
return CustomerMandates(self.client, customer).get(self.mandate_id)

@property
def payments(self):
# We could also have implemented this using the "payments" entry from the _links, but then we would not have
# the explicit interface using .payments.list()
from ..resources import SubscriptionPayments

# Create a fake customer object with minimal payload
customer = Customer({"id": self.customer_id}, self.client)
return SubscriptionPayments(self.client, customer=customer, subscription=self)

@property
def mandate(self):
if self.mandate_id and self.customer_id:
from ..resources import CustomerMandates

customer = Customer({"id": self.customer_id}, self.client)
return CustomerMandates(self.client, customer).get(self.mandate_id)
6 changes: 3 additions & 3 deletions tests/responses/settlement_single.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@
}
}
],
"invoiceId": "inv_FrvewDA3Pr"
"invoiceId": "inv_xBEbP9rvAq"
}
}
},
"invoiceId": "inv_FrvewDA3Pr",
"invoiceId": "inv_xBEbP9rvAq",
"_links": {
"self": {
"href": "https://api.mollie.com/v2/settlements/next",
Expand All @@ -119,7 +119,7 @@
"type": "application/hal+json"
},
"invoice": {
"href": "https://api.mollie.com/v2/invoices/inv_FrvewDA3Pr",
"href": "https://api.mollie.com/v2/invoices/inv_xBEbP9rvAq",
"type": "application/hal+json"
},
"documentation": {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_client_get_organization(oauth_client, response):
response.get(f"https://api.mollie.com/v2/organizations/{CLIENT_ID}", "organization_single")

client = oauth_client.clients.get(CLIENT_ID)
organization = client.organization
organization = client.get_organization()
assert isinstance(organization, Organization)


Expand All @@ -49,5 +49,5 @@ def test_client_get_onboarding(oauth_client, response):
response.get(f"https://api.mollie.com/v2/onboarding/{CLIENT_ID}", "onboarding_single")

client = oauth_client.clients.get(CLIENT_ID)
onboarding = client.onboarding
onboarding = client.get_onboarding()
assert isinstance(onboarding, Onboarding)
6 changes: 3 additions & 3 deletions tests/test_customer_mandates.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def test_get_customer_mandate(client, response):
assert mandate.is_pending() is False
assert mandate.is_valid() is True
assert mandate.is_invalid() is False
assert mandate.customer is not None


def test_get_customer_mandate_invalid_id(client, response):
Expand All @@ -67,8 +66,9 @@ def test_customer_mandate_get_related_customer(client, response):

customer = client.customers.get(CUSTOMER_ID)
mandate = customer.mandates.get(MANDATE_ID)
assert isinstance(mandate.customer, Customer)
assert mandate.customer.id == CUSTOMER_ID
customer = mandate.get_customer()
assert isinstance(customer, Customer)
assert customer.id == CUSTOMER_ID


def test_create_customer_mandate(client, response):
Expand Down
12 changes: 5 additions & 7 deletions tests/test_customer_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def test_get_customer_subscription(client, response):
f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}/subscriptions/{SUBSCRIPTION_ID}",
"subscription_single",
)
response.get(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}/mandates/{MANDATE_ID}", "customer_mandate_single")

customer = client.customers.get(CUSTOMER_ID)
subscription = customer.subscriptions.get(SUBSCRIPTION_ID)
Expand All @@ -52,8 +51,6 @@ def test_get_customer_subscription(client, response):
assert subscription.start_date == "2016-06-01"
assert subscription.next_payment_date == "2016-09-01"
assert subscription.canceled_at is None
assert subscription.customer is not None
assert subscription.mandate is not None
assert subscription.metadata == {"order_id": 1337}
assert subscription.application_fee is None
assert subscription.is_active() is True
Expand Down Expand Up @@ -82,8 +79,9 @@ def test_customer_subscription_get_related_customer(client, response):

customer = client.customers.get(CUSTOMER_ID)
subscription = customer.subscriptions.get(SUBSCRIPTION_ID)
assert isinstance(subscription.customer, Customer)
assert subscription.customer.id == CUSTOMER_ID
related_customer = subscription.get_customer()
assert isinstance(related_customer, Customer)
assert related_customer.id == CUSTOMER_ID == customer.id


def test_customer_subscription_get_related_profile(client, response):
Expand All @@ -96,7 +94,7 @@ def test_customer_subscription_get_related_profile(client, response):

customer = client.customers.get(CUSTOMER_ID)
subscription = customer.subscriptions.get(SUBSCRIPTION_ID)
profile = subscription.profile
profile = subscription.get_profile()
assert isinstance(profile, Profile)
assert profile.id == PROFILE_ID

Expand All @@ -111,7 +109,7 @@ def test_customer_subscription_get_related_mandate(client, response):

customer = client.customers.get(CUSTOMER_ID)
subscription = customer.subscriptions.get(SUBSCRIPTION_ID)
mandate = subscription.mandate
mandate = subscription.get_mandate()
assert isinstance(mandate, Mandate)
assert mandate.id == MANDATE_ID

Expand Down
2 changes: 1 addition & 1 deletion tests/test_onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ def test_onboarding_get_organization(oauth_client, response):
)

onboarding = oauth_client.onboarding.get("me")
organization = onboarding.organization
organization = onboarding.get_organization()
assert isinstance(organization, Organization)
assert organization.id == ORGANIZATION_ID
Loading

0 comments on commit 61a2f03

Please sign in to comment.