mollie-api-python
mollie-api-python copied to clipboard
Refactor the Result classes using a proper Descriptor protocol implementation
Using a generic descriptor class for handling all Result class (all classes in mollie.api.objects.*
) properties would allow us to remove lots of duplicate and repetitive code.
Code for all properties in all classes could be simplified like this:
class Payment: # mollie.api.objects.payment.Payment
# Current code
@property
def created_at(self):
return self._get_property("createdAt")
@property
def authorized_at(self):
return self._get_property("authorizedAt")
# Possible replacement code
created_at = ResultProperty()
authorized_at = ResultProperty()
The current property methods (def created_at()
from above example) are written for almost every root property in each API response, so we have hundreds of them. They have been prone to subtle copy/paste and typo errors ever since they existed, and year-old errors are still discovered now and then. This proposal would remove them and replace them with a generic replacement.
Apart from all the duplicated code, we could gain:
- A single place to manipulate/validate values returned from the API, in stead of many (hundreds) separate methods.
- Writable properties. Currently there is no use for updating properties since there is no way to store or validate the updated values, but we could implement creating/updating from the Result object self:
payment = client.payments.get("tr_12345")
payment.description = "Updated payment description"
payment.update()
Python docs on descriptor protocol (the pattern that would be used to implement the ResultProperty
class): https://docs.python.org/3/howto/descriptor.html