from dlubal.api import rfem
import time

def request_counts_start():
    with rfem.Application() as rfem_app:

        info= rfem_app.get_application_info()
        print(info)

        info = rfem_app.get_subscription_info()

        if info.api_requests_count > info.api_requests_limit:
            message = 'WARNING: Number of requests exceeds the limit.'
        elif info.api_requests_count >= info.api_requests_limit * 0.8:
            message = 'INFO: Number of requests approaching limit.'
        else:
            message = 'INFO: Number of inquiries fits the plan'
        print(f'{message}')

        # Get and set base_data
        base_data = rfem_app.get_base_data()
        base_data.addons.glass_design_active=True
        rfem_app.set_base_data(base_data=base_data)
        print(f'base_data.model_name: {base_data.main.model_name}')
        rfem_app.calculate_all(skip_warnings=True)
        return info.api_requests_count

def request_counts_end():
    with rfem.Application() as rfem_app:
        info = rfem_app.get_subscription_info()
        return info.api_requests_count

def main():
    try:
        counts_start = request_counts_start()
        sec=70
        # Wait for 70 seconds to allow for any sync delays
        for sec in range(sec, 0, -1):
            print(f"{sec}s", end=" ", flush=True)
            time.sleep(1)
        print()
        counts_end = request_counts_end()
        count_difference = counts_end - counts_start
        print(f'Request count at start and end may be the same due to sync delay.')
        print(f'Request counts: start={counts_start}, end={counts_end} => difference={count_difference}')
    except Exception as exc:
        print("Error during execution::", exc)

if __name__ == '__main__':
    main()