Skip to content

Commit 02e1622

Browse files
authored
Merge pull request #4 from tonyfujs/svy_list
Update `get_survey_list()` to always return the complete list of available surveys
2 parents 87ca640 + 9370271 commit 02e1622

File tree

7 files changed

+103
-8
lines changed

7 files changed

+103
-8
lines changed

DESCRIPTION

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ Description: More about what it does (maybe more than one line)
99
License: MIT + file LICENSE
1010
Encoding: UTF-8
1111
LazyData: true
12-
Imports: httr,
13-
jsonlite
14-
RoxygenNote: 6.1.1
12+
Imports:
13+
httr,
14+
jsonlite,
15+
purrr
16+
RoxygenNote: 7.1.1
1517
Suggests: knitr,
1618
rmarkdown
1719
VignetteBuilder: knitr

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export(connect_mdlib)
44
export(get_access_types)
5+
export(get_data_dictionary)
56
export(get_datafiles_list)
67
export(get_doc_metadata)
78
export(get_public_survey_list)

R/get_data_dictionary.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#' get_data_dictionary
2+
#'
3+
#' @param id numeric: Survey Unique ID
4+
#' @param token character: Microdata API authentication token
5+
#' @param limit character: maximum number of records per page
6+
#'
7+
#' @return list
8+
#' @export
9+
#'
10+
11+
get_data_dictionary <- function(id, token, limit = NULL){
12+
if (is.null(limit)) {
13+
path <- paste0('index.php/api/v2/metadata/list_variables/', id)
14+
out <- connect_mdlib(path = path, token = token)
15+
out <- out$content$items
16+
17+
return(out)
18+
19+
} else {
20+
out <- vector(mode = 'list', length = 1000)
21+
counter = 1
22+
23+
records_number <- limit
24+
while (records_number == limit) {
25+
temp_offset <- (counter - 1) * limit
26+
path <- paste0('index.php/api/v2/metadata/list_variables/', id, '/', limit, '/', temp_offset)
27+
temp_out <- connect_mdlib(path = path, token = token)
28+
temp_out <- temp_out$content$items
29+
out[[counter]] <- temp_out
30+
counter <- counter + 1
31+
records_number <- length(temp_out)
32+
}
33+
34+
out <- purrr::flatten(out)
35+
36+
return(out)
37+
}
38+
}

R/get_survey_list.R

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,33 @@
77
#'
88

99
get_survey_list <- function(token){
10-
out <- connect_mdlib(path = 'index.php/api/catalog/search?ps=10000', token = token)
10+
11+
n_records <- get_number_of_surveys(token)
12+
path <- paste0('index.php/api/catalog/search?ps=', n_records)
13+
out <- connect_mdlib(path = path, token = token)
1114
out <- out$content$result$rows
1215

1316
return(out)
1417
}
18+
19+
get_number_of_surveys <- function(token) {
20+
# Should be better integrated with the other functions (URL should not be hard coded)
21+
url <- "http://microdatalib.worldbank.org/index.php/api/catalog/search?ps=1"
22+
23+
resp <- httr::GET(url,
24+
httr::add_headers(.headers = c('X-API-KEY' = token,
25+
'charset' = "utf-8")),
26+
httr::accept_json())
27+
28+
# Return useful message on error
29+
httr::stop_for_status(resp, task = 'complete request to Microdata library API\n')
30+
# CHECK: datatype is .JSON
31+
if (httr::http_type(resp) != "application/json") {
32+
stop("API did not return json", call. = FALSE)
33+
}
34+
# Parse response
35+
parsed <- jsonlite::fromJSON(httr::content(resp, "text"), simplifyVector = FALSE)
36+
n_records <- parsed$result$total
37+
38+
return(n_records)
39+
}

man/connect_mdlib.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_data_dictionary.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/Getting started with mdlibconnect.Rmd renamed to vignettes/getting_started.Rmd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ You can get your API KEY by:
3232
1. Loging using your World Bank SecureID token
3333
1. Clicking the __create new key__ button.
3434

35+
API documentation can be accessed [here](http://microdatalibqa.worldbank.org/apidocumentation/#api-Metadata-datafile_variables)
36+
3537
### List all available surveys
3638
```{r}
37-
library(mdlibconnect)
38-
get_survey_list(token = Sys.getenv('mdlib_token'))
39+
devtools::load_all()
40+
response <- get_survey_list(token = Sys.getenv('mdlib_token'))
41+
str(response, max.level = 2, list.len = 3)
42+
3943
```
4044

4145

0 commit comments

Comments
 (0)