Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output as generator with lazy-loading pagination #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ pip-log.txt
#Mr Developer
.mr.developer.cfg

pipedrive/ve/
pipedrive/ve/

__pycache__
30 changes: 26 additions & 4 deletions pipedrive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _request(self, endpoint, data, method='POST'):
response, data = self.http.request(uri, method=method, headers={'Content-Type': 'application/x-www-form-urlencoded'})
else:
uri = PIPEDRIVE_API_URL + endpoint + '?api_token=' + str(self.api_token)
response, data = self.http.request(uri, method=method, body=urlencode(data), headers={'Content-Type': 'application/x-www-form-urlencoded'})
response, data = self.http.request(uri, method=method, body=json.dumps(data), headers={'Content-Type': 'application/json'})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"We recommend using JSON body format when performing API requests" https://developers.pipedrive.com/docs/api/v1/#/

Copy link

@bieli bieli Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can see general issues with Pipedrive API server implementation (wrong specification of API server - they rely on "Content-Type" header instead of "Accept" - look to RFC: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept):

  1. When using requests Python library, I can see HTML instead of JSON the same with curl
  2. Documentation doesn't follow HTTP RFC in case of headers request specification, what is wrong today (maybe 20 years ago it would have been fine, but not in 2024!) -> https://pipedrive.readme.io/docs/core-api-concepts-requests

Sometimes junior developers needs to listen critical comments carefully :-)
This is not comment to you @kevhill - this is for Pipedrive API server developers :-)


logger.debug('sending {method} request to {uri}'.format(
method=method,
Expand Down Expand Up @@ -64,8 +64,30 @@ def __init__(self, email, password=None):

def __getattr__(self, name):
def wrapper(data={}, method='GET'):

response = self._request(name.replace('_', '/'), data, method)
if 'error' in response:
raise PipedriveError(response)
return response

def _generator():
if 'error' in response:
raise PipedriveError(response)

additional_data = response.get('additional_data', {})
pagination_info = additional_data.get('pagination', {})

logger.debug('pagination_info: {}'.format(pagination_info))
if isinstance(response['data'], dict):
yield response['data']
else:
yield from response['data']

if pagination_info.get('more_items_in_collection', False):

data.update({
'start': pagination_info['next_start']
})

yield from wrapper(data, method)

return _generator()

return wrapper
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='python-pipedrive',
version="0.4.0",
version="0.5.1",
license="MIT",

install_requires=[
Expand Down