Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mv_getusers.sas
Go to the documentation of this file.
1 /**
2  @file mv_getusers.sas
3  @brief Creates a dataset with a list of users
4  @details First, be sure you have an access token (which requires an app token).
5 
6  Using the macros here:
7 
8  filename mc url
9  "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
10  %inc mc;
11 
12  An administrator needs to set you up with an access code:
13 
14  %mv_registerclient(outds=client)
15 
16  Navigate to the url from the log (opting in to the groups) and paste the
17  access code below:
18 
19  %mv_tokenauth(inds=client,code=wKDZYTEPK6)
20 
21  Now we can run the macro!
22 
23  %mv_getusers(outds=users)
24 
25  Output (lengths are dynamic):
26 
27  ordinal_root num,
28  ordinal_items num,
29  version num,
30  id char(20),
31  name char(23),
32  providerId char(4),
33  type char(4),
34  creationTimeStamp char(24),
35  modifiedTimeStamp char(24),
36  state char(6)
37 
38  @param access_token_var= The global macro variable to contain the access token
39  @param grant_type= valid values:
40  * password
41  * authorization_code
42  * detect - will check if access_token exists, if not will use sas_services if
43  a SASStudioV session else authorization_code. Default option.
44  * sas_services - will use oauth_bearer=sas_services
45 
46  @param outds= The library.dataset to be created that contains the list of groups
47 
48 
49  @version VIYA V.03.04
50  @author Allan Bowe
51  @source https://github.com/sasjs/core
52 
53  <h4> Dependencies </h4>
54  @li mp_abort.sas
55  @li mf_getplatform.sas
56  @li mf_getuniquefileref.sas
57  @li mf_getuniquelibref.sas
58 
59 **/
60 
61 %macro mv_getusers(outds=work.mv_getusers
62  ,access_token_var=ACCESS_TOKEN
63  ,grant_type=sas_services
64  );
65 %local oauth_bearer;
66 %if &grant_type=detect %then %do;
67  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
68  %else %let grant_type=sas_services;
69 %end;
70 %if &grant_type=sas_services %then %do;
71  %let oauth_bearer=oauth_bearer=sas_services;
72  %let &access_token_var=;
73 %end;
74 %put &sysmacroname: grant_type=&grant_type;
75 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
76  and &grant_type ne sas_services
77  )
78  ,mac=&sysmacroname
79  ,msg=%str(Invalid value for grant_type: &grant_type)
80 )
81 
82 options noquotelenmax;
83 
84 %local base_uri; /* location of rest apis */
85 %let base_uri=%mf_getplatform(VIYARESTAPI);
86 
87 /* fetching folder details for provided path */
88 %local fname1;
89 %let fname1=%mf_getuniquefileref();
90 %let libref1=%mf_getuniquelibref();
91 
92 proc http method='GET' out=&fname1 &oauth_bearer
93  url="&base_uri/identities/users?limit=10000";
94 %if &grant_type=authorization_code %then %do;
95  headers "Authorization"="Bearer &&&access_token_var"
96  "Accept"="application/json";
97 %end;
98 %else %do;
99  headers "Accept"="application/json";
100 %end;
101 run;
102 /*data _null_;infile &fname1;input;putlog _infile_;run;*/
103 %mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
104  ,mac=&sysmacroname
105  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
106 )
107 libname &libref1 JSON fileref=&fname1;
108 
109 data &outds;
110  set &libref1..items;
111 run;
112 
113 /* clear refs */
114 filename &fname1 clear;
115 libname &libref1 clear;
116 
117 %mend;