Skip to content

Commit

Permalink
Merge branch 'bamboo_sdk_employee' into bamboo_sdk_main
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo committed Dec 20, 2023
2 parents 8267cc3 + e792302 commit 6979dc2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 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
3 changes: 2 additions & 1 deletion bamboosdk/api/employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
class Employee(ApiBase):

GET_EMPLOYEE_REPORT = '/v1/reports/custom?format=JSON&onlyCurrent=false'
payload = { "fields": ["displayName", "firstName", "lastName", "department", "workEmail", "supervisorEmail", "status"] }

def get_all(self):
"""Get the list of employees from bambooHr
Returns:
List with dicts in Employee schema.
"""
return self._post_request(self.GET_EMPLOYEE_REPORT)
return self._post_request(self.GET_EMPLOYEE_REPORT, payload=self.payload)

0 comments on commit 6979dc2

Please sign in to comment.