Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mv_getviyafileextparms.sas
Go to the documentation of this file.
1/**
2 @file mv_getviyafileextparms.sas
3 @brief Reads the VIYA file-extension type definition and returns selected
4 values in SAS macro variables
5
6 @details Content is derived from the following endpoint:
7 "https://${serverUrl}/types/types?limit=999999"
8
9 @param [in] ext File extension to retrieve property info for.
10 @param [out] propertiesVar= SAS macro variable name that will contain
11 the 'properties' object json, if found, else blank.
12 @param [out] typeDefNameVar= SAS macro variable name that will contain
13 the 'typeDefName' property value, if found, else blank.
14 @param [out] mediaTypeVar= SAS macro variable name that will contain
15 the 'mediaType' property value, if found, else blank.
16 @param [out] viyaFileExtRespLibDs (work.mv_getViyaFileExtParmsResponse)
17 Library.name of the dataset to receive the local working copy of the initial
18 response that requests all file extension details. Created once per session
19 to avoid multiple api calls.
20 @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
21
22 <h4> SAS Macros </h4>
23 @li mf_existds.sas
24 @li mf_getplatform.sas
25 @li mf_getuniquefileref.sas
26 @li mf_getuniquelibref.sas
27 @li mf_getuniquename.sas
28 @li mf_getvalue.sas
29 @li mf_getvarlist.sas
30 @li mf_getvartype.sas
31 @li mf_isblank.sas
32 @li mf_nobs.sas
33 @li mp_abort.sas
34
35**/
36
37%macro mv_getViyaFileExtParms(
38 ext,
39 typeDefNameVar=,
40 propertiesVar=,
41 mediaTypeVar=,
42 viyaFileExtRespLibDs=work.mv_getViyaFileExtParmsResponse,
43 mdebug=0
44 );
45 %local base_uri; /* location of rest apis */
46 %local url; /* File extension info end-point */
47
48 %mp_abort(
49 iftrue=(%mf_isBlank(&ext))
50 ,msg=%str(No file extension provided.)
51 ,mac=MV_GETVIYAFILEEXTPARMS
52 )
53
54 %mp_abort(
55 iftrue=(%mf_isBlank(&typeDefNameVar) and
56 %mf_isBlank(&propertiesVar) and
57 %mf_isBlank(&mediaTypeVar))
58 ,msg=%str(MV_GETVIYAFILEEXTPARMS - No parameter was requested.)
59 ,mac=MV_GETVIYAFILEEXTPARMS
60 )
61
62 %mp_abort(
63 iftrue=(%mf_isBlank(&viyaFileExtRespLibDs))
64 ,msg=%str(No <libname.>dataset name provided to cache inital response.)
65 ,mac=MV_GETVIYAFILEEXTPARMS
66 )
67
68 /* Declare requested parameters as global macro vars and initialize blank */
69 %if not %mf_isBlank(&typeDefNameVar) %then %do;
70 %global &typeDefNameVar;
71 %let &typeDefNameVar = %str();
72 %end;
73 %if not %mf_isBlank(&propertiesVar) %then %do;
74 %global &propertiesVar;
75 %let &propertiesVar = %str();
76 %end;
77 %if not %mf_isBlank(&mediaTypeVar) %then %do;
78 %global &mediaTypeVar;
79 %let &mediaTypeVar = %str();
80 %end;
81
82 %let base_uri=%mf_getplatform(VIYARESTAPI);
83 %if &mdebug=1 %then %put DEBUG: &=base_uri;
84
85 %let ext=%lowcase(&ext);
86
87 /* Create a local copy of the Viya response containing all file type info, if
88 it does not already exist. */
89 %if not %mf_existds(&viyaFileExtRespLibDs) %then %do;
90 /* Create a temp file and fill with JSON that declares */
91 /* VIYA file-type details for the given file extension */
92 %local viyatypedefs;
93 %let viyatypedefs=%mf_getuniquefileref();
94 filename &viyatypedefs temp;
95
96 %let url = &base_uri/types/types?limit=999999;
97
98 proc http oauth_bearer=sas_services out=&viyatypedefs
99 url="&url";
100 run;
101
102 %if &mdebug=1 %then %put DEBUG: &sysmacroname &=url
103 &=SYS_PROCHTTP_STATUS_CODE &=SYS_PROCHTTP_STATUS_PHRASE;
104
105 %if (&SYS_PROCHTTP_STATUS_CODE ne 200) %then %do;
106 /* To avoid a breaking change, exit early if the request failed.
107 The calling process will proceed with empty macro variables. */
108 %put INFO: &sysmacroname File extension details were not retrieved.;
109 filename &viyatypedefs clear;
110 %return;
111 %end;
112
113 %if &mdebug=1 %then %do;
114 /* Dump the response to the log */
115 data _null_;
116 length line $120;
117 null=byte(0);
118 infile &viyatypedefs dlm=null lrecl=120 recfm=n;
119 input line $120.;
120 if _n_ = 1 then put "DEBUG:";
121 put line;
122 run;
123 %end;
124
125 /* Convert the content of that JSON into SAS datasets */
126 /* First prepare a new WORK-based folder to receive the datasets */
127 %local jsonworkfolder jsonlib opt_dlcreatedir;
128 %let jsonworkfolder=%sysfunc(pathname(work))/%mf_getuniquename(prefix=jsn_);
129 %let jsonlib=%mf_getuniquelibref(prefix=json);
130 /* And point a libname at it */
131 %let opt_dlcreatedir = %sysfunc(getoption(dlcreatedir));
132 options dlcreatedir; libname &jsonlib "&jsonworkfolder";
133 options &opt_dlcreatedir;
134
135 /* Read the json output once and copy datasets to its work folder */
136 %local libref1;
137 %let libref1=%mf_getuniquelibref();
138 libname &libref1 JSON fileref=&viyatypedefs automap=create;
139 proc copy in=&libref1 out=&jsonlib; run;
140
141 libname &libref1 clear;
142
143 /* Now give all rows belonging to the same items array a grouping value */
144 data &viyaFileExtRespLibDs;
145 length _viyaItemIdx 8;
146 set &jsonlib..alldata;
147 retain _viyaItemIdx 0;
148 /* Increment the row group index when a new 'items' group is observed */
149 if P=1 and P1='items' then _viyaItemIdx + 1;
150 run;
151
152 %if &mdebug=0 %then %do;
153 /* Tidy up, unless debug=1 */
154 proc datasets library=&jsonlib nolist kill; quit;
155 libname &jsonlib clear;
156 %end;
157
158 filename &viyatypedefs clear;
159
160 %end; /* If initial filetype query response didn't exist */
161
162 %if &mdebug %then %put DEBUG: Find the row-group for extension &ext;
163 %local itemRowGroup;
164 data _null_;
165 set &viyaFileExtRespLibDs;
166 where p1='items' and p2='extensions' and value="&ext";
167 call symputx('itemRowGroup',_viyaItemIdx,'l');
168 %if &mdebug %then %do;
169 putlog (_all_)(=);
170 %end;
171 run;
172
173 %if &mdebug %then %put DEBUG: &=itemRowGroup;
174
175 %if %mf_isBlank(&itemRowGroup) %then %do;
176 /* extension was not found */
177 %if &mdebug %then %put DEBUG: No type details found for extension "&ext";
178 %return;
179 %end;
180
181 /* Filter the cached response data down to the required file extension */
182 %local dsItems;
183 %let dsItems = %mf_getuniquename(prefix=dsItems_);
184 data work.&dsItems;
185 set &viyaFileExtRespLibDs;
186 where _viyaItemIdx = &itemRowGroup;
187 run;
188
189 /* Populate typeDefName, if requested */
190 %if (not %mf_isBlank(&typeDefNameVar)) %then %do;
191 %let &typeDefNameVar = %mf_getvalue(
192 &dsItems,value,filter=%quote(p1="items" and p2="name"));
193 %if &mdebug %then
194 %put DEBUG: &=typeDefNameVar &typeDefNameVar=&&&typeDefNameVar;
195 %end;
196
197 /* Populate mediaType, if requested */
198 %if (not %mf_isBlank(&mediaTypeVar)) %then %do;
199 %let &mediaTypeVar = %mf_getvalue(
200 &dsItems,value,filter=%quote(p1="items" and p2="mediaType"));
201 %if &mdebug %then %put DEBUG: &=mediaTypeVar &mediaTypeVar=&&&mediaTypeVar;
202 %end;
203
204 /* Populate properties macro variable, if requested */
205 %if not %mf_isBlank(&propertiesVar) %then %do;
206
207 /* Filter dsItems down to the properties */
208 %local dsProperties;
209 %let dsProperties = %mf_getuniquename(prefix=dsProperties_);
210 data work.&dsProperties ( rename=(p3 = propertyName) );
211 set work.&dsItems;
212 where p2="properties" and v=1;
213 run;
214
215 /* Check for 1+ properties */
216 %if ( %mf_nobs(&dsProperties) = 0 ) %then %do;
217 %let &propertiesVar = %str();
218 %if &mdebug %then %put DEBUG: &SYSMACRONAME - No Viya properties %trim(
219 )found for file suffix %str(%')&ext%str(%');
220 %end;
221 %else %do;
222 /* Properties potentially span multiple rows in the input table */
223 data _null_;
224 length
225 line $32767
226 properties $32767
227 ;
228 retain properties;
229 set &dsProperties end=last;
230 if _n_ = 1 then properties = '{';
231
232 line = cats(quote(trim(propertyName)),':');
233 /* Only strings and bools appear in properties */
234 if value not in ("true","false") then value = quote(trim(value));
235 line = catx(' ',line,value);
236 /* Add a comma separator to all except the last line */
237 if not last then line = cats(line,',');
238
239 /* Add this line to the output value */
240 properties = catx(' ',properties,line);
241
242 if last then do;
243 /* Close off the properties object and output to the macro variable */
244 properties=catx(' ',properties,'}');
245 call symputx("&propertiesVar",properties);
246 end;
247 run;
248
249 %if &mdebug %then
250 %put DEBUG: &=propertiesVar &propertiesVar=&&&propertiesVar;
251 %end;
252
253 %end;
254
255%mend mv_getViyaFileExtParms;