This is the full report:
DROP PROCEDURE IF EXISTS sp_DBR_sync_sling_shifts
$$
CREATE PROCEDURE `sp_DBR_sync_sling_shifts`(
inLogin varchar(256),
inParamJson text,
inPostJsonId int(11)
)
BEGIN
declare _stage varchar(45);
set @user = 'hidden';
set @password = 'hidden';
set @url = 'hidden';
set @org_id = hidden;
if json_valid(inParamJson) and inParamJson ->> '$.stage' is not null then
set _stage = inParamJson ->> "$.stage";
else
set _stage = '';
end if;
if _stage = '' then
# get token;
set @header_n = FLOOR(RAND() * (10000000));
set @token_url = concat(@url, 'hidden');
select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'Content-Type: application/json';
select 'dbr.http.header', 'sp_save_http_header', @header_n;
select 'dbr.http.save', 'sp_save_json';
select 'dbr.report', 'sp_DBR_sync_sling_shifts', 'inParamJson=p_json', 'inPostJsonId=id';
select 'dbr.http.post', @token_url, @user as "email", @password as "password", json_object('stage', 'got_token', 'header_id', @header_n) as p_json;
elseif _stage = 'got_token' then
select 'Got Token';
select inPostJsonId;
select inParamJson;
set @header_id = inParamJson ->> "$.header_id";
select * from AhaInfo.http_json where id = inPostJsonId;
select * from AhaInfo.dbr_http_headers where header_id = @header_id;
set @session_url = concat(@url, '/v1/account/session');
set @token = (
select
TRIM(
REPLACE(
REPLACE(
SUBSTRING_INDEX(header_item, ' ', -1),
'\r', ''),
'\n', '')
)
from
AhaInfo.dbr_http_headers
where
header_id = @header_id
and header_item like 'authorization%'
);
set @auth = concat('authorization: ', @token);
select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'content-type: application/json';
select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'accept: application/json';
select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', @auth;
select 'dbr.http.put', @session_url, @org_id as 'orgId', 'silent';
set @dates_param = 'dates=2023-07-01/2023-07-02';
set @v_param = '2';
select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', @auth;
select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'accept: application/json';
select 'dbr.http.save', 'sp_save_json';
select 'dbr.report', 'sp_DBR_sync_sling_shifts', 'inParamJson=p_json', 'inPostJsonId=id';
select 'dbr.http.get', concat(@url, '/v1/reports/roster', '?', @dates_param, '&', @v_param), json_object('stage', 'got_shifts') as p_json;
elseif _stage = 'got_shifts' then
select 'Got shifts';
select * from AhaInfo.http_json where id = inPostJsonId;
end if;
END
$$
I'm always calling the same report using json for parameters to avoid creating multiple reports, causing clutter.