Skip to content

Commit a628ab0

Browse files
Merge pull request #333 from Bladieblah/remove-get-resource-class-method
Remove get resource class method
2 parents cb584a1 + 547bffe commit a628ab0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+801
-332
lines changed

mollie/api/objects/balance.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
from .balance_report import BalanceReport
44
from .base import ObjectBase
5-
from .list import ObjectList
5+
from .list import PaginationList
66

77

88
class Balance(ObjectBase):
9-
@classmethod
10-
def get_resource_class(cls, client):
11-
from ..resources import Balances
12-
13-
return Balances(client)
14-
159
@property
1610
def resource(self):
1711
return self._get_property("resource")
@@ -65,7 +59,7 @@ def get_report(self, **params: Any) -> BalanceReport:
6559

6660
return BalanceReports(self.client, self).get_report(params=params)
6761

68-
def get_transactions(self, **params: Any) -> ObjectList:
62+
def get_transactions(self, **params: Any) -> PaginationList:
6963
from ..resources import BalanceTransactions
7064

7165
return BalanceTransactions(self.client, self).list(params=params)

mollie/api/objects/balance_report.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
from typing import TYPE_CHECKING, Any
2-
31
from .base import ObjectBase
42

5-
if TYPE_CHECKING:
6-
from ..client import Client
7-
from ..resources import BalanceReports
8-
93

104
class BalanceReport(ObjectBase):
11-
@classmethod
12-
def get_resource_class(cls, client: "Client", **kwargs: Any) -> "BalanceReports":
13-
from ..resources import BalanceReports
14-
15-
balance = kwargs["balance"]
16-
return BalanceReports(client, balance)
17-
185
@property
196
def resource(self):
207
return self._get_property("resource")

mollie/api/objects/balance_transaction.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
from typing import TYPE_CHECKING, Any
2-
31
from .base import ObjectBase
42

5-
if TYPE_CHECKING:
6-
from ..client import Client
7-
from ..resources import BalanceTransactions
8-
93

104
class BalanceTransaction(ObjectBase):
11-
@classmethod
12-
def get_resource_class(cls, client: "Client", **kwargs: Any) -> "BalanceTransactions":
13-
from ..resources import BalanceTransactions
14-
15-
balance = kwargs["balance"]
16-
return BalanceTransactions(client, balance)
17-
185
@classmethod
196
def get_object_name(cls):
207
# Overwrite get_object_name since BalanceTransactions gets returned by Mollie as balance_transactions.

mollie/api/objects/base.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
from typing import TYPE_CHECKING, Any
2-
31
from ..error import EmbedNotFound
42

5-
if TYPE_CHECKING:
6-
from ..client import Client
7-
83

94
class ObjectBase(dict):
105
def __init__(self, data, client):
@@ -45,7 +40,3 @@ def get_embedded(self, name: str) -> dict:
4540
def get_object_name(cls):
4641
name = cls.__name__.lower()
4742
return f"{name}s"
48-
49-
@classmethod
50-
def get_resource_class(cls, client: "Client", **kwargs: Any) -> Any:
51-
raise NotImplementedError # pragma: no cover

mollie/api/objects/capture.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
from typing import TYPE_CHECKING, Any
2-
31
from .base import ObjectBase
42

5-
if TYPE_CHECKING:
6-
from ..client import Client
7-
from ..resources import PaymentCaptures
8-
93

104
class Capture(ObjectBase):
11-
@classmethod
12-
def get_resource_class(cls, client: "Client", **kwargs: Any) -> "PaymentCaptures":
13-
from ..resources import PaymentCaptures
14-
15-
payment = kwargs["payment"]
16-
return PaymentCaptures(client, payment)
17-
185
@property
196
def id(self):
207
return self._get_property("id")

mollie/api/objects/chargeback.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55

66
class Chargeback(ObjectBase):
7-
@classmethod
8-
def get_resource_class(cls, client):
9-
from ..resources import Chargebacks
10-
11-
return Chargebacks(client)
12-
137
@property
148
def id(self):
159
return self._get_property("id")

mollie/api/objects/client.py

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

33

44
class Client(ObjectBase):
5-
@classmethod
6-
def get_resource_class(cls, client):
7-
from ..resources import Clients
8-
9-
return Clients(client)
10-
115
# Documented properties
126

137
@property

mollie/api/objects/customer.py

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

33

