Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Add last changed API #51

Open
wants to merge 1 commit 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
24 changes: 24 additions & 0 deletions PyBambooHR/PyBambooHR.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,30 @@ def get_meta_users(self):

return r.json()

def get_last_changed(self, iso_timestamp):
"""
API method for discovering which employees have been recently added, changed or deleted.
https://www.bamboohr.com/api/documentation/changes.php
@param iso_timestamp: get last changed since this ISO 8601 timestamp

@return: dictionary with employees which have been changed recently
"""

url = self.base_url + "employees/changed/"
payload = {
'since': "{}".format(iso_timestamp)
}
# Convert payload to stop percent encoding the URL for requests
payload_str = "&".join("%s=%s" % (k,v) for k,v in payload.items())

r = requests.get(url, timeout=self.timeout, headers=self.headers, params=payload_str, auth=(self.api_key, ''))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you pass params=payload instead of building the string. Compare https://requests.readthedocs.io/en/master/user/quickstart/#passing-parameters-in-urls

r.raise_for_status()

last_changed = r.json()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible crash bug if r.text is not json parsable, e.g., timeout.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I'd open a different issue for to improve the code because this affects all API methods. @vahedq could you open an new issue with this? And if you have a fix at hand, a PR would be highly appreciated 😬

Nevertheless, it is expected that BambooHR answers with a JSON, so not critical.


return last_changed


def _query(self, url, params, raw=False):
url = self.base_url + url
r = requests.get(url, timeout=self.timeout, params=params, headers=self.headers, auth=(self.api_key, ''))
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,20 @@ bamboo = PyBambooHR(subdomain='yoursub', api_key='yourapikeyhere', only_current=

```
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 `only_current` to `False`. As a note, this only works for pulling reports and getting employee information. This does not work on getting the employee directory.


Getting last changed
```python
from PyBambooHR import PyBambooHR

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

# Date in ISO 8601, 15 minutes ago
since_time = (datetime.datetime.now() - datetime.timedelta(minutes=15)).replace(microsecond=0)
since_time_iso = since_time.strftime('%Y-%m-%dT%H:%M:%SZ')

result = get_last_changed(since_time_iso)

```

Note: Last change timestamps are a newer feature in BambooHR, and they only started recording these dates from June 5th, 2011.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2011 is quite some time ago. I think it's fine to drop this info from the README 😉