Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mv_getfoldermembers.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Gets a list of folder members (and ids) for a given root
4 @details Returns all members for a particular Viya folder. Works at both root
5 level and below, and results are created in an output dataset.
6
7 %mv_getfoldermembers(root=/Public, outds=work.mymembers)
8
9 A root with nothing to list - an empty folder, or an answer that carries no
10 members link - still yields &outds, with the documented columns and zero rows.
11 A non-200 HTTP response aborts with the HTTP status rather than being
12 interpreted as an empty listing.
13
14
15 @param [in] root= (/) The path for which to return the list of folders
16 @param [out] outds= (work.mv_getfolders) The output dataset to create. Format:
17 |ordinal_root|ordinal_items|creationTimeStamp| modifiedTimeStamp|createdBy|modifiedBy|id| uri|added| type|name|description|
18 |---|---|---|---|---|---|---|---|---|---|---|---|
19 |1|1|2021-05-25T11:15:04.204Z|2021-05-25T11:15:04.204Z|allbow|allbow|4f1e3945-9655-462b-90f2-c31534b3ca47|/folders/folders/ed701ff3-77e8-468d-a4f5-8c43dec0fd9e|2021-05-25T11:15:04.212Z|child|my_folder_name|My folder Description|
20
21 @param [in] access_token_var= (ACCESS_TOKEN) The global macro variable to
22 contain the access token
23 @param [in] grant_type= (sas_services) Valid values are:
24 @li password
25 @li authorization_code
26 @li detect
27 @li sas_services
28
29 @version VIYA V.03.04
30 @author Allan Bowe, source: https://github.com/sasjs/core
31
32 <h4> SAS Macros </h4>
33 @li mp_abort.sas
34 @li mf_getplatform.sas
35 @li mf_getuniquefileref.sas
36 @li mf_getuniquelibref.sas
37 @li mf_isblank.sas
38
39 <h4> Related Macros </h4>
40 @li mv_createfolder.sas
41 @li mv_deletefoldermember.sas
42 @li mv_deleteviyafolder.sas
43 @li mv_getfoldermembers.test.sas
44
45 @sasjslint {"maxLineLength": 230}
46**/
47
48%macro mv_getfoldermembers(root=/
49 ,access_token_var=ACCESS_TOKEN
50 ,grant_type=sas_services
51 ,outds=mv_getfolders
52 );
53%local oauth_bearer;
54%if &grant_type=detect %then %do;
55 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
56 %else %let grant_type=sas_services;
57%end;
58%if &grant_type=sas_services %then %do;
59 %let oauth_bearer=oauth_bearer=sas_services;
60 %let &access_token_var=;
61%end;
62
63%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
64 and &grant_type ne sas_services
65 )
66 ,mac=&sysmacroname
67 ,msg=%str(Invalid value for grant_type: &grant_type)
68)
69
70%if %mf_isblank(&root)=1 %then %let root=/;
71
72options noquotelenmax;
73
74/* request the client details */
75%local fname1 libref1;
76%let fname1=%mf_getuniquefileref();
77%let libref1=%mf_getuniquelibref();
78
79%local base_uri; /* location of rest apis */
80%let base_uri=%mf_getplatform(VIYARESTAPI);
81
82%if "&root"="/" %then %do;
83 /* if root just list root folders */
84 proc http method='GET' out=&fname1 &oauth_bearer
85 url="&base_uri/folders/rootFolders?limit=1000";
86 %if &grant_type=authorization_code %then %do;
87 headers "Authorization"="Bearer &&&access_token_var";
88 %end;
89 run;
90 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
91 ,mac=&sysmacroname
92 ,msg=%str(Root folders: &SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
93 )
94 libname &libref1 JSON fileref=&fname1;
95 /* an empty collection produces no items dataset at all, so a straight SET
96 would fail with "File ...ITEMS.DATA does not exist" and leave syscc dirty */
97 %if %sysfunc(exist(&libref1..items))=1 %then %do;
98 data &outds;
99 /* the documented columns are declared on every path, so a caller reads
100 the same columns whether the collection is empty or full - an empty
101 collection's items dataset carries a narrower shape (no createdBy,
102 no timestamps) than a populated one. The declared lengths are at
103 least the engine's own, which are sized to the payload */
104 length ordinal_root 8 ordinal_items 8
105 creationTimeStamp $32 modifiedTimeStamp $32
106 createdBy $64 modifiedBy $64
107 id $36 uri $512 added $32 type $32
108 name $256 description $256;
109 /* initialise the declared columns - an empty collection's items
110 dataset carries none of the member columns, and referencing one that
111 the input does not supply raises "Variable x is uninitialized" */
112 call missing(of _all_);
113 set &libref1..items;
114 /* the engine emits one all-missing row for an empty collection */
115 if missing(name) and missing(uri) then delete;
116 run;
117 %end;
118 %else %do;
119 %put NOTE: &sysmacroname - no root folders found;
120 data &outds;
121 length ordinal_root 8 ordinal_items 8
122 creationTimeStamp $32 modifiedTimeStamp $32
123 createdBy $64 modifiedBy $64
124 id $36 uri $512 added $32 type $32
125 name $256 description $256;
126 call missing(of _all_);
127 stop;
128 run;
129 %end;
130%end;
131%else %do;
132 /* first get parent folder id */
133 proc http method='GET' out=&fname1 &oauth_bearer
134 url="&base_uri/folders/folders/@item?path=&root";
135 %if &grant_type=authorization_code %then %do;
136 headers "Authorization"="Bearer &&&access_token_var";
137 %end;
138 run;
139 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
140 ,mac=&sysmacroname
141 ,msg=%str(Folder lookup: &SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
142 )
143 /*data _null_;infile &fname1;input;putlog _infile_;run;*/
144 libname &libref1 JSON fileref=&fname1;
145 /* now get the followon link to list members */
146 %local href cnt;
147 %let cnt=0;
148 /* a root that is not a folder (a member path, for instance) answers
149 without a links dataset, so a straight SET would leave syscc dirty */
150 %if %sysfunc(exist(&libref1..links))=1 %then %do;
151 data _null_;
152 length rel href $512;
153 call missing(rel,href);
154 set &libref1..links;
155 if rel='members' then do;
156 url=cats("'","&base_uri",href,"?limit=10000'");
157 call symputx('href',url,'l');
158 call symputx('cnt',1,'l');
159 end;
160 run;
161 %end;
162 %if &cnt=0 %then %do;
163 %put NOTE:;%put NOTE- No members found in &root!!;%put NOTE-;
164 /* the folder answered without a members link, so there is nothing to list -
165 the output dataset is still created, with the documented columns */
166 data &outds;
167 length ordinal_root 8 ordinal_items 8
168 creationTimeStamp $32 modifiedTimeStamp $32
169 createdBy $64 modifiedBy $64
170 id $36 uri $512 added $32 type $32
171 name $256 description $256;
172 call missing(of _all_);
173 stop;
174 run;
175 filename &fname1 clear;
176 libname &libref1 clear;
177 %return;
178 %end;
179 %local fname2 libref2;
180 %let fname2=%mf_getuniquefileref();
181 %let libref2=%mf_getuniquelibref();
182 proc http method='GET' out=&fname2 &oauth_bearer
183 url=%unquote(%superq(href));
184 %if &grant_type=authorization_code %then %do;
185 headers "Authorization"="Bearer &&&access_token_var";
186 %end;
187 run;
188 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
189 ,mac=&sysmacroname
190 ,msg=%str(Folder members: &SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
191 )
192 libname &libref2 JSON fileref=&fname2;
193 /* an emptied folder produces no items dataset at all, so a straight SET would
194 fail with "File ...ITEMS.DATA does not exist" and leave syscc dirty */
195 %if %sysfunc(exist(&libref2..items))=1 %then %do;
196 data &outds;
197 length ordinal_root 8 ordinal_items 8
198 creationTimeStamp $32 modifiedTimeStamp $32
199 createdBy $64 modifiedBy $64
200 id $36 uri $512 added $32 type $32
201 name $256 description $256;
202 /* initialise the declared columns - an empty collection's items
203 dataset carries none of the member columns, and referencing one that
204 the input does not supply raises "Variable x is uninitialized" */
205 call missing(of _all_);
206 set &libref2..items;
207 /* the engine emits one all-missing row for an empty collection */
208 if missing(name) and missing(uri) then delete;
209 run;
210 %end;
211 %else %do;
212 %put NOTE: &sysmacroname - &root is empty;
213 data &outds;
214 length ordinal_root 8 ordinal_items 8
215 creationTimeStamp $32 modifiedTimeStamp $32
216 createdBy $64 modifiedBy $64
217 id $36 uri $512 added $32 type $32
218 name $256 description $256;
219 call missing(of _all_);
220 stop;
221 run;
222 %end;
223 filename &fname2 clear;
224 libname &libref2 clear;
225%end;
226
227
228/* clear refs */
229filename &fname1 clear;
230libname &libref1 clear;
231
232%mend mv_getfoldermembers;