generate_par

CREATE OR REPLACE FUNCTION generate_par(
    p_base_url           IN VARCHAR2,
    p_tenant             IN VARCHAR2,
    p_idcs_token         IN VARCHAR2,
    p_os_prefix          IN VARCHAR2,
    p_file_name          IN VARCHAR2
) RETURN VARCHAR2
IS
    l_response     CLOB;
    l_req_body     CLOB;
    l_access_uri   VARCHAR2(32767);
BEGIN
    -- Create the JSON request body for FTS invocation
    l_req_body := create_par_request_body(p_os_prefix, p_file_name);
 
    -- Get PAR from FTS service
    l_response := apex_web_service.make_rest_request(
        p_url         => p_base_url || '/' || p_tenant || '/RetailAppsReSTServices/services/private/FTSWrapper/upload',
        p_http_method => 'POST',
        p_body        => l_req_body,
        p_headers     => apex_web_service.g_request_headers
    );
 
    -- Check if the response is not null
    IF l_response IS NULL THEN
        RAISE_APPLICATION_ERROR(-20001, 'Failed to generate PAR: null response');
    END IF;
 
    -- Parse the response
    BEGIN
        APEX_JSON.parse(l_response);
    EXCEPTION
        WHEN OTHERS THEN
            RAISE_APPLICATION_ERROR(-20002, 'Failed to parse PAR response: ' || SQLERRM);
    END;
 
    -- Get the access URI from the response
    l_access_uri := APEX_JSON.get_varchar2(p_path => 'parList[%d].accessUri', p0 => 1);
 
    -- Check if the access URI is not null
    IF l_access_uri IS NULL THEN
        RAISE_APPLICATION_ERROR(-20003, 'Failed to generate PAR: null access URI');
    END IF;
 
    RETURN l_access_uri;
EXCEPTION
    WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(-20004, 'Failed to generate PAR: ' || SQLERRM);
END generate_par;