Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any resource #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Suggests:
Depends: R (>= 4.1.0)
Encoding: UTF-8
VignetteBuilder: knitr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
LazyData: true
13 changes: 10 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# dhis2r 0.2.0

## Improvements

* Add authentication via personal access token @Brar

* Added `get_any_analytic()` to cater for long DHIS2 favorites

# dhis2r 0.1.1
Updated tests for analytics to be flexible to cover unavailable data on a DHIS2 instance

# dhis2r 0.1.0
* Fix R CMD check failures on CRAN

* Added a `NEWS.md` file to track changes to the package.
* Added a NEWS.md file to track changes to the package.
49 changes: 46 additions & 3 deletions R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
}

self$request_sent <- self$request_sent |>
req_url_query(paging = "false") |>
# req_url_query(paging = "false") |>
req_headers("Accept" = "application/json") |>
httr2::req_user_agent("dhis2r (http://www.amanyiraho.com/dhis2r/") |>
httr2::req_retry(max_tries = 5)
Expand Down Expand Up @@ -233,7 +233,7 @@
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Get all possible analytics resources from a DHIS2 instance i.e
#'
#' @return A vector of all possible fields for a specific metadata
#' @return A data frame of the analytics resource
#'
#' @param analytic vector of ID of specific analytic(s) from a DHIS2 instance
#' @param org_unit vector of ID of specific organisation unit(s) from a DHIS2 instance
Expand Down Expand Up @@ -284,7 +284,50 @@



}
},

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @description Get all any analytics resource from a DHIS2 instance to cater for long DHIS2 favorites
#'
#' @return A data frame of the analytics resource
#'
#' @param endpoint_url string part of Analytic(s) from a DHIS2 instance api endpoint starting from `analytics.json?dimension=dx%3...`
#'
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

get_any_analytic = function(endpoint_url) {
# Check for internet
check_internet()
args <- list(endpoint_url = endpoint_url)

Check warning on line 301 in R/connect.R

View check run for this annotation

Codecov / codecov/patch

R/connect.R#L300-L301

Added lines #L300 - L301 were not covered by tests
#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")

Check warning on line 305 in R/connect.R

View check run for this annotation

Codecov / codecov/patch

R/connect.R#L304-L305

Added lines #L304 - L305 were not covered by tests


response_object <- self$request_sent |>
req_url_path_append(endpoint_url) |>
req_perform()

Check warning on line 310 in R/connect.R

View check run for this annotation

Codecov / codecov/patch

R/connect.R#L308-L310

Added lines #L308 - L310 were not covered by tests

print(response_object$url)

Check warning on line 312 in R/connect.R

View check run for this annotation

Codecov / codecov/patch

R/connect.R#L312

Added line #L312 was not covered by tests


response_data <- response_object |>
resp_body_json(simplifyVector = TRUE, flatten = TRUE)

Check warning on line 316 in R/connect.R

View check run for this annotation

Codecov / codecov/patch

R/connect.R#L315-L316

Added lines #L315 - L316 were not covered by tests

if(length(response_data$rows) == 0){

Check warning on line 318 in R/connect.R

View check run for this annotation

Codecov / codecov/patch

R/connect.R#L318

Added line #L318 was not covered by tests

as.data.frame(response_data$rows)

Check warning on line 320 in R/connect.R

View check run for this annotation

Codecov / codecov/patch

R/connect.R#L320

Added line #L320 was not covered by tests

}else{
as.data.frame(response_data$rows) |>
tibble::as_tibble()

Check warning on line 324 in R/connect.R

View check run for this annotation

Codecov / codecov/patch

R/connect.R#L323-L324

Added lines #L323 - L324 were not covered by tests
}



}



)
Expand Down
2 changes: 2 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ Alternatively you can generate a [personal access token](https://docs.dhis2.org/

``` 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")
```
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ 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

Expand Down
30 changes: 27 additions & 3 deletions man/Dhis2r.Rd

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

Loading
Loading