i have a problem extracting information from API and saving that into excel wioth
could someone give me an info why excel is with Employee Email', 'Holiday Name', 'Start Date', 'End Date but all data are empty pls
Code:
import requests
import base64
import openpyxl
def pull_email_list():
base_url = 'https://ID_calamari.calamari.io/api/'
username = 'calamari'
password = 'qkAPIKEYJ'
# Get data from date stamp
date_from_global = '2024-01-01'
date_to_global = '2024-12-31'
# Get data from API
req = 'employees/v1/list'
response = get_post_response(req, {"page": 0}, username, password, base_url)
list_length = response['totalPages']
employee_info = []
for i in range(list_length):
response = get_post_response(req, {"page": i}, username, password, base_url)
employees = response['employees']
for employee in employees:
holidays = pull_public_holidays_for_employee_dates(employee['email'], date_from_global, date_to_global, username, password, base_url)
for holiday in holidays:
employee_info.append([employee['email'], holiday['name'], holiday['start'], holiday['end']])
return employee_info
def pull_public_holidays_for_employee_dates(employee_email, date_from, date_to, username, password, base_url):
req = 'holiday/v1/find'
data = {
'employee': employee_email,
'from': date_from,
'to': date_to
}
response = get_post_response(req, data, username, password, base_url)
return response
def get_post_response(req, data, username, password, base_url):
auth_header = 'Basic ' + base64.b64encode((username + ':' + password).encode()).decode()
headers = {'Authorization': auth_header, 'Content-Type': 'application/json'}
response = requests.post(base_url + req, headers=headers, json=data)
return response.json()
def write_to_excel(data, filename):
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['Employee Email', 'Holiday Name', 'Start Date', 'End Date'])
for item in data:
ws.append(item)
wb.save(filename)
def main():
employee_info = pull_email_list()
write_to_excel(employee_info, 'calamari_leave_data.xlsx')
print("Data has been successfully saved to calamari_leave_data.xlsx")
if __name__ == "__main__":
main()
could someone give me an info why excel is with Employee Email', 'Holiday Name', 'Start Date', 'End Date but all data are empty pls