Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gathering scheduled changes in BambooHR #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions PyBambooHR/PyBambooHR.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import datetime
import json
import requests
from . import utils
from .utils import make_field_xml
Expand All @@ -34,7 +35,7 @@ class PyBambooHR(object):
and an optional datatype argument (defaults to JSON). This class implements
methods for basic CRUD operations for employees and more.
"""
def __init__(self, subdomain='', api_key='', datatype='JSON', underscore_keys=False):
def __init__(self, subdomain='', api_key='', onlyCurrent=True, datatype='JSON', underscore_keys=False):
"""
Using the subdomain, __init__ initializes the base_url for our API calls.
This method also sets up some headers for our HTTP requests as well as our authentication (API key).
Expand Down Expand Up @@ -70,6 +71,9 @@ def __init__(self, subdomain='', api_key='', datatype='JSON', underscore_keys=Fa
# Some people will want to use underscore keys for employee data...
self.underscore_keys = underscore_keys

# Ask BambooHR for information that is scheduled in the future
self.onlyCurrent = onlyCurrent

# We are focusing on JSON for now.
if self.datatype == 'XML':
raise NotImplemented("Returning XML is not currently supported.")
Expand Down Expand Up @@ -166,6 +170,9 @@ def __init__(self, subdomain='', api_key='', datatype='JSON', underscore_keys=Fa
"benefitClassDate": ("date", ""),
"benefitClassClass": ("list", ""),
"benefitClassChangeReason": ("list", ""),
"customOrientation": ("list", ""),
"customPreferredDoorDashEmail": ("text", ""),
"customTeam": ("text", "")
}

def _format_employee_xml(self, employee):
Expand Down Expand Up @@ -293,6 +300,10 @@ def get_employee(self, employee_id, field_list=None):
payload = {
'fields': ",".join(get_fields)
}
if self.onlyCurrent == False:
payload.update({
'onlyCurrent': 'false'
})

url = self.base_url + "employees/{0}".format(employee_id)
r = requests.get(url, headers=self.headers, params=payload, auth=(self.api_key, ''))
Expand Down Expand Up @@ -401,7 +412,7 @@ def request_company_report(self, report_id, report_format='json', output_filenam
raise UserWarning("You requested an invalid report type. Valid values are: {0}".format(','.join([k for k in self.report_formats])))

filter_duplicates = 'yes' if filter_duplicates else 'no'
url = self.base_url + "reports/{0}?format={1}&fd={2}".format(report_id, report_format, filter_duplicates)
url = self.base_url + "reports/{0}?format={1}&fd={2}&onlyCurrent={3}".format(report_id, report_format, filter_duplicates, self.onlyCurrent)
r = requests.get(url, headers=self.headers, auth=(self.api_key, ''))
r.raise_for_status()

Expand Down Expand Up @@ -484,11 +495,19 @@ def get_tabular_data(self, table_name, employee_id='all'):
@return A dictionary with employee ID as key and a list of dictionaries, each dictionary showing
the values of the table's fields for a particular date, which is stored by key 'date' in the dictionary.
"""
records = []

url = self.base_url + 'employees/{}/tables/{}'.format(employee_id, table_name)
r = requests.get(url, headers=self.headers, auth=(self.api_key, ''))
r.raise_for_status()

return utils.transform_tabular_data(r.content)
data = json.loads(r.content.decode())

for row in data:
records.append(row[0])

#return utils.transform_tabular_data(r.content)
return records

def get_employee_changes(self, since=None):
"""
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ for employee in result['employees']:
result = bamboo.request_company_report(1, format='pdf', output_file='/tmp/report.pdf', filter_duplicates=True)

```
Getting information that is scheduled in the future
```python
from PyBambooHR import PyBambooHR

bamboo = PyBambooHR(subdomain='yoursub', api_key='yourapikeyhere', onlyCurrent=False)

```
BambooHR has effective dates for when promotions are scheduled to happen or when new hires are going to join the organization. In order to see these events before they happen using the BambooHR API set `onlyCurrent` to `False`. As a note, this only works for pulling reports and getting employee information. This does not work on getting the employee directory.