Skip to content

Commit

Permalink
Merge pull request #11 from amanyiraho/print-url
Browse files Browse the repository at this point in the history
add print url message
  • Loading branch information
amanyiraho authored Apr 2, 2023
2 parents ced3964 + 2bd8ba1 commit a6d32a3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 31 deletions.
53 changes: 38 additions & 15 deletions R/connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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"]])
Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand All @@ -216,20 +231,28 @@ 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)

analytic <- paste0("dx:", paste0(analytic,collapse = ";"))
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){
Expand Down
12 changes: 6 additions & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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" )

```

Expand Down
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion man/Dhis2r.Rd

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

4 changes: 4 additions & 0 deletions vignettes/dhis2r.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit a6d32a3

Please sign in to comment.