mollie / mollie/mollie-api-python

Refactor the Result classes using a proper Descriptor protocol implementation

Open
#289 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
127
Forks
53
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Result classes in mollie.api.objects.* and the repeated root-property methods described in the issue, then read Python's descriptor protocol documentation. Replace the duplicated property definitions with a generic ResultProperty while preserving current API response behavior; writable properties and update() need scope decisions before they can define completion.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.