Skip to content

Commit

Permalink
check args default values and consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Karim-Mane authored and Bisaloo committed Oct 30, 2023
1 parent 00d6845 commit 2ffa588
Show file tree
Hide file tree
Showing 25 changed files with 525 additions and 319 deletions.
56 changes: 29 additions & 27 deletions R/check_dhis2_attributes-helpers.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Make an API request to the target DHIS2 system
#'
#' @param base_url the base URL of the DHIS2 server
#' @param username the user name
#' @param user_name the user name
#' @param password the user's password
#' @param which the target DHIS2 attribute name. Possible values are:
#' \enumerate{
Expand All @@ -16,11 +16,13 @@
#' interest.
#' @keywords internal
#' @noRd
make_api_request <- function(base_url, username, password,
which = "dataElements") {
dhis2_make_api_request <- function(base_url = "https://play.dhis2.org/dev",
user_name = "admin",
password = "district",
which = "dataElements") {
checkmate::assert_character(base_url, len = 1L, any.missing = FALSE,
null.ok = FALSE, pattern = "https://")
checkmate::assert_character(username,
checkmate::assert_character(user_name,
len = 1L, any.missing = FALSE,
null.ok = FALSE)
checkmate::assert_character(password,
Expand All @@ -38,14 +40,14 @@ make_api_request <- function(base_url, username, password,
which,
"?fields=id,name,shortName&paging=false"
)
response <- httr::GET(url, httr::authenticate(username, password))
response <- httr::GET(url, httr::authenticate(user_name, password))
response
}

#' Get the relevant dataset
#'
#' @param base_url the web address of the server the user wishes to log in to
#' @param username the user name
#' @param user_name the user name
#' @param password the user's password
#' @param attribute_id a comma-separated list of DHIS2 attribute ids. The ids
#' could be those of a dataSet or an organisationUnit.
Expand All @@ -58,25 +60,25 @@ make_api_request <- function(base_url, username, password,
#' @examples
#' \dontrun{
#' result <- dhis2_get_relevant_attributes(
#' attribute_id = "pBOMPrpg1QX,BfMAe6Itzgt",
#' base_url = "https://play.dhis2.org/dev/",
#' username = "admin",
#' user_name = "admin",
#' password = "district",
#' attribute_id = "pBOMPrpg1QX,BfMAe6Itzgt",
#' which = "dataSets"
#' )
#' }
#' @keywords internal
#' @noRd
dhis2_get_relevant_attributes <- function(attribute_id = NULL,
base_url =
dhis2_get_relevant_attributes <- function(base_url =
"https://play.dhis2.org/dev/",
username = "admin",
password = "district",
which = "dataSets") {
user_name = "admin",
password = "district",
attribute_id = NULL,
which = "dataSets") {
checkmate::assert_character(base_url,
len = 1L, any.missing = FALSE,
null.ok = FALSE)
checkmate::assert_character(username,
checkmate::assert_character(user_name,
len = 1L, any.missing = FALSE,
null.ok = FALSE)
checkmate::assert_character(password,
Expand All @@ -93,14 +95,14 @@ dhis2_get_relevant_attributes <- function(attribute_id = NULL,
if (is.character(attribute_id)) {
attribute_id <- unlist(strsplit(attribute_id, ",", fixed = TRUE))
}
response <- make_api_request(base_url, username, password, which)
response <- dhis2_make_api_request(base_url, user_name, password, which)
content <- httr::content(response, as = "parsed")
attributes <- do.call(rbind.data.frame, content[[which]])
if (which != "dataElements") {
idx <- which(attribute_id %in% attributes[["id"]])
if (length(idx) == 0L) {
stop("Provided attribute ids not found!\n
Use readepi:::make_api_request() function to view the list of
Use readepi:::dhis2_make_api_request() function to view the list of
available attributes")
}
if (length(idx) < length(attribute_id)) {
Expand All @@ -109,7 +111,7 @@ dhis2_get_relevant_attributes <- function(attribute_id = NULL,
glue::glue_collapse(attribute_id[-idx], sep = ", ")
)
}
attribute_id <- paste(attribute_id[idx], collapse = ",")
attribute_id <- glue::glue_collapse(attribute_id[idx], sep = ",")
}

res <- switch(
Expand All @@ -128,7 +130,7 @@ dhis2_get_relevant_attributes <- function(attribute_id = NULL,
#' Get the target DHIS2 attribute identifiers and names
#'
#' @param base_url the base URL of the DHIS2 server
#' @param username the user name
#' @param user_name the user name
#' @param password the user's password
#' @param which the target DHIS2 attribute name.
#'
Expand All @@ -140,19 +142,19 @@ dhis2_get_relevant_attributes <- function(attribute_id = NULL,
#' \dontrun{
#' datasets <- get_dhis2_attributes(
#' base_url = "https://play.dhis2.org/dev/",
#' username = "admin",
#' user_name = "admin",
#' password = "district",
#' which = "dataSets"
#' )
#' }
get_dhis2_attributes <- function(base_url = "https://play.dhis2.org/dev/",
username = "admin",
password = "district",
which = "dataSets") {
get_dhis2_attributes <- function(base_url = "https://play.dhis2.org/dev/",
user_name = "admin",
password = "district",
which = "dataSets") {
checkmate::assert_character(base_url,
len = 1L, any.missing = FALSE,
null.ok = FALSE, pattern = "https://")
checkmate::assert_character(username,
null.ok = FALSE)
checkmate::assert_character(user_name,
len = 1L, any.missing = FALSE,
null.ok = FALSE)
checkmate::assert_character(password,
Expand All @@ -163,8 +165,8 @@ get_dhis2_attributes <- function(base_url = "https://play.dhis2.org/dev/",
null.ok = FALSE)
checkmate::check_choice(which, c("dataSets", "organisationUnits",
"dataElementGroups", "dataElements"))
response <- make_api_request(base_url, username, password, which)
content <- httr::content(response, as = "parsed")
response <- dhis2_make_api_request(base_url, user_name, password, which)
content <- httr::content(response, as = "parsed")
attributes <- do.call(rbind.data.frame, content[[which]])
attributes
}
40 changes: 20 additions & 20 deletions R/check_dhis2_attributes.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Check the DHIS2 attributes for data import
#'
#' @param username the user name
#' @param password the user's password
#' @param base_url the web address of the server the user wishes to log in to
#' @param user_name the user name
#' @param password the user's password
#' @param dataset the dataSets identifiers
#' @param organisation_unit the organisationUnits identifiers
#' @param data_element_group the dataElementGroups identifiers
Expand All @@ -12,30 +12,30 @@
#' @examples
#' \dontrun{
#' attributes <- dhis2_check_attributes(
#' username = "admin",
#' password = "district",
#' base_url = "https://play.dhis2.org/dev/",
#' dataset = "pBOMPrpg1QX",
#' organisation_unit = "DiszpKrYNg8",
#' data_element_group = NULL
#' base_url = "https://play.dhis2.org/dev/",
#' user_name = "admin",
#' password = "district",
#' dataset = "pBOMPrpg1QX",
#' organisation_unit = "DiszpKrYNg8",
#' data_element_group = NULL
#' )
#' }
#'
#' @keywords internal
#' @noRd
dhis2_check_attributes <- function(username,
password,
base_url,
dataset,
dhis2_check_attributes <- function(base_url = "https://play.dhis2.org/dev/",
user_name = "admin",
password = "district",
dataset = "pBOMPrpg1QX",
organisation_unit = NULL,
data_element_group = NULL) {
# get the relevant dataset
if (!is.null(dataset)) {
tmp_res <- dhis2_get_relevant_attributes(
attribute_id = dataset,
base_url = base_url,
username = username,
user_name = user_name,
password = password,
attribute_id = dataset,
which = "dataSets"
)
dataset <- tmp_res[["dataset"]]
Expand All @@ -47,10 +47,10 @@ dhis2_check_attributes <- function(username,
# get the relevant organisation units
if (!is.null(organisation_unit)) {
tmp_res <- dhis2_get_relevant_attributes(
attribute_id = organisation_unit,
base_url = base_url,
username = username,
user_name = user_name,
password = password,
attribute_id = organisation_unit,
which = "organisationUnits"
)
organisation_unit <- tmp_res[["organisation_unit"]]
Expand All @@ -62,10 +62,10 @@ dhis2_check_attributes <- function(username,
# get the relevant data element groups
if (!is.null(data_element_group)) {
tmp_res <- dhis2_get_relevant_attributes(
attribute_id = data_element_group,
base_url = base_url,
username = username,
user_name = user_name,
password = password,
attribute_id = data_element_group,
which = "dataElementGroups"
)
data_element_group <- tmp_res[["data_element_group"]]
Expand All @@ -76,10 +76,10 @@ dhis2_check_attributes <- function(username,

# get the data element
data_elements <- dhis2_get_relevant_attributes(
attribute_id = NULL,
base_url = base_url,
username = username,
user_name = user_name,
password = password,
attribute_id = NULL,
which = "dataElements"
)

Expand Down
47 changes: 38 additions & 9 deletions R/read_from_dhis2-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
#' specifying that the user was successfully connected. Otherwise, it will throw
#' an error message.
#'
#' @param username the user name
#' @param password the user's password
#' @param base_url the base URL of the DHIS2 server
#' @param user_name the user name
#' @param password the user's password
#'
#' @return a message if the dhis2_login was successful,
#' throws an error otherwise.
#'
#' @keywords internal
#' @noRd
login <- function(username, password, base_url) {
checkmate::assert_character(username,
dhis2_login <- function(base_url = file.path("https:/", "play.dhis2.org",
"dev"),
user_name = "admin",
password = "district") {
checkmate::assert_character(user_name,
len = 1L, any.missing = FALSE,
null.ok = FALSE)
checkmate::assert_character(password,
Expand All @@ -20,8 +26,8 @@ login <- function(username, password, base_url) {
checkmate::assert_character(base_url,
len = 1L, any.missing = FALSE,
null.ok = FALSE)
url <- file.path(base_url, "api", "me")
resp <- httr::GET(url, httr::authenticate(username, password))
url <- file.path(base_url, "api", "me")
resp <- httr::GET(url, httr::authenticate(user_name, password))
httr::stop_for_status(resp)
message("\nLogged in successfully!")
}
Expand Down Expand Up @@ -52,7 +58,18 @@ login <- function(username, password, base_url) {
#' }
#' @keywords internal
#' @noRd
dhis2_subset_fields <- function(data, fields) {
dhis2_subset_fields <- function(
data = readepi(
credentials_file = system.file("extdata", "test.ini",
package = "readepi"),
data_source = "https://play.dhis2.org/dev",
dataset = "pBOMPrpg1QX,BfMAe6Itzgt",
organisation_unit = "DiszpKrYNg8",
data_element_group = NULL,
start_date = "2014",
end_date = "2023"
)[["data"]],
fields = c("dataElement", "period", "value")) {
checkmate::assert_data_frame(data,
min.rows = 1L, null.ok = FALSE,
min.cols = 1L)
Expand Down Expand Up @@ -107,7 +124,19 @@ dhis2_subset_fields <- function(data, fields) {
#' }
#' @keywords internal
#' @noRd
dhis2_subset_records <- function(data, records, id_col_name) {
dhis2_subset_records <- function(
data = readepi(credentials_file = system.file("extdata",
"test.ini",
package = "readepi"),
data_source = "https://play.dhis2.org/dev",

dataset = "pBOMPrpg1QX,BfMAe6Itzgt",
organisation_unit = "DiszpKrYNg8",
data_element_group = NULL,
start_date = "2014",
end_date = "2023")[["data"]],
records = c("FTRrcoaog83", "eY5ehpbEsB7", "Ix2HsbDMLea"),
id_col_name = "dataElement") {
checkmate::assert_data_frame(data,
min.rows = 1L, null.ok = FALSE,
min.cols = 1L)
Expand Down Expand Up @@ -141,7 +170,7 @@ dhis2_subset_records <- function(data, records, id_col_name) {
#' @return an object of type `list` with the values for the DHIS2 attributes
#' @keywords internal
#' @noRd
get_attributes_from_user <- function(args_list) {
dhis2_get_attributes_from_user <- function(args_list) {
dataset <- organisation_unit <- data_element_group <- start_date <-
end_date <- NULL
if ("dataset" %in% names(args_list)) {
Expand Down
26 changes: 13 additions & 13 deletions R/read_from_dhis2.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@
#' id_col_name = "dataElement"
#' )
#' }
read_from_dhis2 <- function(base_url,
user_name,
password,
dataset,
organisation_unit,
data_element_group,
start_date,
end_date,
records = NULL,
fields = NULL,
id_col_name = "dataElement") {
read_from_dhis2 <- function(base_url = "https://play.dhis2.org/dev",
user_name = "admin",
password = "district",
dataset = "pBOMPrpg1QX",
organisation_unit = "DiszpKrYNg8",
data_element_group = "oDkJh5Ddh7d",
start_date = "2014",
end_date = "2023",
records = NULL,
fields = NULL,
id_col_name = "dataElement") {
checkmate::assert_character(base_url,
len = 1L, null.ok = FALSE,
any.missing = FALSE)
Expand All @@ -73,13 +73,13 @@ read_from_dhis2 <- function(base_url,
null.ok = TRUE, unique = TRUE)

# checking user credentials
login(user_name, password, base_url)
dhis2_login(base_url, user_name, password)

# checking the attribute details
attributes <- dhis2_check_attributes(
base_url,
user_name,
password,
base_url,
dataset,
organisation_unit,
data_element_group
Expand Down
Loading

0 comments on commit 2ffa588

Please sign in to comment.