<?php

function get_olm_token()
{

    $olm_api = get_option('olm_url');
    $olm_USERNAME = get_option('olm_user');
    $olm_PASSWD = get_option('olm_password');
    $authRequest = array("username" => $olm_USERNAME, "password" => $olm_PASSWD);
    try {
        $response = post_request($olm_api . "/login",["Content-Type: application/json"],$authRequest );
    } catch (Exception $e) {
        log_msg("❌ ERROR: OLM Login failed " . $e->getMessage());
        return null;
    }

    set_transient('olm_bearer_token', $response, 90);
    return $response;
}

function cache_get_olm_token()
{
    $token = get_transient('olm_bearer_token');
    if ($token === false) {
        return get_olm_token();
    }
    return $token;
}
