Skip to content

Commit

Permalink
Replace httr with httr2 (#77)
Browse files Browse the repository at this point in the history
* replace {httr} by {httr2}

* Update CITATION.cff

* fix linter and run devtools::document()

* update test file for check_dhis2_attributes-helpers.R

* use {httr2} to submit the query parameters

* fix linters

* allow for the query parameters to be specified as a list by the user

* update the test files to account for the fact that the query parameters are provided as a list by the user

* fix linters

* use a different areaTypeId in the examples and tests

* update the mock files

* update test for readepi()

* update test-readepi.R

* fix issue with reading from DHIS2

* Automatic readme update

* comment out the mock testing in Fingertips test files

* use "%>%" instead of "|>" for harmonisation.

* update the URL to the play DHIS2 instance

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
Karim-Mane and actions-user authored May 22, 2024
1 parent 98f7e47 commit cbef45d
Show file tree
Hide file tree
Showing 281 changed files with 999 additions and 303,326 deletions.
16 changes: 8 additions & 8 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# CITATION file created with {cffr} R package, v0.5.0
# See also: https://docs.ropensci.org/cffr/
# -----------------------------------------------------------

cff-version: 1.2.0
message: 'To cite package "readepi" in publications use:'
type: software
Expand Down Expand Up @@ -136,16 +136,16 @@ references:
orcid: https://orcid.org/0000-0002-6983-2759
year: '2024'
- type: software
title: httr
abstract: 'httr: Tools for Working with URLs and HTTP'
title: httr2
abstract: 'httr2: Perform HTTP Requests and Process the Responses'
notes: Imports
url: https://httr.r-lib.org/
repository: https://CRAN.R-project.org/package=httr
url: https://httr2.r-lib.org
repository: https://CRAN.R-project.org/package=httr2
authors:
- family-names: Wickham
given-names: Hadley
email: hadley@posit.co
year: '2024'
email: hadley@rstudio.com
year: '2023'
- type: software
title: magrittr
abstract: 'magrittr: A Forward-Pipe Operator for R'
Expand All @@ -164,7 +164,7 @@ references:
title: odbc
abstract: 'odbc: Connect to ODBC Compatible Databases (using the DBI Interface)'
notes: Imports
url: https://odbc.r-dbi.org
url: https://r-dbi.github.io/odbc/
repository: https://CRAN.R-project.org/package=odbc
authors:
- family-names: Hester
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Imports:
dplyr,
fingertipsR (>= 1.0.10.9001),
glue,
httr,
httr2,
magrittr,
odbc,
pool,
Expand Down
66 changes: 34 additions & 32 deletions R/check_dhis2_attributes-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @examples
#' \dontrun{
#' response <- dhis2_make_api_request(
#' base_url = file.path("https:/", "play.dhis2.org", "dev"),
#' base_url = file.path("https:/", "play.dhis2.org", "demo"),
#' user_name = "admin",
#' password = "district",
#' which = "dataElements"
Expand All @@ -28,25 +28,26 @@
dhis2_make_api_request <- function(base_url,
user_name,
password,
which = "dataElements") {
which = "dataElements") {
url <- file.path(
base_url,
"api",
which,
"?fields=id,name,shortName&paging=false"
glue::glue(which, "?fields=id,name,shortName&paging=false")
)
response <- httr::GET(url, httr::authenticate(user_name, password))
response
req <- httr2::request(url) %>%
httr2::req_auth_basic(user_name, password) %>%
httr2::req_perform()
req
}

#' Get the relevant dataset
#'
#' @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 attribute_id a comma-separated list of DHIS2 attribute ids. The ids
#' could be those of a dataSet or an organisationUnit.
#' @param which the target DHIS2 attribute name
#' @param attribute_id a vector of DHIS2 attribute ids. The ids
#' could be those of a dataSet or an orgUnit.
#' @param which the target DHIS2 end point
#'
#' @return a `list` of 2 elements: a `character` string with the target
#' attributes ID(s) and a `data.frame` that contains the data of interest
Expand All @@ -55,10 +56,10 @@ dhis2_make_api_request <- function(base_url,
#' @examples
#' \dontrun{
#' result <- dhis2_get_relevant_attributes(
#' base_url = "https://play.dhis2.org/dev/",
#' base_url = "https://play.dhis2.org/demo",
#' user_name = "admin",
#' password = "district",
#' attribute_id = "pBOMPrpg1QX,BfMAe6Itzgt",
#' attribute_id = c("pBOMPrpg1QX", "BfMAe6Itzgt"),
#' which = "dataSets"
#' )
#' }
Expand All @@ -71,23 +72,22 @@ dhis2_get_relevant_attributes <- function(base_url,
checkmate::assert_character(which,
len = 1L, any.missing = FALSE,
null.ok = FALSE)
checkmate::assert_character(attribute_id,
len = 1L, any.missing = FALSE,
null.ok = TRUE)
checkmate::check_choice(which, c("dataSets", "organisationUnits",
"dataElementGroups", "dataElements"))
if (is.character(attribute_id)) {
attribute_id <- unlist(strsplit(attribute_id, ",", fixed = TRUE))
}
checkmate::assert_vector(attribute_id,
min.len = 1L, any.missing = FALSE,
null.ok = TRUE)
checkmate::check_choice(which, c("dataSets", "dataElementGroups",
"organisationUnits",
"organisationUnitGroups", "dataElements"))

response <- dhis2_make_api_request(base_url, user_name, password, which)
content <- httr::content(response, as = "parsed")
content <- httr2::resp_body_json(response)
attributes <- content %>% dplyr::bind_rows()
if (which != "dataElements") {
idx <- which(attribute_id %in% attributes[["id"]])
if (length(idx) == 0L) {
stop("Provided attribute ids not found!\n
Use readepi:::dhis2_make_api_request() function to view the list of
available attributes")
stop("Provided attribute ids not found!",
"Use readepi:::dhis2_make_api_request() function to view the list",
"of available attributes")
}
if (length(idx) < length(attribute_id)) {
warning(
Expand All @@ -100,13 +100,15 @@ dhis2_get_relevant_attributes <- function(base_url,

res <- switch(
which,
dataSets = list(dataset = attribute_id,
data_sets = attributes),
organisationUnits = list(organisation_unit = attribute_id,
org_units = attributes),
dataElementGroups = list(data_element_group = attribute_id,
data_elt_groups = attributes),
dataElements = attributes
dataSets = list(dataset = attribute_id,
data_sets = attributes),
dataElementGroups = list(data_element_group = attribute_id,
data_elt_groups = attributes),
organisationUnits = list(organisation_unit = attribute_id,
org_units = attributes),
organisationUnitGroups = list(organisation_unit_group = attribute_id,
org_units_groups = attributes),
dataElements = attributes
)
res
}
Expand All @@ -125,7 +127,7 @@ dhis2_get_relevant_attributes <- function(base_url,
#' @examples
#' \dontrun{
#' datasets <- dhis2_get_attributes(
#' base_url = "https://play.dhis2.org/dev/",
#' base_url = "https://play.dhis2.org/demo/",
#' user_name = "admin",
#' password = "district",
#' which = "dataSets"
Expand All @@ -148,7 +150,7 @@ dhis2_get_attributes <- function(base_url,
checkmate::check_choice(which, c("dataSets", "organisationUnits",
"dataElementGroups", "dataElements"))
response <- dhis2_make_api_request(base_url, user_name, password, which)
content <- httr::content(response, as = "parsed")
content <- httr2::resp_body_json(response)
attributes <- content %>% dplyr::bind_rows()
attributes
}
67 changes: 40 additions & 27 deletions R/check_dhis2_attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,35 @@
#' @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
#' @param query_parameters a list with the parameters that will be used to
#' determine which data is returned by the query
#'
#' @return a list of 7 elements of type `character`.
#' @return a list of 9 elements of type `character`.
#'
#' @examples
#' \dontrun{
#' attributes <- dhis2_check_attributes(
#' base_url = "https://play.dhis2.org/dev/",
#' user_name = "admin",
#' password = "district",
#' dataset = "pBOMPrpg1QX",
#' organisation_unit = "DiszpKrYNg8",
#' data_element_group = NULL
#' )
#' attributes <- dhis2_check_attributes(
#' base_url = "https://play.dhis2.org/dev",
#' user_name = "admin",
#' password = "district",
#' query_parameters = list(dataSet = "pBOMPrpg1QX",
#' orgUnit = "DiszpKrYNg8")
#' )
#' }
#'
#' @keywords internal
#'
dhis2_check_attributes <- function(base_url,
user_name,
password,
dataset,
organisation_unit = NULL,
data_element_group = NULL) {
query_parameters) {
# get the relevant dataset
if (!is.null(dataset)) {
if ("dataSet" %in% names(query_parameters)) {
tmp_res <- dhis2_get_relevant_attributes(
base_url = base_url,
user_name = user_name,
password = password,
attribute_id = dataset,
attribute_id = query_parameters[["dataSet"]],
which = "dataSets"
)
dataset <- tmp_res[["dataset"]]
Expand All @@ -44,13 +40,28 @@ dhis2_check_attributes <- function(base_url,
dataset <- data_sets <- NULL
}

# get the relevant data element groups
if ("dataElementGroup" %in% names(query_parameters)) {
tmp_res <- dhis2_get_relevant_attributes(
base_url = base_url,
user_name = user_name,
password = password,
attribute_id = query_parameters[["dataElementGroup"]],
which = "dataElementGroups"
)
data_element_group <- tmp_res[["data_element_group"]]
data_elt_groups <- tmp_res[["data_elt_groups"]]
} else {
data_element_group <- data_elt_groups <- NULL
}

# get the relevant organisation units
if (!is.null(organisation_unit)) {
if ("orgUnit" %in% names(query_parameters)) {
tmp_res <- dhis2_get_relevant_attributes(
base_url = base_url,
user_name = user_name,
password = password,
attribute_id = organisation_unit,
attribute_id = query_parameters[["orgUnit"]],
which = "organisationUnits"
)
organisation_unit <- tmp_res[["organisation_unit"]]
Expand All @@ -59,19 +70,19 @@ dhis2_check_attributes <- function(base_url,
organisation_unit <- org_units <- NULL
}

# get the relevant data element groups
if (!is.null(data_element_group)) {
tmp_res <- dhis2_get_relevant_attributes(
# get the relevant organisation units
if ("orgUnitGroup" %in% names(query_parameters)) {
tmp_res <- dhis2_get_relevant_attributes(
base_url = base_url,
user_name = user_name,
password = password,
attribute_id = data_element_group,
which = "dataElementGroups"
attribute_id = query_parameters[["orgUnitGroup"]],
which = "organisationUnitGroups"
)
data_element_group <- tmp_res[["data_element_group"]]
data_elt_groups <- tmp_res[["data_elt_groups"]]
organisation_unit_group <- tmp_res[["organisation_unit_group"]]
org_units_groups <- tmp_res[["org_units_groups"]]
} else {
data_element_group <- data_elt_groups <- NULL
organisation_unit_group <- org_units_groups <- NULL
}

# get the data element
Expand All @@ -90,6 +101,8 @@ dhis2_check_attributes <- function(base_url,
org_units_details = org_units,
data_element_group = data_element_group,
data_element_groups_details = data_elt_groups,
organisation_unit_group = organisation_unit_group,
org_units_groups = org_units_groups,
data_elements = data_elements
)
}
Loading

0 comments on commit cbef45d

Please sign in to comment.