diff --git a/R/connect.R b/R/connect.R index bf6372c..97131e7 100644 --- a/R/connect.R +++ b/R/connect.R @@ -72,10 +72,11 @@ Dhis2r <- R6::R6Class( args <- list(base_url = base_url, username = username, password = password, api_version_position = api_version_position) #Check that at least one argument is not null - attempt::stop_if_any(args, is.null, "You need to specify all the four arguements") + attempt::stop_if_any(args, is.null,"You need to specify all arguements") + attempt::stop_if_none(args, is.character, "All arguements should be type character") - if(is.null(api_version)){ + if(is.null(api_version)){ self$request_sent <- request(base_url = base_url) |> req_url_path_append("api") @@ -115,11 +116,13 @@ Dhis2r <- R6::R6Class( # Check for internet check_internet() - reponse <- self$request_sent |> + response_object <- self$request_sent |> req_url_path_append("me") |> req_perform() - response_data <- reponse |> + print(response_object$url) + + response_data <- response_object |> resp_body_json(simplifyVector = TRUE) self$access_rights <- unlist(response_data[["access"]]) @@ -150,23 +153,31 @@ Dhis2r <- R6::R6Class( if(is.null(endpoint)){ - reponse <- self$request_sent |> + response_object <- self$request_sent |> req_url_path_append("resources") |> req_perform() - response_data <- reponse |> + print(response_object$url) + + response_data <- response_object |> resp_body_json(simplifyVector = TRUE) tibble::tibble(response_data$resources) }else{ - reponse <- self$request_sent |> + + attempt::stop_if_not(endpoint, is.character, "endpoint should be type character") + + response_object <- self$request_sent |> req_url_path_append(endpoint) |> req_url_query(fields = paste0(fields, collapse = ",")) |> req_perform() - response_data <- reponse |> + print(response_object$url) + + + response_data <- response_object |> resp_body_json(simplifyVector = TRUE) @@ -184,18 +195,22 @@ Dhis2r <- R6::R6Class( #' @param endpoint a resource, get the available resources using `get_metadata()` without any arguments #' #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - get_metadata_fields = function(endpoint = NULL) { - # Check for internet - check_internet() + get_metadata_fields = function(endpoint) { + # Check for internet + check_internet() + attempt::stop_if(endpoint, is.null, "endpoint shouldnot be NULL") + attempt::stop_if_not(endpoint, is.character, "endpoint should be type character") - reponse <- self$request_sent |> + response_object <- self$request_sent |> req_url_path_append(endpoint) |> req_url_query(fields = ":all") |> req_url_query(paging = "true") |> req_url_query(pageSize = "1") |> req_perform() - response_data <- reponse |> + print(response_object$url) + + response_data <- response_object |> resp_body_json(simplifyVector = TRUE) @@ -216,6 +231,11 @@ Dhis2r <- R6::R6Class( get_analytics= function(analytic,org_unit ,period, output_scheme= c("UID", "NAME")) { # Check for internet check_internet() + args <- list(analytic = analytic,org_unit= org_unit ,period = period, output_scheme = output_scheme) + #Check that at least one argument is not null + + attempt::stop_if_any(args, is.null,"You need to specify all arguements") + attempt::stop_if_none(args, is.character, "All arguements should be type character") output_scheme <- match.arg(output_scheme) @@ -223,13 +243,16 @@ Dhis2r <- R6::R6Class( org_unit <- paste0("dimension=ou:", paste0(org_unit,collapse = ";")) period <- paste0("dimension=pe:", paste0(period,collapse = ";")) - reponse <- self$request_sent |> + response_object <- self$request_sent |> req_url_path_append("analytics") |> req_url_query(dimension= I(paste(analytic, org_unit, period, sep = "&"))) |> req_url_query(outputIdScheme = output_scheme) |> req_perform() - response_data <- reponse |> + print(response_object$url) + + + response_data <- response_object |> resp_body_json(simplifyVector = TRUE, flatten = TRUE) if(length(response_data$rows) == 0){ diff --git a/README.Rmd b/README.Rmd index f65f9c0..2e0b90b 100644 --- a/README.Rmd +++ b/README.Rmd @@ -61,7 +61,7 @@ Users must be authenticated to access a specific DHIS2 instance before connectin The easiest way to connect to a DHIS2 instance using dhis2r is to use the `username` and `password` of the DHIS2 user. -```{r} +``` r library(dhis2r) dhis2_play_connection <- Dhis2r$new(base_url = "https://play.dhis2.org/", username = "admin", @@ -82,11 +82,11 @@ As an example, let's pull Analytics data of `BCG doses given` Analytics data can include data from indicators, dataElements, program indicators, etc. -```{r} -# dhis2_play_connection$get_analytics(analytic = "s46m5MS0hxu", #BCG doses given -# org_unit = "ImspTQPwCqd", #Sierra Leone (National level) -# period = "202101", -# output_scheme = "NAME" ) +``` r + dhis2_play_connection$get_analytics(analytic = "s46m5MS0hxu", #BCG doses given + org_unit = "ImspTQPwCqd", #Sierra Leone (National level) + period = "202101", + output_scheme = "NAME" ) ``` diff --git a/README.md b/README.md index e693a9d..fae5196 100644 --- a/README.md +++ b/README.md @@ -46,11 +46,6 @@ billion people. ## Installation -Install the stable version from CRAN - -``` r -install.packages("dhis2r") -``` You can install the development version of dhis2r from [GitHub](https://github.com/) with: @@ -91,10 +86,10 @@ Analytics data can include data from indicators, dataElements, program indicators, etc. ``` r -# dhis2_play_connection$get_analytics(analytic = "s46m5MS0hxu", #BCG doses given -# org_unit = "ImspTQPwCqd", #Sierra Leone (National level) -# period = "202101", -# output_scheme = "NAME" ) + dhis2_play_connection$get_analytics(analytic = "s46m5MS0hxu", #BCG doses given + org_unit = "ImspTQPwCqd", #Sierra Leone (National level) + period = "202101", + output_scheme = "NAME" ) ``` You can pull data on the following: diff --git a/man/Dhis2r.Rd b/man/Dhis2r.Rd index 862d57b..33fa13b 100644 --- a/man/Dhis2r.Rd +++ b/man/Dhis2r.Rd @@ -141,7 +141,7 @@ A data frame \subsection{Method \code{get_metadata_fields()}}{ Get all possible fields for a specific metadata resource from a DHIS2 instance \subsection{Usage}{ -\if{html}{\out{
}}\preformatted{Dhis2r$get_metadata_fields(endpoint = NULL)}\if{html}{\out{
}} +\if{html}{\out{
}}\preformatted{Dhis2r$get_metadata_fields(endpoint)}\if{html}{\out{
}} } \subsection{Arguments}{ diff --git a/vignettes/dhis2r.Rmd b/vignettes/dhis2r.Rmd index 0b2df38..087409b 100644 --- a/vignettes/dhis2r.Rmd +++ b/vignettes/dhis2r.Rmd @@ -29,6 +29,10 @@ dhis2_play_connection <- Dhis2r$new(base_url = "https://play.dhis2.org/", api_version_position = "before") ``` + +> [Ways of keeping your user credentials safe](https://solutions.posit.co/connections/db/best-practices/managing-credentials/) + + ### Get user information ```{r}