From bb66c231ef37bb73b8a3fc77439078fb2993399b Mon Sep 17 00:00:00 2001 From: Nikolay Ishiev Date: Wed, 21 Apr 2021 23:02:52 +0300 Subject: [PATCH] Add new REST method call variants --- bitrix24/bitrix24.py | 89 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 16 deletions(-) diff --git a/bitrix24/bitrix24.py b/bitrix24/bitrix24.py index 27031e3..35471b6 100644 --- a/bitrix24/bitrix24.py +++ b/bitrix24/bitrix24.py @@ -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) @@ -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 + \ No newline at end of file