Skip to content

Commit

Permalink
bambbo sdk base api class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo committed Dec 11, 2023
1 parent 3812a71 commit e7dfcb3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Empty file added bamboosdk/__init__.py
Empty file.
Empty file added bamboosdk/api/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions bamboosdk/api/api_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import requests
import base64
import json
from bamboosdk.exceptions import *

class ApiBase:
"""
Base class for all API classes
"""
API_BASE_URL = 'https://api.bamboohr.com/api/gateway.php/{}'

def __init__(self) -> None:
self.__api_token = None
self.__sub_domain = None

def post_request(self, module_api_path):
"""
HTTP post method to send data to BambooHR API URL
Parameters:
payload (dict): Data to be sent to Bamboo API
module_api_path (str): URL of BambooHR API
"""

headers = {
"content-type": "application/json",
"authorization": f"Basic {self.__encode_username_password()}"
}

payload = { "fields": ["displayName", "firstName", "lastName", "department", "workEmail", "supervisorEmail", "status"] }

url= self.API_BASE_URL.format(self.__sub_domain) + module_api_path
response = requests.post(url=url, json=payload, headers=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)


def __encode_username_password(self):
"""
Utility method to be used in the header for authorization
converts the api token and password to base64
"""

payload = f'{self.__api_token}:a'
payload = payload.encode()

return base64.b64encode(payload).decode()

def set_api_token(self, api_token):
self.__api_token = api_token

def set_sub_domain(self, sub_domain):
self.__sub_domain = sub_domain
27 changes: 27 additions & 0 deletions bamboosdk/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
BambooHR SDK exceptions
"""


class BambooHrSDKError(Exception):
"""
The base exception class for BambooHR SDK
"""

def __init__(self, msg, response=None):
super(BambooHrSDKError, self).__init__(msg)
self.message = msg
self.response = response

def __str__(self):
return repr(self.message)


class NoPrivilegeError(BambooHrSDKError):
"""The user has insufficient privilege, 403 error."""

class NotFoundItemError(BambooHrSDKError):
"""Not found the item from URL, 404 error."""

class InvalidTokenError(BambooHrSDKError):
"""Invalid or non-existing access token, 401 error"""

0 comments on commit e7dfcb3

Please sign in to comment.