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