From a9b6ebdf390f73e112dbab121a6f80854379f807 Mon Sep 17 00:00:00 2001 From: Ashutosh619-sudo Date: Wed, 20 Dec 2023 13:46:10 +0530 Subject: [PATCH] added new methods --- bamboosdk/api/api_base.py | 72 +++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/bamboosdk/api/api_base.py b/bamboosdk/api/api_base.py index 0da8761c..e2dd6ffc 100644 --- a/bamboosdk/api/api_base.py +++ b/bamboosdk/api/api_base.py @@ -3,9 +3,6 @@ import json from bamboosdk.exceptions import * - -payload = { "fields": ["displayName", "firstName", "lastName", "department", "workEmail", "supervisorEmail", "status"] } - class ApiBase: """ Base class for all API classes @@ -17,7 +14,38 @@ def __init__(self) -> None: self.__sub_domain = None self.headers = None - def _post_request(self, module_api_path): + def _get_request(self, module_api_path): + """ + HTTP get method get data from BambooHR API URL + + Parameters: + module_api_path (str): URL of BambooHR API + """ + + url = self.API_BASE_URL.format(self.__sub_domain) + module_api_path + response = requests.get(url=url, headers=self.headers) + if response.status_code == 200: + result = json.loads(response.text) + return result + + if response.status_code == 403: + error_msg = json.loads(response.text) + raise NoPrivilegeError('Forbidden, the user has insufficient privilege', error_msg) + + if response.status_code == 404: + error_msg = json.loads(response.text) + raise NotFoundItemError('Not found item with ID', error_msg) + + if response.status_code == 401: + error_msg = 'The api token is invalid' + raise InvalidTokenError('Invalid token, try to refresh it', error_msg) + + else: + error_msg = json.loads(response.text) + raise BambooHrSDKError('Status code {0}'.format(response.status_code), error_msg) + + + def _post_request(self, module_api_path, payload): """ HTTP post method to send data to BambooHR API URL @@ -44,15 +72,39 @@ def _post_request(self, module_api_path): error_msg = 'The api token is invalid' raise InvalidTokenError('Invalid token, try to refresh it', error_msg) - if response.status_code == 500: + else: + error_msg = json.loads(response.text) + raise BambooHrSDKError('Status code {0}'.format(response.status_code), error_msg) + + def _delete_request(self, module_api_path): + """ + HTTP delete method to delete resource on BambooHR + + Parameters: + module_api_path (str): URL of BambooHR API + """ + url= self.API_BASE_URL.format(self.__sub_domain) + module_api_path + response = requests.delete(url=url, headers=self.headers) + if response.status_code == 200: + result = json.loads(response.text) + return result + + if response.status_code == 403: + error_msg = json.loads(response.text) + raise NoPrivilegeError('Forbidden, the user has insufficient privilege', error_msg) + + if response.status_code == 404: error_msg = json.loads(response.text) - raise InternalServerError('Internal server error', error_msg) + raise NotFoundItemError('Not found item with ID', error_msg) - raise BambooHrSDKError( - 'Status code {0}'.format(response.status_code), response.text - ) + if response.status_code == 401: + error_msg = 'The api token is invalid' + raise InvalidTokenError('Invalid token, try to refresh it', error_msg) - + else: + error_msg = json.loads(response.text) + raise BambooHrSDKError('Status code {0}'.format(response.status_code), error_msg) + def __encode_username_password(self): """ Utility method to be used in the header for authorization