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**/
49
50%macro ms_triggerstp(pgm
51 ,debug=131
52 ,inputparams=_null_
53 ,inputfiles=_null_
54 ,expiresAfterMins=15
55 ,outds=work.ms_triggerstp
56 ,mdebug=0
57 );
58 %local dbg mainref authref boundary libref triggered_sid;
59 %let mainref=%mf_getuniquefileref();
60 %let authref=%mf_getuniquefileref();
61 %let boundary=%mf_getuniquename();
62 %if &inputparams=0 %then %let inputparams=_null_;
63
64 %if &mdebug=1 %then %do;
65 %put &sysmacroname entry vars:;
66 %put _local_;
67 %end;
68 %else %let dbg=*;
69
70
71 %mp_abort(iftrue=("&pgm"="")
72 ,mac=&sysmacroname
73 ,msg=%str(Program not provided)
74 )
75 %mp_abort(iftrue=("&outds"="")
76 ,mac=&sysmacroname
77 ,msg=%str(Output dataset not provided)
78 )
79
80 /* avoid sending bom marker to API */
81 %local optval;
82 %let optval=%sysfunc(getoption(bomfile));
83 options nobomfile;
84
85 /* Add params to the content */
86 data _null_;
87 file &mainref termstr=crlf lrecl=32767 mod;
88 length line $1000 name $32 value $32767;
89 if _n_=1 then call missing(of _all_);
90 set &inputparams;
91 put "--&boundary";
92 line=cats('Content-Disposition: form-data; name="',name,'"');
93 put line;
94 put ;
95 put value;
96 run;
97
98 /* parse input file list */
99 %local webcount;
100 %let webcount=0;
101 data _null_;
102 set &inputfiles end=last;
103 length fileref $8 name $32 filename $256;
104 call symputx(cats('webref',_n_),fileref,'l');
105 call symputx(cats('webname',_n_),name,'l');
106 call symputx(cats('webfilename',_n_),filename,'l');
107 if last then do;
108 call symputx('webcount',_n_);
109 call missing(of _all_);
110 end;
111 run;
112
113 /* write out the input files to the content */
114 %local i;
115 %do i=1 %to &webcount;
116 data _null_;
117 file &mainref termstr=crlf lrecl=32767 mod;
118 infile &&webref&i lrecl=32767;
119 if _n_ = 1 then do;
120 length line $32767;
121 line=cats(
122 'Content-Disposition: form-data; name="'
123 ,"&&webname&i"
124 ,'"; filename="'
125 ,"&&webfilename&i"
126 ,'"'
127 );
128 put "--&boundary";
129 put line;
130 put "Content-Type: text/plain";
131 put ;
132 end;
133 input;
134 put _infile_; /* add the actual file to be sent */
135 run;
136 %end;
137
138 /* Add footer to the content */
139 data _null_;
140 file &mainref termstr=crlf mod;
141 put / "--&boundary--";
142 run;
143
144 data _null_;
145 file &authref lrecl=1000;
146 infile "&_sasjs_tokenfile" lrecl=1000;
147 input;
148 if _n_=1 then put "Content-Type: multipart/form-data; boundary=&boundary";
149 put _infile_;
150 run;
151
152 %if &mdebug=1 %then %do;
153 data _null_;
154 if _n_ eq 1 then putlog "NOTE: ***** authref=&authref content *****";
155 infile &authref;
156 input;
157 put _infile_;
158 data _null_;
159 if _n_ eq 1 then putlog "NOTE: ***** mainref=&mainref content *****";
160 infile &mainref;
161 input;
162 put _infile_;
163 run;
164 %end;
165
166 %local resp_path outref;
167 %let resp_path=%sysfunc(pathname(work))/%mf_getuniquename();
168 %let outref=%mf_getuniquefileref();
169 filename &outref "&resp_path" lrecl=32767;
170
171 /* prepare request*/
172 proc http method='POST' headerin=&authref in=&mainref out=&outref
173 url="&_sasjs_apiserverurl/SASjsApi/stp/trigger?%trim(
174 )_program=&pgm%str(&)_debug=131%str(&)expiresAfterMins=&expiresaftermins";
175 %if &mdebug=1 %then %do;
176 debug level=2;
177 %end;
178 run;
179
180 %if (&SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201)
181 or &mdebug=1
182 %then %do;
183 data _null_;
184 if _n_ eq 1 then putlog "NOTE: ***** outref=&outref content *****";
185 infile &outref;
186 input;
187 putlog _infile_;
188 run;
189 %end;
190 %mp_abort(
191 iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200
192 and &SYS_PROCHTTP_STATUS_CODE ne 201)
193 ,mac=&sysmacroname
194 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
195 )
196
197 /* reset options */
198 options &optval;
199
200 %let libref=%mf_getuniquelibref();
201 libname &libref JSON fileref=&outref;
202 %let triggered_sid=%mf_getuniquename(prefix=triggered_sid_);
203
204 data work.&triggered_sid (keep=sessionid);
205 set &libref..root;
206
207 %if &mdebug=1 %then %do;
208 putlog (_all_)(=);
209 %end;
210 run;
211
212 %if %mf_nobs(work.&triggered_sid)>0 %then %do;
213 proc append base=&outds data=work.&triggered_sid;
214 run;
215 %end;
216
217 %if &mdebug=1 %then %do;
218 %put &sysmacroname exit vars:;
219 %put _local_;
220 %end;
221 %else %do;
222 /* clear refs */
223 filename &authref;
224 filename &mainref;
225 filename &outref;
226 libname &libref clear;
227 /* and remove temp dataset */
228 %mp_dropmembers(&triggered_sid,libref=work);
229 %end;
230
231%mend ms_triggerstp;