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