diff --git a/DESCRIPTION b/DESCRIPTION index 95a6ad24..d419bab4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: datimutils Type: Package Title: Utilities for interacting with the DATIM api from R -Version: 0.5.0 -Date: 2022-07-29 +Version: 0.5.1 +Date: 2022-08-02 Authors@R: c( person("Scott", "Jackson", email = "sjackson@baosystems.com", diff --git a/NEWS.md b/NEWS.md index d75ad967..807b8b4a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# datimutils 0.5.1 + +## New features +* Adds `verbose` parameter to all functions which returns entire response. Set to `FALSE` by default. +* Adds `quiet` parameter to all functions which determines whether URL is printed. Set to `TRUE` by default. + # datimutils 0.5.0 ## New features diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index ce05778f..a088731c 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -7,12 +7,16 @@ #' default will not try again #' @param timeout how long should a reponse be waited for #' @param api_version defaults to current but can pass in version number +#' @param verbose return raw content with data +#' @param quiet Echo the URL which is called to the console. #' @return Result of DATIM API query returned as named list. #' api_get <- function(path, d2_session, retry = 1, timeout = 60, - api_version = NULL) { + api_version = NULL, + verbose = FALSE, + quiet = TRUE) { base_url <- d2_session$base_url handle <- d2_session$handle @@ -76,7 +80,10 @@ api_get <- function(path, # removes whitespace url <- gsub(" ", "", url) - print(url) + if (!quiet) { + print(url) + } + # retry api get block, only retries if reponse code not in 400s i <- 1 response_code <- 5 @@ -129,10 +136,16 @@ api_get <- function(path, } # extract text response from api response - resp <- jsonlite::fromJSON(httr::content(resp, as = "text"), + content <- jsonlite::fromJSON(httr::content(resp, as = "text"), simplifyDataFrame = TRUE, flatten = TRUE ) - return(resp) + if (verbose) { + return(list("data" = content, "api_responses" = resp)) + } else { + return(content) + } + + } diff --git a/R/getAnalytics.R b/R/getAnalytics.R index 34e4735e..676350cf 100644 --- a/R/getAnalytics.R +++ b/R/getAnalytics.R @@ -20,6 +20,8 @@ #' @param d2_session the d2Session object, default is "d2_default_session", #' it will be made upon logining in to datim with loginToDATIM #' @param retry retry +#' @param verbose return raw content with data +#' @param quiet Echo the URL which is called to the console if TRUE. #' @return data frame with the rows of the response getAnalytics <- function(..., @@ -31,7 +33,9 @@ getAnalytics <- function(..., return_names = FALSE, d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1, - timeout = 60) { + timeout = 60, + verbose = FALSE, + quiet = TRUE) { # cap time out at 5 minutes if (timeout > 300) { @@ -78,7 +82,14 @@ getAnalytics <- function(..., resp <- api_get(path = path, d2_session = d2_session, retry = retry, - timeout = timeout) + timeout = timeout, + verbose = verbose, + quiet = quiet) + + if (verbose) { + meta_data <- resp$api_responses + resp <- resp$data + } if (NROW(resp$rows) == 0) { return(NULL) @@ -103,8 +114,11 @@ getAnalytics <- function(..., #change data types to numeric where possible resp[, coercions == "NUMBER"] <- sapply(resp[, coercions == "NUMBER"], as.numeric) - - return(resp) + if (verbose) { + return(list("data" = resp, "api_responses" = meta_data)) + } else { + return(resp) + } } diff --git a/R/getDataValueSets.R b/R/getDataValueSets.R index 0b341f1b..854add86 100644 --- a/R/getDataValueSets.R +++ b/R/getDataValueSets.R @@ -10,13 +10,17 @@ #' with DATIM #' @param retry number of times to retry #' @param timeout number of seconds to wait during call +#' @param verbose return raw content with data +#' @param quiet Echo the URL which is called to the console if TRUE. #' @return Data frame with the data requested #' getDataValueSets <- function(variable_keys = NULL, #keys, variable_values = NULL, #values, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1, timeout = 180) { + retry = 1, timeout = 180, + verbose = FALSE, + quiet = TRUE) { #Test that the provided variables have their associated values used for # munging @@ -77,10 +81,22 @@ getDataValueSets <- function(variable_keys = NULL, #keys, path = path, d2_session = d2_session, retry = retry, - timeout = timeout + timeout = timeout, + verbose = verbose, + quiet = quiet ) + if (verbose) { + meta_data <- resp$api_responses + resp <- resp$data + } + #Create Dataframe from api response resp <- as.data.frame(resp$dataValues, stringsAsFactors = FALSE) + if (verbose) { + return(list("data" = resp, "api_responses" = meta_data)) + } else { + return(resp) + } } diff --git a/R/getMetadata.R b/R/getMetadata.R index c6f3e9c0..6a7560c8 100644 --- a/R/getMetadata.R +++ b/R/getMetadata.R @@ -79,6 +79,8 @@ simplifyStructure <- function(resp) { #' it will be made upon logining in to datim with loginToDATIM #' @param retry number of times to retry #' @param timeout integer - seconds to wait for a response, default = 180 +#' @param verbose return raw content with data +#' @param quiet Echo the URL which is called to the console if TRUE. #' @return the metadata response in json format and flattened #' @@ -89,7 +91,9 @@ getMetadata <- function(end_point, d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1, - timeout = 180) { + timeout = 180, + verbose = FALSE, + quiet = TRUE) { if (!is.character(fields)) { stop("The fields argument of getMetadata should be of type character") } @@ -125,9 +129,16 @@ getMetadata <- function(end_point, resp <- api_get( path = path, d2_session = d2_session, retry = retry, timeout = timeout, - api_version = NULL + api_version = NULL, + verbose = verbose, + quiet = quiet ) + if (verbose) { + meta_data <- resp$api_responses + resp <- resp$data + } + # simplify data structure resp <- simplifyStructure(resp) @@ -151,10 +162,18 @@ getMetadata <- function(end_point, fixed = TRUE ) )) { - return(resp[[1]]) + if (verbose) { + return(list("data" = resp[[1]], "api_responses" = meta_data)) + } else { + return(resp[[1]]) + } } - return(resp) + if (verbose) { + return(list("data" = resp, "api_responses" = meta_data)) + } else { + return(resp) + } } #' @export diff --git a/R/getMetadataEndpoint.R b/R/getMetadataEndpoint.R index 665af65b..238e6e0c 100644 --- a/R/getMetadataEndpoint.R +++ b/R/getMetadataEndpoint.R @@ -49,57 +49,60 @@ duplicateResponse <- function(resp, expand, by) { #' @param d2_session the d2Session object, default is "d2_default_session", #' it will be made upon logining in to datim with loginToDATIM #' @param retry the number of times to try the call +#' @param verbose return raw content with data #' @return the metadata response in json format and flattened #' @usage #' -#' .getMetadataEndpoint(end_point, values, by, fields, d2_session, retry) +#' .getMetadataEndpoint(end_point, values, by, fields, d2_session, retry, verbose) #' -#' getCategories(values, by, fields, d2_session, retry) +#' getCategories(values, by, fields, d2_session, retry, verbose) #' -#' getCatCombos(values, by, fields, d2_session, retry) +#' getCatCombos(values, by, fields, d2_session, retry, verbose) #' -#' getCatOptionCombos(values, by, fields, d2_session, retry) +#' getCatOptionCombos(values, by, fields, d2_session, retry, verbose) #' -#' getCatOptionGroupSets(values, by, fields, d2_session, retry) +#' getCatOptionGroupSets(values, by, fields, d2_session, retry, verbose) #' -#' getCatOptionGroups(values, by, fields, d2_session, retry) +#' getCatOptionGroups(values, by, fields, d2_session, retry, verbose) #' -#' getCatOptions(values, by, fields, d2_session, retry) +#' getCatOptions(values, by, fields, d2_session, retry, verbose) #' -#' getDataElementGroupSets(values, by, fields, d2_session, retry) +#' getDataElementGroupSets(values, by, fields, d2_session, retry, verbose) #' -#' getDataElementGroups(values, by, fields, d2_session, retry) +#' getDataElementGroups(values, by, fields, d2_session, retry, verbose) #' -#' getDataElements(values, by, fields, d2_session, retry) +#' getDataElements(values, by, fields, d2_session, retry, verbose) #' -#' getDataSets(values, by, fields, d2_session, retry) +#' getDataSets(values, by, fields, d2_session, retry, verbose) #' -#' getIndicatorGroupSets(values, by, fields, d2_session, retry) +#' getIndicatorGroupSets(values, by, fields, d2_session, retry, verbose) #' -#' getIndicatorGroups(values, by, fields, d2_session, retry) +#' getIndicatorGroups(values, by, fields, d2_session, retry, verbose) #' -#' getIndicators(values, by, fields, d2_session, retry) +#' getIndicators(values, by, fields, d2_session, retry, verbose) #' -#' getOptionGroupSets(values, by, fields, d2_session, retry) +#' getOptionGroupSets(values, by, fields, d2_session, retry, verbose) #' -#' getOptionGroups(values, by, fields, d2_session, retry) +#' getOptionGroups(values, by, fields, d2_session, retry, verbose) #' -#' getOptionSets(values, by, fields, d2_session, retry) +#' getOptionSets(values, by, fields, d2_session, retry, verbose) #' -#' getOptions(values, by, fields, d2_session, retry) +#' getOptions(values, by, fields, d2_session, retry, verbose) #' -#' getOrgUnitGroupSets(values, by, fields, d2_session, retry) +#' getOrgUnitGroupSets(values, by, fields, d2_session, retry, verbose) #' -#' getOrgUnitGroups(values, by, fields, d2_session, retry) +#' getOrgUnitGroups(values, by, fields, d2_session, retry, verbose) #' -#' getOrgUnits(values, by, fields, d2_session, retry) +#' getOrgUnits(values, by, fields, d2_session, retry, verbose) #' -#' getDimensions(values, by, fields, d2_session, retry) +#' getDimensions(values, by, fields, d2_session, retry, verbose) #' .getMetadataEndpoint <- function(end_point, values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1, + verbose = FALSE, + quiet = TRUE) { see <- try(stringi::stri_extract_all_regex(fields, "\\[[^()]+\\]")[[1]], silent = TRUE) name_reduce <- NULL @@ -164,28 +167,44 @@ duplicateResponse <- function(resp, expand, by) { end_point = !!end_point, d2_session = d2_session, x, - fields = default_fields, retry = retry + fields = default_fields, retry = retry, verbose = verbose, quiet = quiet )}) - # bind the responses - data <- do.call("rbind", data_list) - } else { - #normal route + # bind the responses + if (verbose) { + data <- do.call("rbind", lapply(data_list, `[[`, 1)) + } else { + data <- do.call("rbind", data_list) + } + + if (verbose) { + data <- list("data" = data, "api_responses" = data_list$api_responses) + } + + } else { #normal route filters <- metadataFilter( values = unique_values, property = by, operator = "in" ) - # call getMetadataa single time + # call getMetadata single time data <- getMetadata( end_point = !!end_point, d2_session = d2_session, filters, - fields = default_fields, retry = retry + fields = default_fields, + retry = retry, + verbose = verbose, + quiet = quiet ) } + if (verbose) { + meta_data <- data$api_responses + data <- data$data + } + #return NULL if there is nothing to return length_response <- try(length(data[[1]]), silent = TRUE) if (length_response == 0) { @@ -207,9 +226,17 @@ duplicateResponse <- function(resp, expand, by) { if (length(values) == 1 && length(data) == 1 && is.list(data)) { - return(data[[1]]) + if (verbose) { + return(list("data" = data[[1]], "api_responses" = meta_data)) + } else { + return(data[[1]]) + } } else { - return(data) + if (verbose) { + return(list("data" = data, "api_responses" = meta_data)) + } else { + return(data) + } } } @@ -218,13 +245,18 @@ duplicateResponse <- function(resp, expand, by) { getCategories <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, + quiet = TRUE) { .getMetadataEndpoint("categories", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry - ) + d2_session = d2_session, + retry = retry, + verbose = verbose, + quiet = quiet) } #' @export @@ -232,12 +264,18 @@ getCategories <- function(values, getCatCombos <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, + quiet = TRUE) { .getMetadataEndpoint("categoryCombos", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, + quiet = quiet ) } @@ -246,12 +284,17 @@ getCatCombos <- function(values, getCatOptionCombos <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, + quiet = TRUE) { .getMetadataEndpoint("categoryOptionCombos", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, ) } @@ -260,12 +303,16 @@ getCatOptionCombos <- function(values, getCatOptionGroupSets <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("categoryOptionGroupSets", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -274,12 +321,16 @@ getCatOptionGroupSets <- function(values, getCatOptionGroups <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("categoryOptionGroups", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -288,12 +339,16 @@ getCatOptionGroups <- function(values, getCatOptions <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("categoryOptions", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -302,12 +357,16 @@ getCatOptions <- function(values, getDataElementGroupSets <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("dataElementGroupSets", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -316,12 +375,16 @@ getDataElementGroupSets <- function(values, getDataElementGroups <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("dataElementGroups", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -330,12 +393,16 @@ getDataElementGroups <- function(values, getDataElements <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("dataElements", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -344,12 +411,16 @@ getDataElements <- function(values, getDataSets <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("dataSets", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -358,12 +429,16 @@ getDataSets <- function(values, getUserGroups <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("userGroups", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -372,12 +447,16 @@ getUserGroups <- function(values, getIndicatorGroupSets <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("indicatorGroupSets", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -386,12 +465,16 @@ getIndicatorGroupSets <- function(values, getIndicatorGroups <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("indicatorGroups", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -400,12 +483,16 @@ getIndicatorGroups <- function(values, getIndicators <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("indicators", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -414,12 +501,16 @@ getIndicators <- function(values, getOptionGroupSets <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("optionGroupSets", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -428,12 +519,16 @@ getOptionGroupSets <- function(values, getOptionGroups <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("optionGroups", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -442,12 +537,16 @@ getOptionGroups <- function(values, getOptionSets <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("optionSets", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -456,12 +555,16 @@ getOptionSets <- function(values, getOptions <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("options", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -470,12 +573,16 @@ getOptions <- function(values, getOrgUnitGroupSets <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("organisationUnitGroupSets", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -484,12 +591,16 @@ getOrgUnitGroupSets <- function(values, getOrgUnitGroups <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("organisationUnitGroups", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -498,12 +609,16 @@ getOrgUnitGroups <- function(values, getOrgUnits <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("organisationUnits", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } @@ -512,11 +627,15 @@ getOrgUnits <- function(values, getDimensions <- function(values, by = "id", fields = NULL, - d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1) { + d2_session = dynGet("d2_default_session", inherits = TRUE), + retry = 1, + verbose = FALSE, quiet = TRUE) { .getMetadataEndpoint("dimensions", values = values, by = as.character(rlang::ensym(by)), fields = fields, - d2_session = d2_session, retry = retry + d2_session = d2_session, + retry = retry, + verbose = verbose, quiet = quiet ) } diff --git a/R/getSqlView.R b/R/getSqlView.R index 81123a37..aaca59b9 100644 --- a/R/getSqlView.R +++ b/R/getSqlView.R @@ -15,13 +15,17 @@ #' it will be made upon logging in to datim with loginToDATIM #' @param retry number of times to retry #' @param timeout number of seconds to wait during call +#' @param verbose return raw content with data +#' @param quiet Echo the URL which is called if TRUE. #' @return dataframe with the results of the sql view getSqlView <- function(..., sql_view_uid, variable_keys = NULL, variable_values = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1, timeout = 180) { + retry = 1, timeout = 180, + verbose = FALSE, + quiet = TRUE) { assertthat::assert_that(length(variable_keys) == length(variable_values)) @@ -52,9 +56,16 @@ getSqlView <- function(..., sql_view_uid, variable_keys = NULL, path = path, d2_session = d2_session, retry = retry, - timeout = timeout + timeout = timeout, + verbose = verbose, + quiet = quiet ) + if (verbose) { + meta_data <- resp$api_responses + resp <- resp$data + } + x <- resp$listGrid$headers$name if (length(resp$listGrid$rows) == 0) { @@ -77,7 +88,11 @@ getSqlView <- function(..., sql_view_uid, variable_keys = NULL, colnames(resp) <- x - return(resp) + if (verbose) { + return(list("data" = resp, "api_responses" = meta_data)) + } else { + return(resp) + } } #' @export diff --git a/R/loginToDATIM.R b/R/loginToDATIM.R index ae3d6d6a..f9b7bfac 100644 --- a/R/loginToDATIM.R +++ b/R/loginToDATIM.R @@ -25,8 +25,6 @@ d2Session <- R6::R6Class("d2Session", #' @param handle httr handle to be used for dhis2 #' connections #' @param me DHIS2 me response object - #' @param max_cache_age cache expiry currently used - #' by datim validation initialize = function(config_path = NA_character_, base_url, handle, diff --git a/man/api_get.Rd b/man/api_get.Rd index a47339d9..8984c455 100644 --- a/man/api_get.Rd +++ b/man/api_get.Rd @@ -4,7 +4,15 @@ \alias{api_get} \title{Execute and return a DATIM API query.} \usage{ -api_get(path, d2_session, retry = 1, timeout = 60, api_version = NULL) +api_get( + path, + d2_session, + retry = 1, + timeout = 60, + api_version = NULL, + verbose = FALSE, + quiet = TRUE +) } \arguments{ \item{path}{Should begin with api/ and contain the query} @@ -18,6 +26,10 @@ default will not try again} \item{timeout}{how long should a reponse be waited for} \item{api_version}{defaults to current but can pass in version number} + +\item{verbose}{return raw content with data} + +\item{quiet}{Echo the URL which is called to the console.} } \value{ Result of DATIM API query returned as named list. diff --git a/man/dot-getMetadataEndpoint.Rd b/man/dot-getMetadataEndpoint.Rd index 33913f63..c5e55d1f 100644 --- a/man/dot-getMetadataEndpoint.Rd +++ b/man/dot-getMetadataEndpoint.Rd @@ -26,56 +26,58 @@ \alias{getDimensions} \title{getMetadataEndpoint} \usage{ -.getMetadataEndpoint(end_point, values, by, fields, d2_session, retry) +.getMetadataEndpoint(end_point, values, by, fields, d2_session, retry, verbose) -getCategories(values, by, fields, d2_session, retry) +getCategories(values, by, fields, d2_session, retry, verbose) -getCatCombos(values, by, fields, d2_session, retry) +getCatCombos(values, by, fields, d2_session, retry, verbose) -getCatOptionCombos(values, by, fields, d2_session, retry) +getCatOptionCombos(values, by, fields, d2_session, retry, verbose) -getCatOptionGroupSets(values, by, fields, d2_session, retry) +getCatOptionGroupSets(values, by, fields, d2_session, retry, verbose) -getCatOptionGroups(values, by, fields, d2_session, retry) +getCatOptionGroups(values, by, fields, d2_session, retry, verbose) -getCatOptions(values, by, fields, d2_session, retry) +getCatOptions(values, by, fields, d2_session, retry, verbose) -getDataElementGroupSets(values, by, fields, d2_session, retry) +getDataElementGroupSets(values, by, fields, d2_session, retry, verbose) -getDataElementGroups(values, by, fields, d2_session, retry) +getDataElementGroups(values, by, fields, d2_session, retry, verbose) -getDataElements(values, by, fields, d2_session, retry) +getDataElements(values, by, fields, d2_session, retry, verbose) -getDataSets(values, by, fields, d2_session, retry) +getDataSets(values, by, fields, d2_session, retry, verbose) -getIndicatorGroupSets(values, by, fields, d2_session, retry) +getIndicatorGroupSets(values, by, fields, d2_session, retry, verbose) -getIndicatorGroups(values, by, fields, d2_session, retry) +getIndicatorGroups(values, by, fields, d2_session, retry, verbose) -getIndicators(values, by, fields, d2_session, retry) +getIndicators(values, by, fields, d2_session, retry, verbose) -getOptionGroupSets(values, by, fields, d2_session, retry) +getOptionGroupSets(values, by, fields, d2_session, retry, verbose) -getOptionGroups(values, by, fields, d2_session, retry) +getOptionGroups(values, by, fields, d2_session, retry, verbose) -getOptionSets(values, by, fields, d2_session, retry) +getOptionSets(values, by, fields, d2_session, retry, verbose) -getOptions(values, by, fields, d2_session, retry) +getOptions(values, by, fields, d2_session, retry, verbose) -getOrgUnitGroupSets(values, by, fields, d2_session, retry) +getOrgUnitGroupSets(values, by, fields, d2_session, retry, verbose) -getOrgUnitGroups(values, by, fields, d2_session, retry) +getOrgUnitGroups(values, by, fields, d2_session, retry, verbose) -getOrgUnits(values, by, fields, d2_session, retry) +getOrgUnits(values, by, fields, d2_session, retry, verbose) -getDimensions(values, by, fields, d2_session, retry) +getDimensions(values, by, fields, d2_session, retry, verbose) getCategories( values, by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getCatCombos( @@ -83,7 +85,9 @@ getCatCombos( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getCatOptionCombos( @@ -91,7 +95,9 @@ getCatOptionCombos( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getCatOptionGroupSets( @@ -99,7 +105,9 @@ getCatOptionGroupSets( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getCatOptionGroups( @@ -107,7 +115,9 @@ getCatOptionGroups( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getCatOptions( @@ -115,7 +125,9 @@ getCatOptions( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getDataElementGroupSets( @@ -123,7 +135,9 @@ getDataElementGroupSets( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getDataElementGroups( @@ -131,7 +145,9 @@ getDataElementGroups( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getDataElements( @@ -139,7 +155,9 @@ getDataElements( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getDataSets( @@ -147,7 +165,9 @@ getDataSets( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getUserGroups( @@ -155,7 +175,9 @@ getUserGroups( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getIndicatorGroupSets( @@ -163,7 +185,9 @@ getIndicatorGroupSets( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getIndicatorGroups( @@ -171,7 +195,9 @@ getIndicatorGroups( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getIndicators( @@ -179,7 +205,9 @@ getIndicators( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getOptionGroupSets( @@ -187,7 +215,9 @@ getOptionGroupSets( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getOptionGroups( @@ -195,7 +225,9 @@ getOptionGroups( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getOptionSets( @@ -203,7 +235,9 @@ getOptionSets( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getOptions( @@ -211,7 +245,9 @@ getOptions( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getOrgUnitGroupSets( @@ -219,7 +255,9 @@ getOrgUnitGroupSets( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getOrgUnitGroups( @@ -227,7 +265,9 @@ getOrgUnitGroups( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getOrgUnits( @@ -235,7 +275,9 @@ getOrgUnits( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) getDimensions( @@ -243,7 +285,9 @@ getDimensions( by = "id", fields = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), - retry = 1 + retry = 1, + verbose = FALSE, + quiet = TRUE ) } \arguments{ @@ -261,6 +305,8 @@ components are present} it will be made upon logining in to datim with loginToDATIM} \item{retry}{the number of times to try the call} + +\item{verbose}{return raw content with data} } \value{ the metadata response in json format and flattened diff --git a/man/getAnalytics.Rd b/man/getAnalytics.Rd index 9246b5ef..f91ccf59 100644 --- a/man/getAnalytics.Rd +++ b/man/getAnalytics.Rd @@ -19,7 +19,9 @@ getAnalytics( return_names = FALSE, d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1, - timeout = 60 + timeout = 60, + verbose = FALSE, + quiet = TRUE ) } \arguments{ @@ -55,6 +57,10 @@ list(startDate = "2018-01-01", endDate = "2018-06-01")]} it will be made upon logining in to datim with loginToDATIM} \item{retry}{retry} + +\item{verbose}{return raw content with data} + +\item{quiet}{Echo the URL which is called to the console if TRUE.} } \value{ data frame with the rows of the response diff --git a/man/getDataValueSets.Rd b/man/getDataValueSets.Rd index 3a085eb9..a077de7d 100644 --- a/man/getDataValueSets.Rd +++ b/man/getDataValueSets.Rd @@ -9,7 +9,9 @@ getDataValueSets( variable_values = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1, - timeout = 180 + timeout = 180, + verbose = FALSE, + quiet = TRUE ) } \arguments{ @@ -20,6 +22,10 @@ with DATIM} \item{timeout}{number of seconds to wait during call} +\item{verbose}{return raw content with data} + +\item{quiet}{Echo the URL which is called to the console if TRUE.} + \item{keys}{character vector - data value set parameter keys (e.g. "dataSet", "period")} diff --git a/man/getMetadata.Rd b/man/getMetadata.Rd index 957a8017..f7957d1e 100644 --- a/man/getMetadata.Rd +++ b/man/getMetadata.Rd @@ -11,7 +11,9 @@ getMetadata( as_vector = TRUE, d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1, - timeout = 180 + timeout = 180, + verbose = FALSE, + quiet = TRUE ) } \arguments{ @@ -48,6 +50,10 @@ it will be made upon logining in to datim with loginToDATIM} \item{retry}{number of times to retry} \item{timeout}{integer - seconds to wait for a response, default = 180} + +\item{verbose}{return raw content with data} + +\item{quiet}{Echo the URL which is called to the console if TRUE.} } \value{ the metadata response in json format and flattened diff --git a/man/getSqlView.Rd b/man/getSqlView.Rd index 15bfc718..352f439a 100644 --- a/man/getSqlView.Rd +++ b/man/getSqlView.Rd @@ -11,7 +11,9 @@ getSqlView( variable_values = NULL, d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1, - timeout = 180 + timeout = 180, + verbose = FALSE, + quiet = TRUE ) } \arguments{ @@ -32,6 +34,10 @@ it will be made upon logging in to datim with loginToDATIM} \item{retry}{number of times to retry} \item{timeout}{number of seconds to wait during call} + +\item{verbose}{return raw content with data} + +\item{quiet}{Echo the URL which is called if TRUE.} } \value{ dataframe with the results of the sql view diff --git a/tests/testthat/test-getAnalytics.R b/tests/testthat/test-getAnalytics.R index 26062d25..918c234b 100644 --- a/tests/testthat/test-getAnalytics.R +++ b/tests/testthat/test-getAnalytics.R @@ -29,8 +29,15 @@ data <- getAnalytics("displayProperty=NAME", "hierarchyMeta=true", pe = c("THIS_YEAR", "LAST_YEAR"), ou = c("ImspTQPwCqd", "LEVEL-4"), d2_session = play2335, timeout = 80) +data2 <- getAnalytics("displayProperty=NAME", "hierarchyMeta=true", + dx = c("fbfJHSPpUQD", "cYeuwXTCPkU"), + pe = c("THIS_YEAR", "LAST_YEAR"), + ou = c("ImspTQPwCqd", "LEVEL-4"), + d2_session = play2335, timeout = 80, + verbose = TRUE) testthat::expect_s3_class(data, "data.frame") + testthat::expect_type(data2, "list") testthat::expect_equal(NROW(data), 4394) testthat::expect_named(data, c("Data", "Period", "Organisation unit", "Value")) rm(data) }) diff --git a/tests/testthat/test-getDataValueSets.R b/tests/testthat/test-getDataValueSets.R index 9061b9ac..366e1fbe 100644 --- a/tests/testthat/test-getDataValueSets.R +++ b/tests/testthat/test-getDataValueSets.R @@ -27,6 +27,25 @@ test_that("GetDataValueSets", { "comment", "followup")) testthat::expect_equal(NROW(data), 3) + + data2 <- getDataValueSets(c("dataSet", "period", "orgUnit"), + c("pBOMPrpg1QX", "202201", "DiszpKrYNg8"), + d2_session = play2372, + verbose = TRUE) + testthat::expect_type(data2, "list") + testthat::expect_named(data2$data, c("dataElement", + "period", + "orgUnit", + "categoryOptionCombo", + "attributeOptionCombo", + "value", + "storedBy", + "created", + "lastUpdated", + "comment", + "followup")) + testthat::expect_equal(NROW(data2$data), 3) + testthat::expect_error(getDataValueSets(c("limit"), c("3"), play2372), diff --git a/tests/testthat/test-getMetadata.R b/tests/testthat/test-getMetadata.R index 57464a7b..782e9dde 100644 --- a/tests/testthat/test-getMetadata.R +++ b/tests/testthat/test-getMetadata.R @@ -355,8 +355,15 @@ with_mock_api({ fields = "name", d2_session = play233 ) + data2 <- getMetadata( + end_point = "organisationUnitGroups", + fields = "name", + d2_session = play233, + verbose = TRUE + ) testthat::expect_equal(NROW(data), 18) testthat::expect_named(data, NULL) + testthat::expect_named(data2$data, NULL) rm(data) data <- getMetadata( diff --git a/tests/testthat/test-getMetadataEndpoint.r b/tests/testthat/test-getMetadataEndpoint.r index f868b2ce..23296176 100644 --- a/tests/testthat/test-getMetadataEndpoint.r +++ b/tests/testthat/test-getMetadataEndpoint.r @@ -47,8 +47,14 @@ httptest::with_mock_api({ data <- getOrgUnitGroups( "CXw2yu5fodb", d2_session = play233) + data2 <- getOrgUnitGroups( + "CXw2yu5fodb", + d2_session = play233, + verbose = TRUE) testthat::expect_equal(data, "CHC") + testthat::expect_equal(data2$data, "CHC") rm(data) + rm(data2) }) @@ -65,8 +71,13 @@ httptest::with_mock_api({ data <- getOrgUnitGroups( "CHC", by = "name", d2_session = play233) + data2 <- getOrgUnitGroups( + "CHC", by = "name", + d2_session = play233, + verbose = TRUE) testthat::expect_equal(data, "CXw2yu5fodb") + testthat::expect_equal(data2$data, "CXw2yu5fodb") rm(data) } ) @@ -102,10 +113,16 @@ httptest::with_mock_api({ "CHC", by = code, d2_session = play233 ) + data2 <- getOrgUnitGroups( + "CHC", by = code, + d2_session = play233, verbose = TRUE + ) - testthat::expect_equal(NROW(data), 1) - testthat::expect_equal(data, "CHC") - rm(data) + testthat::expect_equal(NROW(data), 1) + testthat::expect_equal(NROW(data2$data), 1) + testthat::expect_equal(data, "CHC") + rm(data) + rm(data2) } ) @@ -635,10 +652,16 @@ data <- getOrgUnits("Afro Arab Clinic", data <- getOrgUnits(c(long_list$id, long_list_ordered$id), d2_session = play233) + data2 <- getOrgUnits(c(long_list$id, + long_list_ordered$id), + d2_session = play233, + verbose = TRUE) testthat::expect_equal(length(data), 2664) + testthat::expect_equal(length(data2$data), 2664) testthat::expect_identical(data, c(long_list$name, long_list_ordered$name)) rm(data) + rm(data2) }) #test for split url component function diff --git a/tests/testthat/test-getSqlView.R b/tests/testthat/test-getSqlView.R index 231f9608..d712c522 100644 --- a/tests/testthat/test-getSqlView.R +++ b/tests/testthat/test-getSqlView.R @@ -44,6 +44,19 @@ testthat::expect_s3_class(data, "data.frame") testthat::expect_equal(NROW(data), 172) rm(data) }) +test_that("getSqlView verbose: ", { + data2 <- getSqlView(sql_view_uid = "tw3A6ZXOdbA", + variable_keys = c("valuetype"), + variable_values = c("TEXT"), + valuetype %.like% "TEXT", + d2_session = play2335, + verbose = TRUE) + + testthat::expect_type(data2, "list") + testthat::expect_s3_class(data2$data, "data.frame") + testthat::expect_equal(NROW(data2$data), 172) + rm(data2) }) + # https://play.dhis2.org/2.35.13/api/sqlViews/QAlOivHBY3a/data.json?paging=false test_that("We can fetch a SQL view without nested lists", { test_dataset <- getSqlView(sql_view_uid = "QAlOivHBY3a",