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