44
class Customer(ObjectBase):
5-
@classmethod
6-
def get_resource_class(cls, client):
7-
from ..resources import Customers
8-
9-
return Customers(client)
10-
115
@property
126
def id(self):
137
return self._get_property("id")

mollie/api/objects/invoice.py

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

33

44
class Invoice(ObjectBase):
5-
@classmethod
6-
def get_resource_class(cls, client):
7-
from ..resources import Invoices
8-
9-
return Invoices(client)
10-
115
@property
126
def id(self):
137
return self._get_property("id")

mollie/api/objects/list.py

Lines changed: 101 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
1-
from .base import ObjectBase
2-
1+
from abc import ABC, abstractmethod
2+
from typing import TYPE_CHECKING, Any, Optional, Type
33

4-
class UnknownObject(ObjectBase):
5-
"""Mock object for empty lists."""
4+
from .base import ObjectBase
65

7-
@classmethod
8-
def get_object_name(cls):
9-
return "unknown"
6+
if TYPE_CHECKING:
7+
from mollie.api.client import Client
8+
from mollie.api.resources.base import ResourceBase
109

1110

12-
class ObjectList(ObjectBase):
11+
class ListBase(ObjectBase, ABC):
1312
current = None
1413

15-
def __init__(self, result, object_type, client=None):
16-
# If an empty dataset was injected, we mock the structure that the remainder of the clas expects.
17-
# TODO: it would be better if the ObjectList was initiated with a list of results, rather than with
18-
# the full datastructure as it is now, so we can remove all this mucking around with fake data,
19-
# mocked result objects, and loads of lengthy accessor workarounds everywhere in the ObjectList.
20-
if result == {}:
21-
result = {"_embedded": {"unknown": []}, "count": 0}
22-
object_type = UnknownObject
23-
24-
super().__init__(result, client)
25-
self.object_type = object_type
26-
2714
def __len__(self):
2815
"""Return the count field."""
2916
return self.count
@@ -66,7 +53,7 @@ def __getitem__(self, key):
6653
},
6754
"count": len(sliced_data),
6855
}
69-
return ObjectList(sliced_result, self.object_type, self.client)
56+
return self.new(sliced_result)
7057

7158
return super().__getitem__(key)
7259

@@ -84,16 +71,101 @@ def has_previous(self):
8471
"""Return True if the ObjectList contains an url for the previous set."""
8572
return self._get_link("previous") is not None
8673

74+
@abstractmethod
8775
def get_next(self):
88-
"""Return the next set of objects in an ObjectList."""
76+
...
77+
78+
@abstractmethod
79+
def get_previous(self):
80+
...
81+
82+
@property
83+
@abstractmethod
84+
def object_type(self):
85+
...
86+
87+
@abstractmethod
88+
def new(self, result):
89+
...
90+
91+
92+
class PaginationList(ListBase):
93+
"""
94+
Pagination lists are used to return a paginated list of Objects.
95+
96+
You can use the `has_next` and `get_next` methods to get the next page of result data from the API.
97+
The `has_previous` and `get_previous` methods return the previous page.
98+
"""
99+
100+
_parent: "ResourceBase"
101+
102+
def __init__(self, result: Any, parent: "ResourceBase", client: "Client"):
103+
# If an empty dataset was injected, we mock the structure that the remainder of the clas expects.
104+
# TODO: it would be better if the ObjectList was initiated with a list of results, rather than with
105+
# the full datastructure as it is now, so we can remove all this mucking around with fake data,
106+
# mocked result objects, and loads of lengthy accessor workarounds everywhere in the ObjectList.
107+
self._parent = parent
108+
109+
if result == {}:
110+
result = {"_embedded": {f"{self._parent.object_type.get_object_name()}": []}, "count": 0}
111+
112+
super().__init__(result, client)
113+
114+
def get_next(self):
115+
"""Return the next set of objects in the paginated list."""
89116
url = self._get_link("next")
90-
resource = self.object_type.get_resource_class(self.client)
91-
resp = resource.perform_api_call(resource.REST_READ, url)
92-
return ObjectList(resp, self.object_type, self.client)
117+
if url is None:
118+
return None
119+
resp = self._parent.perform_api_call(self._parent.REST_READ, url)
120+
return PaginationList(resp, self._parent, self.client)
93121

