mercredi 11 mai 2022

Create many class objects with little repetition

I'm working on a Python wrapper for a REST API. The API has many types of endpoints, like Users, Participants, Payments, Calendars, Periods, etc...

Many of these endpoints have the same set of, or a subset of, methods, like: create, create_versions, get, get_versions, list, delete, delete_versions, update and update_versions.

Some of the endpoints do not support versioning, so in those the *_versions methods need to be left out. Other do not allow create or even delete. Finally there are two special cases that need there own implementation entirely.

So far, I've implemented a Client class to store the session and handle the get, post, put, update and delete requests. And an Endpoint class to define the methods above, with a reference to the Client.

Inheriting from the Endpoint class enables all of the methods by default. Not very user friendly.

What is a good design-pattern to implement this use case?

Aucun commentaire:

Enregistrer un commentaire