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

merge main into DEV #77

Merged
merged 50 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e0b40dc
Merge pull request #66 from worldbank/DEV
randrescastaneda Jul 6, 2023
9f23350
initial function to build requests using httr2
tonyfujs Aug 31, 2023
356aced
replace all references to httr with httr2
tonyfujs Sep 1, 2023
88c8e2d
adjust unit test suite and fix a few issues
tonyfujs Sep 1, 2023
e5a0a22
add documentation
tonyfujs Sep 1, 2023
80c5bec
add httr2 and R minimum version to support native pipe
tonyfujs Sep 1, 2023
23fc955
fix documentation
tonyfujs Sep 1, 2023
8eff3da
disable ad-hoc caching (to be replaced with httr2 cache mechanism)
tonyfujs Sep 1, 2023
104f887
Add automatic retry to handle throttling
tonyfujs Sep 1, 2023
b22f8c7
add response caching
tonyfujs Sep 1, 2023
8d14b16
disable httr2 cache. Not working as expected
tonyfujs Sep 1, 2023
6c897f5
add back caching with new mechanism to handle cache path
tonyfujs Sep 4, 2023
c767e48
Increment version number to 1.1.0
tonyfujs Sep 5, 2023
3130a4d
add helper functions to deal with cache
tonyfujs Sep 5, 2023
31e364c
make arrow default format
tonyfujs Sep 14, 2023
fa1af89
check() passed
giorgiacek Sep 26, 2024
ddee18b
# PR 63 added, check() passed
giorgiacek Sep 27, 2024
7638f4c
new function `get_grouped_stats()` and support `build_request_v2()`
giorgiacek Oct 2, 2024
668e7d2
changes proposed
giorgiacek Oct 3, 2024
82859fa
last changes (estimate instead of endpoint, pivoted result).
giorgiacek Oct 4, 2024
7769f4a
changed name to get_gd(), check() passed
giorgiacek Oct 7, 2024
c219985
some typos corrected
giorgiacek Oct 7, 2024
086df66
popshare added back to grouped-stats
giorgiacek Oct 7, 2024
e061e18
final get_cp() draft, no tests
giorgiacek Oct 9, 2024
558c85e
draft get-gd
giorgiacek Nov 4, 2024
60da6d7
examples added to get_gd and lorenz parameter added back, not tested,…
giorgiacek Nov 8, 2024
e0b92d0
get_gd() vignette draft (not tested as api not working yet but should…
giorgiacek Nov 8, 2024
77c3b3c
cp actually needs a default value
giorgiacek Nov 8, 2024
c918bbf
use cases added to get-gd draft
giorgiacek Nov 11, 2024
e6d6a3e
typo corrected in get_gd, get_cp examples changes and default measure…
giorgiacek Nov 11, 2024
5acca6d
new datasets to test gd data, gd data vignette
giorgiacek Nov 15, 2024
0cd973f
get_gd some changes + full coverage with tests for get_gd()
giorgiacek Nov 15, 2024
884c4af
get_cp() and get_cp_ki() with vignette done
giorgiacek Nov 18, 2024
b288fff
get_cp() tests done, coverage 100%
giorgiacek Nov 18, 2024
7e8f553
minor adjustment and full coverage
giorgiacek Nov 18, 2024
9dc23cf
full coverage and check() passed
giorgiacek Nov 18, 2024
98676be
skipped one test for json (check) and added nowcast test
giorgiacek Nov 27, 2024
a819439
get_stats() when empty with json returns a different format than othe…
giorgiacek Nov 27, 2024
ba96a0a
removed server = 'dev' and server = 'qa'. Check() passed.
giorgiacek Dec 2, 2024
9faa22a
export get_gd()
randrescastaneda Dec 6, 2024
cde5f0d
Merge remote-tracking branch 'origin/httr2_gc_2' into httr2_gc_2
randrescastaneda Dec 6, 2024
63b4929
fix bug in get_gd
randrescastaneda Dec 6, 2024
0aa1a1a
small change to get_gd() test
giorgiacek Dec 7, 2024
e94f4d8
typos in the vignettes corrected.
giorgiacek Dec 7, 2024
fed4d4b
small update
randrescastaneda Dec 9, 2024
e451617
fix legend in chart
randrescastaneda Dec 9, 2024
eee99de
Merge pull request #75 from worldbank/httr2_gc_2
randrescastaneda Dec 9, 2024
5defab9
add Giorgia as author
randrescastaneda Dec 9, 2024
cf3e853
server = 'dev' typo corrected
giorgiacek Dec 9, 2024
57f18ce
Merge pull request #76 from worldbank/typo
giorgiacek Dec 19, 2024
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
23 changes: 13 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pipr
Title: Client for the Poverty and Inequality Platform ('PIP') API
Version: 1.0.0
Version: 1.1.0
Authors@R:
c(person(given = "Tony",
family = "Fujs",
Expand All @@ -18,6 +18,10 @@ Authors@R:
family = "Castañeda",
role = "aut",
email = "acastanedaa@worldbank.org"),
person(given = "Giorgia",
family = "Cecchinato",
role = "aut",
email = "gcecchinato@worldbank.org"),
person(given = "World Bank",
role = "cph")
)
Expand All @@ -28,7 +32,7 @@ License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
URL: https://worldbank.github.io/pipr/,
https://github.com/worldbank/pipr,
<https://pip.worldbank.org/api>
Expand All @@ -41,7 +45,6 @@ Suggests:
rmarkdown,
markdown,
callr,
mockery,
ggplot2,
tidyr,
ggthemes,
Expand All @@ -50,20 +53,20 @@ Suggests:
dplyr,
readr
Language: en-US
Imports:
Imports:
arrow,
attempt,
curl,
httr,
jsonlite,
tibble,
purrr,
memoise,
cachem,
data.table,
cli,
rlang,
utils
utils,
httr2,
stringr,
vroom
Depends:
R (>= 3.6.0)
R (>= 4.1.0)
Config/testthat/edition: 3
Date: 2023-04-28
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Generated by roxygen2: do not edit by hand

export(call_aux)
export(change_grouped_stats_to_csv)
export(check_api)
export(delete_cache)
export(display_aux)
export(get_aux)
export(get_cache_info)
export(get_cp)
export(get_cp_ki)
export(get_gd)
export(get_pip_info)
export(get_stats)
export(get_versions)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# pipr 1.1.0

* [Use httr2](https://github.com/worldbank/pipr/pull/70)
* API responses are now cached locally according to the PIP API cache policy
from the PIP API responses headers
* `pipr` automatically handles retries when hitting the PIP API rate limiting
threshold
* Improved translation of HTTP errors into R error messages
* New helper functions `delete_cache()` and `get_cache_info()`

# pipr 1.0.0

* [Mock live API calls or skip them on CRAN](https://github.com/worldbank/pipr/pull/45)
Expand Down
17 changes: 10 additions & 7 deletions R/aaa.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
.pip <- new.env(parent = emptyenv())
.pip <- new.env(parent = emptyenv()) # PR 63
.pipcache <- new.env(parent = emptyenv()) # PR 63


#' Set auxiliary table in .pip environment for later call
#'
#' @param table character: name of the table in .pip env
#' @param value data to be saved
#' @inheritParams get_aux
#' @param replace logical.
#'
#' @return Invisible TRUE if set correctly. FALSE otherwise
#' @keywords internal
set_aux <- function(table,
value,
force = FALSE) {
replace = FALSE) { # PR 63
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Evaluate if exists --------

to_set <- 1
if (rlang::env_has(.pip, table)) {
if (force == FALSE) {
if (isFALSE(replace)) { # PR 63
cli::cli_alert("Table {.field {table}} already exists.")
to_set <- utils::menu(c("Replace with new table", "Abort"))
}
Expand Down Expand Up @@ -65,12 +66,12 @@ set_aux <- function(table,
#' @examples
#' # call one table
#'
#' get_aux("gdp", assign_tb = TRUE, force = TRUE)
#' get_aux("gdp", assign_tb = TRUE, replace = TRUE) # PR 63
#' call_aux("gdp")
#'
#' # see the name of several tables in memory
#' tb <- c("cpi", "ppp", "pop")
#' lapply(tb, get_aux, assign_tb = TRUE, force = TRUE)
#' lapply(tb, get_aux, assign_tb = TRUE, replace = TRUE) # PR 63
#' call_aux()
call_aux <- function(table = NULL) {

Expand Down Expand Up @@ -119,7 +120,9 @@ call_aux <- function(table = NULL) {
return(rlang::env_get(.pip, table))
} else {
msg <- c("*" = "Table {.field {table}} does not exist")
cli::cli_abort(msg, wrap = TRUE)
cli::cli_abort(msg,

wrap = TRUE)
}

}
Expand Down
88 changes: 88 additions & 0 deletions R/build_request.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#' Build request version 2
#'
#' @param server character: Server. For WB internal use only
#' @param api_version character: API version
#' @param endpoint character: PIP API endpoint
#' @param ... other parameters
#'
#' @return httr2 request
#'
build_request <- function(server,
api_version,
endpoint,
...) {



base_url <- select_base_url(server = server)
params <- list(...)

req <- httr2::request(base_url) |>
httr2::req_url_path_append(api_version) |>
httr2::req_url_path_append(endpoint) |>
# .multi = "comma" works fine without applying fix_params
httr2::req_url_query(!!!params, .multi = "comma") |>
httr2::req_cache(tools::R_user_dir("pipr", which = "cache"),
use_on_error = TRUE,
debug = TRUE) |>
httr2::req_user_agent(pipr_user_agent) |>
httr2::req_error(body = parse_error_body) |>
httr2::req_retry(
is_transient = pip_is_transient,
after = retry_after,
max_seconds = 60
)


return(req)

}




#' build_request, OLD version
#'
#' @param server character: Server. For WB internal use only
#' @param api_version character: API version
#' @param endpoint character: PIP API endpoint
#' @param ... other parameters
#'
#' @return httr2 request
#'
build_request_old <- function(server,
api_version,
endpoint,
...) {

base_url <- select_base_url(server = server)

params <- list(...)
params <- lapply(params, fix_params)

req <- httr2::request(base_url) |>
httr2::req_url_path_append(api_version) |>
httr2::req_url_path_append(endpoint) |>
httr2::req_url_query(!!!params) |>
httr2::req_cache(tools::R_user_dir("pipr", which = "cache"),
use_on_error = TRUE,
debug = TRUE) |>
httr2::req_user_agent(pipr_user_agent) |>
httr2::req_error(body = parse_error_body) |>
httr2::req_retry(
is_transient = pip_is_transient,
after = retry_after,
max_seconds = 60
)

return(req)

}

fix_params <- function(param) {
if (length(param) > 1) {
return(paste(param, collapse = ","))
} else {
return(param)
}
}
36 changes: 36 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#' Datt (1998) grouped data for rural india, 1983
#'
#' Dataset from Datt (1998) with grouped data for rural India in 1983.
#'
#' @format A data frame with 13 observations on the following 6 variables:
#' \describe{
#' \item{monthly_pc_exp}{Welfare range class}
#' \item{mean_monthly_pc_exp}{Mean welfare for given welfare range class}
#' \item{percentage_of_persons}{Percentage of individuals in given welfare class}
#' \item{L}{Cumulative welfare}
#' \item{p}{Cumulative population}
#' \item{area}{rural}
#' }
#'
#' @source Datt, G. (1998). See get_cp vignette.
#'
"datt_rural"

#' Grouped data for urban india, 1983
#'
#' Dataset from Sarvekshana N26 Vol 9 N 4, created by the author following
#' Datt(1998) methodology with grouped data for urban India in 1983.
#'
#' @format A data frame with 13 observations on the following 6 variables:
#' \describe{
#' \item{monthly_pc_exp}{Welfare range class}
#' \item{mean_monthly_pc_exp}{Mean welfare for given welfare range class}
#' \item{percentage_of_persons}{Percentage of individuals in given welfare class}
#' \item{L}{Cumulative welfare}
#' \item{p}{Cumulative population}
#' \item{area}{urban}
#' }
#'
#' @source Sarvekshana N26 Vol 9 N 4, and Datt, G. (1998) for methodology. See get_cp vignette.
#'
"datt_urban"
7 changes: 5 additions & 2 deletions R/display_aux.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ display_aux <- function(version = NULL,

# ____________________________________________________________________________
# Build query string ####
req <- build_request(server = server,
api_version = api_version,
endpoint = "aux")
res <- req |>
httr2::req_perform()

u <- build_url(server, "aux", api_version = api_version)
res <- httr::GET(u)
tbs_tb <- parse_response(res, simplify = simplify)
tbs <- tbs_tb[["tables"]]
if (isTRUE(run_cli)) {
Expand Down
29 changes: 18 additions & 11 deletions R/get_aux.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#' assigned to exactly the same name as the one of the desired table. If
#' character, the table will be assigned to that name.
#' @inheritParams get_stats
#' @param force logical: force replacement. Default is FALSE
#' @param replace logical: force replacement of aux files in `.pip` env. Default
#' is FALSE.
#'
#' @return If `simplify = FALSE`, it returns a list of class "pip_api". If
#' `simplify = TRUE`, it returns a tibble with the requested data. This is the
Expand Down Expand Up @@ -54,19 +55,22 @@ get_aux <- function(table = NULL,
simplify = TRUE,
server = NULL,
assign_tb = FALSE,
force = FALSE) {
replace = FALSE) {

# Match args
api_version <- match.arg(api_version)
format <- match.arg(format)
run_cli <- run_cli()
# Build query string
u <- build_url(server, "aux", api_version = api_version)
req <- build_request(server = server,
api_version = api_version,
endpoint = "aux")

# Return response
# If no table is specified, returns list of available tables
if (is.null(table)) {
res <- httr::GET(u)
res <- req |>
httr2::req_perform()
tables <- parse_response(res, simplify = simplify)
cli::cli_text("Auxiliary tables available are")
cli::cli_ul(tables$tables)
Expand All @@ -80,12 +84,15 @@ get_aux <- function(table = NULL,
return(invisible(tables))
# If a table is specified, returns that table
} else {
args <- build_args(.table = table,
.version = version,
.ppp_version = ppp_version,
.release_version = release_version,
.format = format)
res <- httr::GET(u, query = args, httr::user_agent(pipr_user_agent))
req <- build_request(server = server,
api_version = api_version,
endpoint = "aux",
table = table,
version = version,
release_version = release_version,
format = format)

res <- httr2::req_perform(req)
rt <- parse_response(res, simplify = simplify)
}

Expand All @@ -107,7 +114,7 @@ get_aux <- function(table = NULL,

srt <- set_aux(table = tb_name,
value = rt,
force = force)
replace = replace)

if (isTRUE(srt)) {

Expand Down
Loading
Loading