94122
def get_previous(self):
95-
"""Return the previous set of objects in an ObjectList."""
123+
"""Return the previous set of objects in the paginated list."""
96124
url = self._get_link("previous")
97-
resource = self.object_type.get_resource_class(self.client)
98-
resp = resource.perform_api_call(resource.REST_READ, url)
99-
return ObjectList(resp, self.object_type, self.client)
125+
if url is None:
126+
return None
127+
resp = self._parent.perform_api_call(self._parent.REST_READ, url)
128+
return PaginationList(resp, self._parent, self.client)
129+
130+
@property
131+
def object_type(self):
132+
return self._parent.object_type
133+
134+
def new(self, result):
135+
return PaginationList(result, self._parent, self.client)
136+
137+
138+
class ObjectList(ListBase):
139+
"""
140+
Object lists are used to return an embedded list on an object.
141+
142+
It works to similar to PaginationList, but has no pagination (as all data is already embedded).
143+
"""
144+
145+
_object_type: Type[ObjectBase]
146+
147+
def __init__(self, result: Any, object_type: Type[ObjectBase], client: Optional["Client"] = None):
148+
# If an empty dataset was injected, we mock the structure that the remainder of the clas expects.
149+
# TODO: it would be better if the ObjectList was initiated with a list of results, rather than with
150+
# the full datastructure as it is now, so we can remove all this mucking around with fake data,
151+
# mocked result objects, and loads of lengthy accessor workarounds everywhere in the ObjectList.
152+
self._object_type = object_type
153+
154+
if result == {}:
155+
result = {"_embedded": {f"{self._object_type.get_object_name()}": []}, "count": 0}
156+
157+
super().__init__(result, client)
158+
159+
def get_next(self):
160+
"""Return the next set of objects in an ObjectList."""
161+
return None
162+
163+
def get_previous(self):
164+
return None
165+
166+
@property
167+
def object_type(self):
168+
return self._object_type
169+
170+
def new(self, result):
171+
return ObjectList(result, self._object_type, self.client)

mollie/api/objects/mandate.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
from typing import TYPE_CHECKING, Any
2-
31
from .base import ObjectBase
42

5-
if TYPE_CHECKING:
6-
from ..client import Client
7-
from ..resources import CustomerMandates
8-
93

104
class Mandate(ObjectBase):
11-
@classmethod
12-
def get_resource_class(cls, client: "Client", **kwargs: Any) -> "CustomerMandates":
13-
from ..resources import CustomerMandates
14-
15-
customer = kwargs["customer"]
16-
return CustomerMandates(client, customer)
17-
185
STATUS_PENDING = "pending"
196
STATUS_VALID = "valid"
207
STATUS_INVALID = "invalid"

mollie/api/objects/method.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55

66
class Method(ObjectBase):
7-
@classmethod
8-
def get_resource_class(cls, client):
9-
from ..resources import Methods
10-
11-
return Methods(client)
12-
137
# Payment methods for Payments and Orders
148
APPLEPAY = "applepay"
159
BANCONTACT = "bancontact"

mollie/api/objects/onboarding.py

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

33

44
class Onboarding(ObjectBase):
5-
@classmethod
6-
def get_resource_class(cls, client):
7-
from ..resources import Onboarding as OnboardingResource
8-
9-
return OnboardingResource(client)
10-
115
STATUS_NEEDS_DATA = "needs-data"
126
STATUS_IN_REVIEW = "in-review" # Waiting for a valid mandate.
137
STATUS_COMPLETED = "completed"

mollie/api/objects/order.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ class Order(ObjectBase):
55
def __init__(self, data, client):
66
super().__init__(data, client)
77

8-
@classmethod
9-
def get_resource_class(cls, client):
10-
from ..resources import Orders
11-
12-
return Orders(client)
13-
148
STATUS_CREATED = "created"
159
STATUS_PAID = "paid"
1610
STATUS_AUTHORIZED = "authorized"

mollie/api/objects/order_line.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
from typing import TYPE_CHECKING, Any
2-
31
from .base import ObjectBase
42

5-
if TYPE_CHECKING:
6-
from ..client import Client
7-
from ..resources import OrderLines
8-
93

104
class OrderLine(ObjectBase):
11-
@classmethod
12-
def get_resource_class(cls, client: "Client", **kwargs: Any) -> "OrderLines":
13-
from ..resources import OrderLines
14-
15-
order = kwargs["order"]
16-
return OrderLines(client, order)
17-
185
STATUS_CREATED = "created"
196
STATUS_AUTHORIZED = "authorized"
207
STATUS_PAID = "paid"

0 commit comments

Comments
 (0)