jscott1989 / python-pipedrive

Python API for interacting with the pipedrive.com API
MIT License
93 stars 43 forks source link

List updates about a deal #27

Open lamazze opened 1 year ago

lamazze commented 1 year ago

Hello all, I'm trying to get the update for a specific deal. The API looks like this: GET /v1/deals/{id}/flow

How do get it ?

params = {
    'id': 13608
}
response = pipedrive.deals(params, method = 'GET').???

Thank you very much BR

lamazze commented 1 year ago

Hello again, I'm new to Python but I've done a bit of debugging. Obviously you can use a method with a underscore and it will be translated in a slash: pipedrive.persons_search() will create a request with /persons/search.

But in order to get the flow, you need a trailing path "/flow" ie after the data.

I've modified the code as follow (look for trailingpath in the code):

def _request(self, endpoint, data, method='POST', trailingpath=None):
        # avoid storing the string 'None' when a value is None
        data = {k: "" if v is None else v for k, v in data.items()}
        if method == "GET":
            uri = PIPEDRIVE_API_URL + endpoint
            if trailingpath:
                if 'id' in data:
                    putid = data.pop('id')
                    uri += '/' + str(putid) + trailingpath
                else:
                    uri += trailingpath
            uri += '?api_token=' + str(self.api_token)
            if data:
                uri += '&' + urlencode(data)
            response, data = self.http.request(uri, method=method, headers={'Content-Type': 'application/x-www-form-urlencoded'})
[....]
    def __getattr__(self, name):
        def wrapper(data={}, method='GET', trailingpath=None):
            response = self._request(name.replace('_', '/'), data, method, trailingpath)
            if 'error' in response:
                raise PipedriveError(response)
            return response
        return wrapper

I would submit my modifications but as I said, I'm pretty new to Python so I don't know if the whole idea is not garbage and/or the code modification makes sense.

PS: I've added the trailing path only for the GET method, not sure it's useful for POST/PUT

Your opinion ?