Skip to content

Commit

Permalink
Merge pull request #154 from pbchase/add_to_credential_scraping
Browse files Browse the repository at this point in the history
Add to credential scraping
  • Loading branch information
ChemiKyle committed Mar 26, 2024
2 parents 2a0ea1c + d62bfc4 commit b753c0a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ Suggests:
tidyverse
VignetteBuilder: knitr
Config/testthat/edition: 3
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Depends:
R (>= 3.5.0)
16 changes: 8 additions & 8 deletions R/credential_management.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ scrape_user_api_tokens <- function(conn, username_to_scrape = Sys.info()[["user"
# collect super API token if one exists
super_credentials <- dplyr::tbl(conn, "redcap_user_information") %>%
dplyr::filter(.data$username == username_to_scrape) %>%
dplyr::select(.data$username, .data$api_token) %>%
dplyr::select("username", "api_token") %>%
dplyr::collect() %>%
dplyr::mutate(project_id = 0) %>%
dplyr::filter(!is.na(.data$api_token)) %>%
Expand All @@ -32,25 +32,25 @@ scrape_user_api_tokens <- function(conn, username_to_scrape = Sys.info()[["user"
dplyr::filter(.data$username == username_to_scrape) %>%
dplyr::filter(!is.na(.data$api_token)) %>%
dplyr::select(
.data$project_id,
.data$username,
.data$api_token
"project_id",
"username",
"api_token"
) %>%
# add project information
dplyr::left_join(
dplyr::tbl(conn, "redcap_projects") %>%
dplyr::select(
.data$project_id,
.data$app_title
"project_id",
"app_title"
),
by = "project_id"
) %>%
dplyr::collect() %>%
# bind_rows used over rbind to avoid need to align column order
dplyr::bind_rows(super_credentials) %>%
dplyr::rename(
project_display_name = .data$app_title,
token = .data$api_token # rename for compatibility with REDCapR credential objects
project_display_name = "app_title",
token = "api_token" # rename for compatibility with REDCapR credential objects
)

return(credentials)
Expand Down
73 changes: 73 additions & 0 deletions vignettes/credential-scraping.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,79 @@ dbDisconnect(file_conn)
dbDisconnect(source_conn)
```

### Scraping one user's API tokens

Scraping one user's API tokens and putting them in a new, local sqlite DB for use.

You must provide the username in the API_USER environment variable or on the command line. To specify it on the command line via `Rscript` and specify a REDCap username. e.g.

```sh
Rscript scrape_one_user.R jane_doe
```

```{r, eval = FALSE}
library(redcapcustodian)
library(DBI)
library(tidyverse)
library(dotenv)
# Get the username provided on the command line if one is provided.
# Otherwise, get the username form the environment.
# Otherwise, exit.
args <- commandArgs(trailingOnly = TRUE)
if (length(args) > 0) {
# Use the command line argument
username <- args
} else if (Sys.getenv("API_USER") != "") {
# Use the environment variable
username <- Sys.getenv("API_USER")
} else {
# Exit
warning("Please provide a username that whose API tokens you want to read either on the command line or via the API_USER environment variable. No action taken.")
quit()
}
# Creates the credentials file if one does not exists.
# Otherwise it deletes the credentials file
credentials_db_path <- here::here(paste0("credentials-", username, ".db"))
if (fs::file_exists(credentials_db_path)) fs::file_delete(credentials_db_path)
file_conn <- DBI::dbConnect(RSQLite::SQLite(), credentials_db_path)
# SQLite friendly schema
credentials_sql <- "CREATE TABLE IF NOT EXISTS `credentials` (
`redcap_uri` TEXT NOT NULL,
`server_short_name` varchar(128) NOT NULL,
`username` varchar(191) NOT NULL,
`project_id` int(10) NOT NULL,
`project_display_name` TEXT NOT NULL,
`project_short_name` varchar(128) DEFAULT NULL,
`token` varchar(64) NOT NULL,
`comment` varchar(256) DEFAULT NULL
);
"
dbExecute(file_conn, credentials_sql)
# Connect to the REDCap database specified in the environment
# loaded by the dotenv library
source_conn <- connect_to_redcap_db()
source_credentials <- scrape_user_api_tokens(
conn = source_conn,
username_to_scrape = username
)
# alter credentials to match local schema
source_credentials_upload <- source_credentials %>%
mutate(
redcap_uri = Sys.getenv("URI"),
server_short_name = tolower(Sys.getenv("INSTANCE"))
)
dbAppendTable(file_conn, "credentials", source_credentials_upload)
dbDisconnect(file_conn)
dbDisconnect(source_conn)
```
### Creating API tokens for all local projects

```{r, eval = FALSE}
Expand Down

0 comments on commit b753c0a

Please sign in to comment.