Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
whyscream opened this issue Dec 2, 2022 · 0 comments

Comments

@whyscream
Copy link
Contributor

whyscream commented Dec 2, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant