I have a lot of classes that are just a CRUD interface for microservices. They only have a param for the endpoint and then some of the methods get_list/get_item/create/update/delete/activate/deactivate. To be DRY, the methods that are necessary for the microservice calls are all grouped in a BaseAPI class. A small example would be like that:
class GetItem(BaseAPI): API_ENDPOINT = 'www.example.com' @classmethod def get_list(cls): return cls.get(cls.path(cls.API_ENDPOINT)) @classmethod def create(cls, data): return cls.post(cls.path(cls.API_ENDPOINT,data))
and so on.
What I see here is that I just created an additional layer and my
update etc. methods, might as well live inside the
BaseAPI and then I would only have to add the endpoint. On the other hand, not all microservices support all the methods and I wouldn’t want the classes to have methods that are not supported.
One way to solve it could be to pretend that the methods are private by adding an underscore and then just extend the ones that are supported. But this doesn’t really solve the DRY part since I still have to write a new method for every supported method.
Another solution I thought of is to have one class for every method and use multiple inheritance to define which methods are supported.
class BaseGetList(): @classmethod def get_list(cls): return cls.get(cls.path(cls.API_ENDPOINT)) class BaseCreate(): @classmethod def create(cls, data): return cls.post(cls.path(cls.API_ENDPOINT,data)) class GetItem(BaseAPI, BaseGetList, BaseCreate): API_ENDPOINT = 'www.example.com'
I am being tempted to go towards the second solution but I am a bit concerned if it is easily understandable, developer friendly, acceptable as a practice and if there are caveats that I don’t see.
Of course any other suggestion is welcome.