Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
ms_triggerstp.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Triggers a SASjs Server STP using the /SASjsApi/stp/trigger endpoint
4 @details Triggers the STP and returns the sessionId
5
6 Example:
7
8 %ms_triggerstp(/some/stored/program
9 ,debug=131
10 ,outds=work.myresults
11 )
12
13 @param [in] pgm The full path to the Stored Program in SASjs Drive (_program
14 parameter)
15 @param [in] debug= (131) The value to supply to the _debug URL parameter
16 @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
17 @param [in] inputparams=(_null_) A dataset containing name/value pairs in the
18 following format:
19 |name:$32|value:$10000|
20 |---|---|
21 |stpmacname|some value|
22 |mustbevalidname|can be anything, oops, %abort!!|
23 @param [in] inputfiles= (_null_) A dataset containing fileref/name/filename in
24 the following format:
25 |fileref:$8|name:$32|filename:$256|
26 |---|---|--|
27 |someref|some_name|some_filename.xls|
28 |fref2|another_file|zyx_v2.csv|
29 @param [in] expiresaftermins= (15) The number of minutes to retain the session
30 folder after the session ends.
31
32 @param [out] outds= (work.ms_triggerstp) Set to the name of a dataset to
33 contain the sessionId. If this dataset already exists, and contains the
34 sessionId, it will be appended to.
35 Format:
36 |sessionId:$36|
37 |---|
38 |20241028074744-54132-1730101664824|
39
40 <h4> SAS Macros </h4>
41 @li mf_getuniquefileref.sas
42 @li mf_getuniquelibref.sas
43 @li mf_getuniquename.sas
44 @li mp_abort.sas
45 @li mp_dropmembers.sas
46 @li mf_nobs.sas
47
48 @sasjslint {"maxLineLength": 83}
49**/
50
51%macro ms_triggerstp(pgm
52 ,debug=131
53 ,inputparams=_null_
54 ,inputfiles=_null_
55 ,expiresAfterMins=15
56 ,outds=work.ms_triggerstp
57 ,mdebug=0
58 );
59 %local dbg mainref authref boundary libref triggered_sid;
60 %let mainref=%mf_getuniquefileref();
61 %let authref=%mf_getuniquefileref();
62 %let boundary=%mf_getuniquename();
63 %if &inputparams=0 %then %let inputparams=_null_;
64
65 %if &mdebug=1 %then %do;
66 %put &sysmacroname entry vars:;
67 %put _local_;
68 %end;
69 %else %let dbg=*;
70
71
72 %mp_abort(iftrue=("&pgm"="")
73 ,mac=&sysmacroname
74 ,msg=%str(Program not provided)
75 )
76 %mp_abort(iftrue=("&outds"="")
77 ,mac=&sysmacroname
78 ,msg=%str(Output dataset not provided)
79 )
80
81 /* avoid sending bom marker to API */
82 %local optval;
83 %let optval=%sysfunc(getoption(bomfile));
84 options nobomfile;
85
86 /* Add params to the content */
87 data _null_;
88 file &mainref termstr=crlf lrecl=32767 mod;
89 length line $1000 name $32 value $32767;
90 if _n_=1 then call missing(of _all_);
91 set &inputparams;
92 put "--&boundary";
93 line=cats('Content-Disposition: form-data; name="',name,'"');
94 put line;
95 put ;
96 put value;
97 run;
98
99 /* parse input file list */
100 %local webcount;
101 %let webcount=0;
102 data _null_;
103 set &inputfiles end=last;
104 length fileref $8 name $32 filename $256;
105 call symputx(cats('webref',_n_),fileref,'l');
106 call symputx(cats('webname',_n_),name,'l');
107 call symputx(cats('webfilename',_n_),filename,'l');
108 if last then do;
109 call symputx('webcount',_n_);
110 call missing(of _all_);
111 end;
112 run;
113
114 /* write out the input files to the content */
115 %local i;
116 %do i=1 %to &webcount;
117 data _null_;
118 file &mainref termstr=crlf lrecl=32767 mod;
119 infile &&webref&i lrecl=32767;
120 if _n_ = 1 then do;
121 length line $32767;
122 line=cats(
123 'Content-Disposition: form-data; name="'
124 ,"&&webname&i"
125 ,'"; filename="'
126 ,"&&webfilename&i"
127 ,'"'
128 );
129 put "--&boundary";
130 put line;
131 put "Content-Type: text/plain";
132 put ;
133 end;
134 input;
135 put _infile_; /* add the actual file to be sent */
136 run;
137 %end;
138
139 /* Add footer to the content */
140 data _null_;
141 file &mainref termstr=crlf mod;
142 put / "--&boundary--";
143 run;
144
145 data _null_;
146 file &authref lrecl=1000;
147 infile "&_sasjs_tokenfile" lrecl=1000;
148 input;
149 if _n_=1 then put "Content-Type: multipart/form-data; boundary=&boundary";
150 put _infile_;
151 run;
152
153 %if &mdebug=1 %then %do;
154 data _null_;
155 if _n_ eq 1 then putlog "NOTE: ***** authref=&authref content *****";
156 infile &authref;
157 input;
158 put _infile_;
159 data _null_;
160 if _n_ eq 1 then putlog "NOTE: ***** mainref=&mainref content *****";
161 infile &mainref;
162 input;
163 put _infile_;
164 run;
165 %end;
166
167 %local resp_path outref;
168 %let resp_path=%sysfunc(pathname(work))/%mf_getuniquename();
169 %let outref=%mf_getuniquefileref();
170 filename &outref "&resp_path" lrecl=32767;
171
172 /* prepare request*/
173 proc http method='POST' headerin=&authref in=&mainref out=&outref
174 url="&_sasjs_apiserverurl/SASjsApi/stp/trigger?%trim(
175 )_program=&pgm%str(&)_debug=&debug%str(&)expiresAfterMins=&expiresaftermins";
176 %if &mdebug=1 %then %do;
177 debug level=2;
178 %end;
179 run;
180
181 %if (&SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201)
182 or &mdebug=1
183 %then %do;
184 data _null_;
185 if _n_ eq 1 then putlog "NOTE: ***** outref=&outref content *****";
186 infile &outref;
187 input;
188 putlog _infile_;
189 run;
190 %end;
191 %mp_abort(
192 iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200
193 and &SYS_PROCHTTP_STATUS_CODE ne 201)
194 ,mac=&sysmacroname
195 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
196 )
197
198 /* reset options */
199 options &optval;
200
201 %let libref=%mf_getuniquelibref();
202 libname &libref JSON fileref=&outref;
203 %let triggered_sid=%mf_getuniquename(prefix=triggered_sid_);
204
205 data work.&triggered_sid (keep=sessionid);
206 set &libref..root;
207
208 %if &mdebug=1 %then %do;
209 putlog (_all_)(=);
210 %end;
211 run;
212
213 %if %mf_nobs(work.&triggered_sid)>0 %then %do;
214 proc append base=&outds data=work.&triggered_sid;
215 run;
216 %end;
217
218 %if &mdebug=1 %then %do;
219 %put &sysmacroname exit vars:;
220 %put _local_;
221 %end;
222 %else %do;
223 /* clear refs */
224 filename &authref;
225 filename &mainref;
226 filename &outref;
227 libname &libref clear;
228 /* and remove temp dataset */
229 %mp_dropmembers(&triggered_sid,libref=work);
230 %end;
231
232%mend ms_triggerstp;