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

Add new REST method call variants #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
89 changes: 73 additions & 16 deletions bitrix24/bitrix24.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,8 @@ def _prepare_params(self, params, prev=''):
ret += "{0}={1}&".format(key, value)
return ret

def callMethod(self, method, **params):
"""Calls a REST method with specified parameters.

:param url: REST method name.
:param \*\*params: Optional arguments which will be converted to a POST request string.
:return: Returning the REST method response as an array, an object or a scalar
"""

def _call_method(self, method, **params):
"""Calls a REST method with specified parameters."""
try:
url = '{0}/{1}.json'.format(self.domain, method)

Expand All @@ -93,19 +87,82 @@ def callMethod(self, method, **params):
raise BitrixError(r)
# Looks like we need to wait until expires limitation time by Bitrix24 API
sleep(2)
return self.callMethod(method, **params)
return self._call_method(method, **params)

if 'error' in r:
raise BitrixError(r)

return r


def callMethodIter(self, method, **params):
"""Calls a REST method with specified parameters.

:param url: REST method name.
:param \*\*params: Optional arguments which will be converted to a POST request string.
:return: Returning the REST method response generator
"""
if 'start' not in params:
params['start'] = 0

r = self._call_method(method, **params)

if 'next' in r and r['total'] > params['start']:
params['start'] += 50
data = self.callMethod(method, **params)
if isinstance(r['result'], dict):
result = r['result'].copy()
result.update(data)
yield from self.callMethodIter(method, **params)

yield r['result']


def callMethod(self, method, **params):
"""Calls a REST method with specified parameters.

:param url: REST method name.
:param \*\*params: Optional arguments which will be converted to a POST request string.
:return: Returning the REST method response as list, dict or scalar
"""

result_dict = {}
result_list = []

for r in self.callMethodIter(method, **params):
if (isinstance(r, dict)):
result_dict.update(r)
elif (isinstance(r, list)):
result_list.extend(r)

return result_dict or result_list or r


def callMethodList(self, method, id=0, key=None, **params):
"""Calls a REST method with specified parameters and fast fetch list of all records.

:param url: REST method name.
:param id: id of start record.
:param key: Result access key, used if method result is a dict, not a list.
:param \*\*params: Optional arguments which will be converted to a POST request string.
:return: Returning the REST method response list of records
"""
params['start'] = -1
params['order'] = {'ID': 'asc'}

if 'filter' not in params:
params['filter'] = {}

result = []

while True:
params['filter'].update({'>ID': id})

r = self._call_method(method, **params)['result']
if isinstance(r, dict):
r = r.get(key, [])

if r:
result.extend(r)
id = r[-1]['id']
else:
result = r['result'] + data
return result
return r['result']
break

return result