Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-bao committed May 15, 2023
2 parents 55f17e9 + cd7e37e commit 88b5103
Show file tree
Hide file tree
Showing 14 changed files with 1,014 additions and 331 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ Suggests:
License: GPL-3 + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
VignetteBuilder: knitr
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export(getDataElementGroups)
export(getDataElements)
export(getDataSets)
export(getDataStoreKey)
export(getDataStoreNamespaceKeys)
export(getDataStoreNamespaces)
export(getDataValueSets)
export(getDimensions)
export(getIndicatorGroupSets)
Expand Down
17 changes: 15 additions & 2 deletions R/callDATIMapi.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,21 @@ api_get <- function(path,
resp <- NULL
resp <-
try(
httr::GET(url, httr::timeout(timeout),
handle = handle)
#Is we are using an OAUTH token, we need to
#put the authorization code in the header.
#Otherwise, just use the cookie.
if (is.null(d2_session$token)) {
httr::GET(url, httr::timeout(timeout),
handle = handle)
} else {
httr::GET(url,
httr::timeout(timeout),
handle = handle,
httr::add_headers(Authorization =
paste("Bearer",
d2_session$token$credentials$access_token, sep = " ")))
}

)

# try is added in order to handle if resp comes back as a "try-error" class
Expand Down
96 changes: 71 additions & 25 deletions R/getDataStore.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
isSimpleString <- function(x) {
!(is.na(x) || x == "" || is.null(x)) & typeof(x) == "character"
}

.handleDataStoreResponse <- function(path, d2_session, retry, timeout) {
resp <-
tryCatch({
api_get(
path,
d2_session = d2_session,
retry = retry,
timeout = timeout,
verbose = TRUE
)
},
error = function(e) {
warning(paste(path, "could not be retreived from the server"))
return(NULL)
})

if (inherits(resp, "list")) {
jsonlite::parse_json(httr::content(resp$api_responses, type = "text"), simplifyVector = FALSE)
} else {
return(NULL)
}
}
#' @title getDataStoreKey returns a particular key from the datastore
#' @description The DHIS2 datastore can be used to store arbitrary JSON objects.
#' @param namespace Namespace of the datastore
Expand All @@ -20,36 +45,57 @@ getDataStoreKey <- function(namespace, key, simplify = FALSE,
retry = 3,
timeout = 60) {

isSimpleString <- function(x) {
!(is.na(x) || x == "" || is.null(x)) & typeof(x) == "character"
}

if (!isSimpleString(namespace) || !isSimpleString(key)) {
stop("Please specify the namespace and key!")
}
url <- paste0(d2_session$base_url, "api/dataStore/", namespace, "/", key)

# retry api get block, only retries if reponse code not in 400s
i <- 1
response_code <- 5

while (i <= retry && (response_code < 400 || response_code >= 500)) {
resp <- httr::GET(url, httr::timeout(timeout),
handle = d2_session$handle)
response_code <- httr::status_code(resp)
Sys.sleep(i - 1)
i <- i + 1
if (response_code == 200 &&
resp$url == url &&
httr::http_type(resp) == "application/json") {
break
}
path <- paste0("api/dataStore/", namespace, "/", key)

.handleDataStoreResponse(path, d2_session, retry, timeout)
}



#' Title getDataStoreNamespaceKeys returns all keys for a given name space
#'
#' @param namespace The name of the namespace as a string
#' @param d2_session the d2Session object, default is "d2_default_session",
#' it will be made upon login in to DATIM with loginToDATIM
#' @param retry Number of times to retry the request
#' @param timeout Timeout in number of seconds
#' @return A character vector of strings contained in the namespace.
#'
#' @export
#'
getDataStoreNamespaceKeys <- function(namespace,
d2_session = dynGet("d2_default_session", inherits = TRUE),
retry = 3,
timeout = 60) {

if (!isSimpleString(namespace)) {
stop("Please specify the namespace as a string!")
}

if (response_code == 404L) {
warning("The requested datastore object could not be found")
return(NULL)
}
path <- paste0("api/dataStore/", namespace)

.handleDataStoreResponse(path, d2_session, retry, timeout)
}

#' Title getDataStoreNamespaces returns a list of all namespaces present in the datastore
#'
#' @param d2_session the d2Session object, default is "d2_default_session",
#' it will be made upon login in to DATIM with loginToDATIM
#' @param retry Number of times to retry the request
#' @param timeout Timeout in number of seconds
#' @return A character vector of strings of the namespaces in the dataStore.
#'
#' @export
#'
getDataStoreNamespaces <- function(d2_session = dynGet("d2_default_session", inherits = TRUE),
retry = 3,
timeout = 60) {

path <- paste0("api/dataStore.json")

jsonlite::parse_json(httr::content(resp, type = "text"), simplifyVector = simplify)
.handleDataStoreResponse(path, d2_session, retry, timeout)
}
29 changes: 29 additions & 0 deletions man/getDataStoreNamespaceKeys.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/getDataStoreNamespaces.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 88b5103

Please sign in to comment.