API to excel dont show data

Joined
Feb 9, 2024
Messages
1
Reaction score
0
i have a problem extracting information from API and saving that into excel wioth

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top