From 2a3d74f8f9bd4be2562f0feed1a140cc229a4b4c Mon Sep 17 00:00:00 2001 From: Brar Piening Date: Sun, 3 Dec 2023 22:23:14 +0100 Subject: [PATCH] Add authentication via personal access token --- R/connect.R | 26 ++++++++++++++++++++------ README.Rmd | 10 ++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/R/connect.R b/R/connect.R index 97131e7..70ec732 100644 --- a/R/connect.R +++ b/R/connect.R @@ -61,43 +61,57 @@ Dhis2r <- R6::R6Class( #' @param base_url Base url e.g https://play.dhis2.org/ #' @param username Registered username e.g "admin" #' @param password Registered password e.g "district" + #' @param api_token Personal Access Token (PAT) to use instead of username and password #' @param api_version The api version e.g "33" #' @param api_version_position position where the api_version is after or before in web API url i.e /api/ #' @return A new `Dhis2r` object #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - initialize = function(base_url , username , password , api_version = NULL, api_version_position = c("after", "before")) { + initialize = function(base_url , username = NULL, password = NULL, api_token = NULL, api_version = NULL, api_version_position = c("after", "before")) { api_version_position <- match.arg(api_version_position) - args <- list(base_url = base_url, username = username, password = password, api_version_position = api_version_position) + args <- list(base_url = base_url, 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 arguements") attempt::stop_if_none(args, is.character, "All arguements should be type character") + if(is.null(api_token)){ + up <- list(username = username, password = password) + attempt::stop_if_any(up, is.null,"You need to specify either api_token or both, username and password") + attempt::stop_if_none(up, is.character, "The username and password argument should be type character") + self$request_sent <- request(base_url = base_url) |> + req_auth_basic(username = username, password = password ) + }else if(!is.character(api_token)) { + stop("The api_token argument should be type character") + } + else { + self$request_sent <- request(base_url = base_url) |> + req_headers("Authorization" = paste("ApiToken", api_token, sep=" ")) + } + if(is.null(api_version)){ - self$request_sent <- request(base_url = base_url) |> + self$request_sent <- self$request_sent |> req_url_path_append("api") }else if(!is.null(api_version) & api_version_position == "before"){ - self$request_sent <- request(base_url = base_url) |> + self$request_sent <- self$request_sent |> req_url_path_append(api_version) |> req_url_path_append("api") }else if(!is.null(api_version) & api_version_position == "after"){ - self$request_sent <- request(base_url = base_url) |> + self$request_sent <- self$request_sent |> req_url_path_append("api") |> req_url_path_append(api_version) } self$request_sent <- self$request_sent |> - req_auth_basic(username = username, password = password ) |> req_url_query(paging = "false") |> req_headers("Accept" = "application/json") |> httr2::req_user_agent("dhis2r (http://www.amanyiraho.com/dhis2r/") |> diff --git a/README.Rmd b/README.Rmd index 2e0b90b..26c1668 100644 --- a/README.Rmd +++ b/README.Rmd @@ -70,6 +70,16 @@ dhis2_play_connection <- Dhis2r$new(base_url = "https://play.dhis2.org/", api_version_position = "before") ``` +Alternatively you can generate a [personal access token](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/introduction.html#webapi_pat_authentication) and use that to connect. + + +``` r +library(dhis2r) +# Replace the value of api_token with the token you have generated +dhis2_play_connection <- Dhis2r$new(base_url = "https://play.dhis2.org/", + api_token = "d2pat_5xVA12xyUbWNedQxy4ohH77WlxRGVvZZ1151814092") +``` + `Dhis2r$new()` returns a `Dhis2r` R6 class which represents a DHIS2 connection and can be used to query the DHIS2 instance