joeirimpan / zammad_py

Python API client for accessing zammad REST API
MIT License
29 stars 30 forks source link

[New Feature] Add instanciation of Zammad Objects #208

Open Anuril opened 1 year ago

Anuril commented 1 year ago

Adding the possibility to instanciate Zammad Objects, instead of having to handle dicts. (See Issue #202)

joeirimpan commented 1 year ago

Easiest way to do this by having the response saved in a dict of Resource class and return instance of resource class itself. Then we can have methods like save / destroy which act on these resource class.

Check ruby code here.

class Resource(ABC):
    ....
    def find(self, id):
        response = self._connection.session.get(self.url + "/%s" % id)
        data = {}
        try:
            data = self._raise_or_return_json(response)
        except HTTPError:
            raise
        else:
            self._new_instance = False
            self._attributes = data
        return self

    def save(self):
        ...

    def destroy(self):
        ...
jvllmr commented 1 year ago

How about using Pydantic (https://docs.pydantic.dev/) for representing the data? It's main purpose is data validation but it's also easy to parse and represent JSON data with its model classes and converting them back to a dict.