Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mv_createfolder.sas
Go to the documentation of this file.
1 /**
2  @file mv_createfolder.sas
3  @brief Creates a viya folder if that folder does not already exist
4  @details Expects oauth token in a global macro variable (default
5  ACCESS_TOKEN).
6 
7  options mprint;
8  %mv_createfolder(path=/Public)
9 
10 
11  @param path= The full path of the folder to be created
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, source: https://github.com/sasjs/core
19 
20  <h4> SAS Macros </h4>
21  @li mp_abort.sas
22  @li mf_getuniquefileref.sas
23  @li mf_getuniquelibref.sas
24  @li mf_isblank.sas
25  @li mf_getplatform.sas
26 
27 **/
28 
29 %macro mv_createfolder(path=
30  ,access_token_var=ACCESS_TOKEN
31  ,grant_type=sas_services
32  );
33 %local oauth_bearer;
34 %if &grant_type=detect %then %do;
35  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
36  %else %let grant_type=sas_services;
37 %end;
38 %if &grant_type=sas_services %then %do;
39  %let oauth_bearer=oauth_bearer=sas_services;
40  %let &access_token_var=;
41 %end;
42 
43 %put &sysmacroname: grant_type=&grant_type;
44 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
45  and &grant_type ne sas_services
46  )
47  ,mac=&sysmacroname
48  ,msg=%str(Invalid value for grant_type: &grant_type)
49 )
50 
51 %mp_abort(iftrue=(%mf_isblank(&path)=1)
52  ,mac=&sysmacroname
53  ,msg=%str(path value must be provided)
54 )
55 %mp_abort(iftrue=(%length(&path)=1)
56  ,mac=&sysmacroname
57  ,msg=%str(path value must be provided)
58 )
59 
60 options noquotelenmax;
61 
62 %local subfolder_cnt; /* determine the number of subfolders */
63 %let subfolder_cnt=%sysfunc(countw(&path,/));
64 
65 %local href; /* resource address (none for root) */
66 %let href="/folders/folders?parentFolderUri=/folders/folders/none";
67 
68 %local base_uri; /* location of rest apis */
69 %let base_uri=%mf_getplatform(VIYARESTAPI);
70 
71 %local x newpath subfolder;
72 %do x=1 %to &subfolder_cnt;
73  %let subfolder=%scan(&path,&x,%str(/));
74  %let newpath=&newpath/&subfolder;
75 
76  %local fname1;
77  %let fname1=%mf_getuniquefileref();
78 
79  %put &sysmacroname checking to see if &newpath exists;
80  proc http method='GET' out=&fname1 &oauth_bearer
81  url="&base_uri/folders/folders/@item?path=&newpath";
82  %if &grant_type=authorization_code %then %do;
83  headers "Authorization"="Bearer &&&access_token_var";
84  %end;
85  run;
86  %local libref1;
87  %let libref1=%mf_getuniquelibref();
88  libname &libref1 JSON fileref=&fname1;
89  %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 404)
90  ,mac=&sysmacroname
91  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
92  )
93  %if &SYS_PROCHTTP_STATUS_CODE=200 %then %do;
94  %put &sysmacroname &newpath exists so grab the follow on link ;
95  data _null_;
96  set &libref1..links;
97  if rel='createChild' then
98  call symputx('href',quote("&base_uri"!!trim(href)),'l');
99  run;
100  %end;
101  %else %if &SYS_PROCHTTP_STATUS_CODE=404 %then %do;
102  %put &sysmacroname &newpath not found - creating it now;
103  %local fname2;
104  %let fname2=%mf_getuniquefileref();
105  data _null_;
106  length json $1000;
107  json=cats("'"
108  ,'{"name":'
109  ,quote(trim(symget('subfolder')))
110  ,',"description":'
111  ,quote("&subfolder, created by &sysmacroname")
112  ,',"type":"folder"}'
113  ,"'"
114  );
115  call symputx('json',json,'l');
116  run;
117 
118  proc http method='POST'
119  in=&json
120  out=&fname2
121  &oauth_bearer
122  url=%unquote(%superq(href));
123  headers
124  %if &grant_type=authorization_code %then %do;
125  "Authorization"="Bearer &&&access_token_var"
126  %end;
127  'Content-Type'='application/vnd.sas.content.folder+json'
128  'Accept'='application/vnd.sas.content.folder+json';
129  run;
130  %put &=SYS_PROCHTTP_STATUS_CODE;
131  %put &=SYS_PROCHTTP_STATUS_PHRASE;
132  %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 201)
133  ,mac=&sysmacroname
134  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
135  )
136  %local libref2;
137  %let libref2=%mf_getuniquelibref();
138  libname &libref2 JSON fileref=&fname2;
139  %put &sysmacroname &newpath now created. Grabbing the follow on link ;
140  data _null_;
141  set &libref2..links;
142  if rel='createChild' then
143  call symputx('href',quote(trim(href)),'l');
144  run;
145 
146  libname &libref2 clear;
147  filename &fname2 clear;
148  %end;
149  filename &fname1 clear;
150  libname &libref1 clear;
151 %end;
152 %mend;