100%macro mv_getjobresult(uri=0
101 ,access_token_var=ACCESS_TOKEN
102 ,grant_type=sas_services
108%
if &mdebug=1 %then %
do;
109 %put &sysmacroname entry vars:;
114%
if &grant_type=detect %then %
do;
115 %
if %symexist(&access_token_var) %then %let grant_type=authorization_code;
116 %
else %let grant_type=sas_services;
118%
if &grant_type=sas_services %then %
do;
119 %let oauth_bearer=oauth_bearer=sas_services;
120 %let &access_token_var=;
123%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
124 and &grant_type ne sas_services
127 ,msg=%str(Invalid value
for grant_type: &grant_type)
135 if length(uri)<12 then
do;
136 call symputx(
'errflg',1);
137 call symputx(
'errmsg',
"URI is invalid (too short) - '&uri'",
'l');
139 if scan(uri,-1)=
'state' or scan(uri,1) ne 'jobExecution' then do;
140 call symputx('errflg',1);
141 call symputx('errmsg',
142 "URI should be in format /jobExecution/jobs/$$$$UUID$$$$"
143 !!
" but is actually like: &uri",
'l');
147%mp_abort(iftrue=(&errflg=1)
152%if &outref ne 0 and %mf_existfileref(&outref) ne 1 %then %do;
153 filename &outref temp;
156options noquotelenmax;
158%let base_uri=%mf_getplatform(VIYARESTAPI);
162%let fname1=%mf_getuniquefileref();
163proc http method='GET' out=&fname1 &oauth_bearer
165 headers
"Accept"=
"application/json"
166 %if &grant_type=authorization_code %then %do;
167 "Authorization"=
"Bearer &&&access_token_var"
171%if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
173 data _null_;infile &fname1;input;putlog _infile_;run;
174 %mp_abort(mac=&sysmacroname
175 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
178%if &mdebug=1 %then %do;
180 infile &fname1 lrecl=32767;
187%local lib1 resuri resname resfall want;
189%let want=%upcase(_&result);
190%let lib1=%mf_getuniquelibref();
191libname &lib1 JSON fileref=&fname1;
201 array _chars{*} _character_;
202 length _name $32 _val $512;
203 do _i=1 to dim(_chars);
204 _name=vname(_chars{_i});
205 if upcase(_name)=symget(
'want') then do;
210 if _val eq
'' then
do _i=1 to dim(_chars);
211 _name=vname(_chars{_i});
212 if _chars{_i}=:
'/files/' and upcase(_name)=:
'_WEBOUT' then
do;
214 call symputx(
'resfall',1,
'l');
218 if _val ne
'' then
do;
219 call symputx(
'resuri',_val,
'l');
220 call symputx(
'resname',_name,
'l');
222 %
if &mdebug=1 %then %
do;
223 putlog
"&sysmacroname results: " (_all_)(=);
226%
if &mdebug=1 and &resfall=1 %then %
do;
227 %put &sysmacroname: _&result was not registered,
using &resname;
229%
if "%superq(resuri)" eq
"" or
"%superq(resuri)" eq
"." %then %
do;
236 %put &sysmacroname: _&result not found in job results -;
237 %put &sysmacroname: use mv_getjoblog to fetch the job log instead;
238 %
if &outref ne 0 %then %
do;
241 put
"&sysmacroname: no &_result result available for job &uri";
244 %
if &outlib ne 0 %then %
do;
246 %let fname3=%mf_getuniquefileref();
251 libname &outlib JSON fileref=&fname3;
253 %
if &mdebug=0 %then %
do;
254 filename &fname1 clear;
262%let fname2=%mf_getuniquefileref();
263proc http method=
'GET' out=&fname2 &oauth_bearer
264 url=
"&base_uri&resuri/content?limit=10000";
265 headers
"Accept"=
"application/json"
266 %
if &grant_type=authorization_code %then %
do;
267 "Authorization"=
"Bearer &&&access_token_var"
271%
if &mdebug=1 %then %
do;
274 infile &fname2 recfm=n;
276 putlog
char $char1. @;
280%
if &outref ne 0 %then %
do;
281 filename &outref temp;
282 %mp_binarycopy(inref=&fname2,outref=&outref)
284%
if &outlib ne 0 %then %
do;
297 %local path2 path3 begvar endvar nil fref3 fref4 fid size2 size4;
298 %let fref3=%mf_getuniquefileref();
299 %let fref4=%mf_getuniquefileref();
300 %let path2=%sysfunc(pathname(&fname2));
301 %let path3=%sysfunc(pathname(&fref3));
302 %let begvar=%str(>>weboutBEGIN<<);
303 %let endvar=%str(>>weboutEND<<);
305 %mp_replace(infile=
"&path2",findvar=begvar,replacevar=nil
307 %mp_replace(infile=
"&path3",findvar=endvar,replacevar=nil
308 ,outfile=
"%sysfunc(pathname(&fref4))")
311 %
if %sysfunc(fexist(&fref4)) ne 1 %then %
do;
312 %mp_abort(mac=&sysmacroname
313 ,msg=%str(Unable to strip the result envelope
for &uri)
316 %let fid=%sysfunc(fopen(&fname2));
317 %let size2=%sysfunc(finfo(&fid,File Size (bytes)));
318 %let fid=%sysfunc(fclose(&fid));
319 %let fid=%sysfunc(fopen(&fref4));
320 %let size4=%sysfunc(finfo(&fid,File Size (bytes)));
321 %let fid=%sysfunc(fclose(&fid));
322 %
if &size4 < &size2 %then %
do;
326 %
if &size4=0 %then %
do;
332 libname &outlib JSON fileref=&fref4;
335 libname &outlib JSON fileref=&fname2;
337 filename &fref3 clear;
338 filename &fref4 clear;
341%
if &mdebug=0 %then %
do;
342 filename &fname1 clear;
343 filename &fname2 clear;
347 %put &sysmacroname exit vars:;
351%mend mv_getjobresult;