Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mv_getfoldermembers.sas
Go to the documentation of this file.
1 /**
2  @file mv_getfoldermembers.sas
3  @brief Gets a list of folders (and ids) for a given root
4  @details Works for both root level and below, oauth or password. Default is
5  oauth, and the token is expected in a global ACCESS_TOKEN variable.
6 
7  %mv_getfoldermembers(root=/Public)
8 
9 
10  @param root= The path for which to return the list of folders
11  @param outds= The output dataset to create (default is work.mv_getfolders)
12  @param access_token_var= The global macro variable to contain the access token
13  @param grant_type= valid values are "password" or "authorization_code" (unquoted).
14  The default is authorization_code.
15 
16 
17  @version VIYA V.03.04
18  @author Allan Bowe
19  @source https://github.com/sasjs/core
20 
21  <h4> Dependencies </h4>
22  @li mp_abort.sas
23  @li mf_getplatform.sas
24  @li mf_getuniquefileref.sas
25  @li mf_getuniquelibref.sas
26  @li mf_isblank.sas
27 
28 **/
29 
30 %macro mv_getfoldermembers(root=/
31  ,access_token_var=ACCESS_TOKEN
32  ,grant_type=sas_services
33  ,outds=mv_getfolders
34  );
35 %local oauth_bearer;
36 %if &grant_type=detect %then %do;
37  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
38  %else %let grant_type=sas_services;
39 %end;
40 %if &grant_type=sas_services %then %do;
41  %let oauth_bearer=oauth_bearer=sas_services;
42  %let &access_token_var=;
43 %end;
44 %put &sysmacroname: grant_type=&grant_type;
45 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
46  and &grant_type ne sas_services
47  )
48  ,mac=&sysmacroname
49  ,msg=%str(Invalid value for grant_type: &grant_type)
50 )
51 
52 %if %mf_isblank(&root)=1 %then %let root=/;
53 
54 options noquotelenmax;
55 
56 /* request the client details */
57 %local fname1 libref1;
58 %let fname1=%mf_getuniquefileref();
59 %let libref1=%mf_getuniquelibref();
60 
61 %local base_uri; /* location of rest apis */
62 %let base_uri=%mf_getplatform(VIYARESTAPI);
63 
64 %if "&root"="/" %then %do;
65  /* if root just list root folders */
66  proc http method='GET' out=&fname1 &oauth_bearer
67  url="&base_uri/folders/rootFolders";
68  %if &grant_type=authorization_code %then %do;
69  headers "Authorization"="Bearer &&&access_token_var";
70  %end;
71  run;
72  libname &libref1 JSON fileref=&fname1;
73  data &outds;
74  set &libref1..items;
75  run;
76 %end;
77 %else %do;
78  /* first get parent folder id */
79  proc http method='GET' out=&fname1 &oauth_bearer
80  url="&base_uri/folders/folders/@item?path=&root";
81  %if &grant_type=authorization_code %then %do;
82  headers "Authorization"="Bearer &&&access_token_var";
83  %end;
84  run;
85  /*data _null_;infile &fname1;input;putlog _infile_;run;*/
86  libname &libref1 JSON fileref=&fname1;
87  /* now get the followon link to list members */
88  data _null_;
89  set &libref1..links;
90  if rel='members' then call symputx('href',quote("&base_uri"!!trim(href)),'l');
91  run;
92  %local fname2 libref2;
93  %let fname2=%mf_getuniquefileref();
94  %let libref2=%mf_getuniquelibref();
95  proc http method='GET' out=&fname2 &oauth_bearer
96  url=%unquote(%superq(href));
97  %if &grant_type=authorization_code %then %do;
98  headers "Authorization"="Bearer &&&access_token_var";
99  %end;
100  run;
101  libname &libref2 JSON fileref=&fname2;
102  data &outds;
103  set &libref2..items;
104  run;
105  filename &fname2 clear;
106  libname &libref2 clear;
107 %end;
108 
109 
110 /* clear refs */
111 filename &fname1 clear;
112 libname &libref1 clear;
113 
114 %mend;