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/code/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
30 @param [out] outds= (work.ms_triggerstp) Set to the name of a dataset to
31 contain the sessionId. If this dataset already exists, and contains the
32 sessionId, it will be appended to.
33 Format:
34 |sessionId:$36|
35 |---|
36 |20241028074744-54132-1730101664824|
37
38 <h4> SAS Macros </h4>
39 @li mf_getuniquefileref.sas
40 @li mf_getuniquename.sas
41 @li mp_abort.sas
42
43**/
44
45%macro ms_triggerstp(pgm
46 ,debug=131
47 ,inputparams=_null_
48 ,inputfiles=_null_
49 ,outds=work.ms_triggerstp
50 ,mdebug=0
51 );
52%local dbg mainref authref;
53%let mainref=%mf_getuniquefileref();
54%let authref=%mf_getuniquefileref();
55%if &inputparams=0 %then %let inputparams=_null_;
56
57%if &mdebug=1 %then %do;
58 %put &sysmacroname entry vars:;
59 %put _local_;
60%end;
61%else %let dbg=*;
62
63
64%mp_abort(iftrue=("&pgm"="")
65 ,mac=&sysmacroname
66 ,msg=%str(Program not provided)
67)
68
69/* avoid sending bom marker to API */
70%local optval;
71%let optval=%sysfunc(getoption(bomfile));
72options nobomfile;
73
74/* add params */
75data _null_;
76 file &mainref termstr=crlf lrecl=32767 mod;
77 length line $1000 name $32 value $32767;
78 if _n_=1 then call missing(of _all_);
79 set &inputparams;
80 put "--&boundary";
81 line=cats('Content-Disposition: form-data; name="',name,'"');
82 put line;
83 put ;
84 put value;
85run;
86
87/* parse input file list */
88%local webcount;
89%let webcount=0;
90data _null_;
91 set &inputfiles end=last;
92 length fileref $8 name $32 filename $256;
93 call symputx(cats('webref',_n_),fileref,'l');
94 call symputx(cats('webname',_n_),name,'l');
95 call symputx(cats('webfilename',_n_),filename,'l');
96 if last then do;
97 call symputx('webcount',_n_);
98 call missing(of _all_);
99 end;
100run;
101
102/* write out the input files */
103%local i;
104%do i=1 %to &webcount;
105 data _null_;
106 file &mainref termstr=crlf lrecl=32767 mod;
107 infile &&webref&i lrecl=32767;
108 if _n_ = 1 then do;
109 length line $32767;
110 line=cats(
111 'Content-Disposition: form-data; name="'
112 ,"&&webname&i"
113 ,'"; filename="'
114 ,"&&webfilename&i"
115 ,'"'
116 );
117 put "--&boundary";
118 put line;
119 put "Content-Type: text/plain";
120 put ;
121 end;
122 input;
123 put _infile_; /* add the actual file to be sent */
124 run;
125%end;
126
127data _null_;
128 file &mainref termstr=crlf mod;
129 put "--&boundary--";
130run;
131
132data _null_;
133 file &authref lrecl=1000;
134 infile "&_sasjs_tokenfile" lrecl=1000;
135 input;
136 if _n_=1 then put "Content-Type: multipart/form-data; boundary=&boundary";
137 put _infile_;
138run;
139
140%if &mdebug=1 %then %do;
141 data _null_;
142 infile &authref;
143 input;
144 put _infile_;
145 data _null_;
146 infile &mainref;
147 input;
148 put _infile_;
149 run;
150%end;
151
152%local resp_path;
153%let resp_path=%sysfunc(pathname(work))/%mf_getuniquename();
154filename &outref "&resp_path" lrecl=32767;
155
156/* prepare request*/
157proc http method='POST' headerin=&authref in=&mainref out=&outref
158 url="&_sasjs_apiserverurl/SASjsApi/stp/trigger?%trim(
159 )_program=&pgm%str(&)_debug=131";
160%if &mdebug=1 %then %do;
161 debug level=2;
162%end;
163run;
164
165%if (&SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201)
166or &mdebug=1
167%then %do;
168 data _null_;infile &outref;input;putlog _infile_;run;
169%end;
170%mp_abort(
171 iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201)
172 ,mac=&sysmacroname
173 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
174)
175
176/* reset options */
177options &optval;
178
179data work.%mf_getuniquename();
180 infile "&resp_path";
181 input
182
183%if &outlogds ne _null_ or &mdebug=1 %then %do;
184 %local matchstr chopout;
185 %let matchstr=SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784;
186 %let chopout=%sysfunc(pathname(work))/%mf_getuniquename(prefix=chop);
187
188 %mp_chop("&resp_path"
189 ,matchvar=matchstr
190 ,keep=LAST
191 ,matchpoint=END
192 ,outfile="&chopout"
193 ,mdebug=&mdebug
194 )
195
196 data &outlogds;
197 infile "&chopout" lrecl=2000;
198 length line $2000;
199 line=_infile_;
200 %if &mdebug=1 %then %do;
201 putlog line=;
202 %end;
203 run;
204%end;
205
206%if &mdebug=1 %then %do;
207 %put &sysmacroname exit vars:;
208 %put _local_;
209%end;
210%else %do;
211 /* clear refs */
212 filename &authref;
213 filename &mainref;
214%end;
215%mend ms_triggerstp;