From 0b769ca9d6f119d04df56ef5a64645532f6b56aa Mon Sep 17 00:00:00 2001 From: Friedrich ten Hagen Date: Thu, 28 Nov 2024 10:06:24 +0100 Subject: [PATCH] Move logic to helper functions to comply with cyclomatic complexity threshold. --- src/personio_py/client.py | 57 ++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/personio_py/client.py b/src/personio_py/client.py index dd99ea7..78517f3 100644 --- a/src/personio_py/client.py +++ b/src/personio_py/client.py @@ -186,30 +186,51 @@ def request_paginated(self, path: str, method='GET', params: Dict[str, Any] = No response = self.request_json(path, method, params, data, auth_rotation=auth_rotation) resp_data = response.get('data') if resp_data: - if url_type == 'absence': - data_acc.extend(resp_data) - if response['metadata']['current_page'] == response['metadata']['total_pages']: - break - else: - params['offset'] += 1 - elif url_type == 'attendance': - if params['offset'] >= response['metadata']['total_elements']: - break - else: - data_acc.extend(resp_data) - params['offset'] += limit - elif url_type == 'employees': - data_acc.extend(resp_data) - if response['metadata']['current_page'] + 1 == response['metadata']['total_pages']: - break - else: - params['offset'] += 1 + if url_type == 'absence' and self._handle_absence_pagination( + data_acc, resp_data, response, params + ): + break + elif url_type == 'attendance' and self._handle_attendance_pagination( + data_acc, resp_data, response, params + ): + break + elif url_type == 'employees' and self._handle_employees_pagination( + data_acc, resp_data, response, params + ): + break else: break # return the accumulated data response['data'] = data_acc return response + def _handle_absence_pagination(self, data_acc, resp_data, response, params) -> bool: + """Handle pagination logic for absences.""" + data_acc.extend(resp_data) + if response['metadata']['current_page'] == response['metadata']['total_pages']: + return True + params['offset'] += 1 + return False + + def _handle_attendance_pagination(self, data_acc, resp_data, response, params) -> bool: + """Handle pagination logic for attendance.""" + if params['offset'] >= response['metadata']['total_elements']: + return True + data_acc.extend(resp_data) + params['offset'] += params['limit'] + return False + + def _handle_employees_pagination(self, data_acc, resp_data, response, params): + """Handle pagination logic for employees.""" + data_acc.extend(resp_data) + total_pages = response['metadata']['total_pages'] + current_page = response['metadata']['current_page'] + if current_page + 1 == total_pages: + return True + else: + params['offset'] += 1 + return False + def request_image(self, path: str, method='GET', params: Dict[str, Any] = None, auth_rotation=False) -> Optional[bytes]: """