@@ -115,6 +115,61 @@ dbDisconnect(file_conn)
115
115
dbDisconnect(source_conn)
116
116
```
117
117
118
+ ### Scraping one user's API tokens
119
+
120
+ Scraping one user's API tokens and putting them in a new, local sqlite DB for use.
121
+
122
+ ``` {r, eval = FALSE}
123
+ library(redcapcustodian)
124
+ library(DBI)
125
+ library(tidyverse)
126
+ library(dotenv)
127
+
128
+ # get the username provided on the command line
129
+ args <- commandArgs(trailingOnly = TRUE)
130
+ username <- args
131
+
132
+ # Creates the credentials file if one does not exists.
133
+ # Otherwise it deletes the credentials file
134
+ credentials_db_path <- here::here(paste0("credentials-", username, ".db"))
135
+ if (fs::file_exists(credentials_db_path)) fs::file_delete(credentials_db_path)
136
+ file_conn <- DBI::dbConnect(RSQLite::SQLite(), credentials_db_path)
137
+
138
+ # SQLite friendly schema
139
+ credentials_sql <- "CREATE TABLE IF NOT EXISTS `credentials` (
140
+ `redcap_uri` TEXT NOT NULL,
141
+ `server_short_name` varchar(128) NOT NULL,
142
+ `username` varchar(191) NOT NULL,
143
+ `project_id` int(10) NOT NULL,
144
+ `project_display_name` TEXT NOT NULL,
145
+ `project_short_name` varchar(128) DEFAULT NULL,
146
+ `token` varchar(64) NOT NULL,
147
+ `comment` varchar(256) DEFAULT NULL
148
+ );
149
+ "
150
+
151
+ dbExecute(file_conn, credentials_sql)
152
+
153
+ # Connect to the REDCap database specified in the environment
154
+ # loaded by the dotenv library
155
+ source_conn <- connect_to_redcap_db()
156
+ source_credentials <- scrape_user_api_tokens(
157
+ conn = source_conn,
158
+ username_to_scrape = username
159
+ )
160
+
161
+ # alter credentials to match local schema
162
+ source_credentials_upload <- source_credentials %>%
163
+ mutate(
164
+ redcap_uri = Sys.getenv("URI"),
165
+ server_short_name = tolower(Sys.getenv("INSTANCE"))
166
+ )
167
+
168
+ dbAppendTable(file_conn, "credentials", source_credentials_upload)
169
+
170
+ dbDisconnect(file_conn)
171
+ dbDisconnect(source_conn)
172
+ ```
118
173
### Creating API tokens for all local projects
119
174
120
175
``` {r, eval = FALSE}
0 commit comments