Skip to content

Commit

Permalink
added new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo committed Dec 20, 2023
1 parent 0427fac commit a9b6ebd
Showing 1 changed file with 62 additions and 10 deletions.
72 changes: 62 additions & 10 deletions bamboosdk/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a9b6ebd

Please sign in to comment.