mollie / mollie-api-python

Mollie API client for Python
http://www.mollie.com
BSD 2-Clause "Simplified" License
113 stars 55 forks source link

Refactor the Result classes using a proper Descriptor protocol implementation #289

Open whyscream opened 1 year ago

whyscream commented 1 year ago

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:

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