FUNCTION get_audio(p_text IN VARCHAR2) RETURN BLOB IS
l_req_blob BLOB;
l_res_blob BLOB;
l_url VARCHAR2(32767) := get_tts_url();
l_cuid VARCHAR2(32767);
l_req utl_http.req;
l_res utl_http.resp;
BEGIN
SELECT instance || '-' || '-' || userenv('sessionid')
INTO l_cuid
FROM v$thread;
l_url := REPLACE(l_url, '#text#', p_text || chr(38));
l_url := REPLACE(l_url, '#token#', get_access_token);
l_url := REPLACE(l_url, '#cuid#', l_cuid);
log(l_url);
l_req := utl_http.begin_request(l_url, 'GET');
dbms_lob.createtemporary(lob_loc => l_req_blob, cache => FALSE);
utl_http.write_raw(l_req, l_req_blob);
l_res := utl_http.get_response(l_req);
dbms_output.put_line('Response> status_code: "' || l_res.status_code || '"');
dbms_output.put_line('Response> reason_phrase: "' ||
l_res.reason_phrase || '"');
dbms_output.put_line('Response> http_version: "' || l_res.http_version || '"');
DECLARE
l_raw_data RAW(512);
--chunk_size CONSTANT INTEGER := 512;
BEGIN
dbms_lob.createtemporary(l_res_blob, FALSE);
LOOP
BEGIN
utl_http.read_raw(l_res, l_raw_data);
IF l_raw_data IS NOT NULL THEN
dbms_lob.writeappend(l_res_blob,
utl_raw.length(l_raw_data),
l_raw_data);
END IF;
EXCEPTION
WHEN utl_http.end_of_body THEN
utl_http.end_response(l_res);
EXIT;
END;
END LOOP;
END;
IF l_req.private_hndl IS NOT NULL THEN
utl_http.end_request(l_req);
END IF;
IF l_res.private_hndl IS NOT NULL THEN
utl_http.end_response(l_res);
END IF;
--dbms_lob.freetemporary(l_res_blob);
RETURN l_res_blob;
END;