|
| 1 | +from pathlib import Path |
| 2 | +import requests |
| 3 | +import json |
| 4 | + |
| 5 | +domain = 'your-bamboohr-domain' |
| 6 | +base_url = f'https://api.bamboohr.com/api/gateway.php/{domain}/v1' |
| 7 | + |
| 8 | +api_key = 'your-api-key' |
| 9 | +auth = (api_key, '') |
| 10 | + |
| 11 | +headers = { |
| 12 | + 'Accept': 'application/json' |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +def get_all_employees(): |
| 17 | + try: |
| 18 | + response_all_employees = requests.request('GET', f'{base_url}/employees/directory', headers=headers, auth=auth) |
| 19 | + if response_all_employees.status_code == 200: |
| 20 | + return response_all_employees.text |
| 21 | + else: |
| 22 | + print( |
| 23 | + f'Something went wrong when trying to get the requested info from the API server. Status code: {response_all_employees.status_code}. Message: {response_all_employees.reason}') |
| 24 | + except Exception as e: |
| 25 | + print("An error occurred while performing the API call: ", e) |
| 26 | + |
| 27 | + |
| 28 | +def print_all_employees(employees_json): |
| 29 | + for entry in employees_json: |
| 30 | + print( |
| 31 | + f'{entry["displayName"]}: {entry["jobTitle"]} at {entry["department"]}, based in {entry["location"]} ({entry["division"]})') |
| 32 | + |
| 33 | + |
| 34 | +def save_all_employees_to_file(employees_json): |
| 35 | + destination_dir = 'data' |
| 36 | + Path(destination_dir).mkdir(parents=True, exist_ok=True) |
| 37 | + json_file = f'{destination_dir}/all_employees.json' |
| 38 | + with open(json_file, 'w', encoding='utf-8') as target_file: |
| 39 | + json.dump(employees_json, target_file, ensure_ascii=False, indent=4) |
| 40 | + print(f"Data saved to {json_file}.") |
| 41 | + |
| 42 | + |
| 43 | +all_employees = get_all_employees() |
| 44 | +if all_employees is not None: |
| 45 | + all_employees_json = json.loads(all_employees)['employees'] |
| 46 | + print_all_employees(all_employees_json) |
| 47 | + save_all_employees_to_file(all_employees_json) |
0 commit comments