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

httr2 #70

Merged
merged 14 commits into from
Dec 19, 2024
Merged
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
13 changes: 6 additions & 7 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 Down Expand Up @@ -41,7 +41,6 @@ Suggests:
rmarkdown,
markdown,
callr,
mockery,
ggplot2,
tidyr,
ggthemes,
Expand All @@ -53,17 +52,17 @@ Language: en-US
Imports:
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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

export(call_aux)
export(check_api)
export(delete_cache)
export(display_aux)
export(get_aux)
export(get_cache_info)
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
45 changes: 45 additions & 0 deletions R/build_request.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' build_request
#'
#' @param server character: Server. For WB internal use only
#' @param api_version character: API version
#' @param endpoint character: PIP API endpoint
#' @param ...
#'
#' @return httr2 request
#'
build_request <- 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)
}
}
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
22 changes: 14 additions & 8 deletions R/get_aux.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ get_aux <- function(table = NULL,
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 +83,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 Down
64 changes: 34 additions & 30 deletions R/get_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ get_stats <- function(country = "all",
ppp_version = NULL,
release_version = NULL,
api_version = "v1",
format = c("rds", "json", "csv"),
format = c("arrow", "rds", "json", "csv"),
simplify = TRUE,
server = NULL) {
# Match args
Expand All @@ -95,23 +95,26 @@ get_stats <- function(country = "all",
}

# Build query string
args <- build_args(
.country = country,
.year = year,
.povline = povline,
.popshare = popshare,
.fill_gaps = fill_gaps,
.group_by = group_by,
.welfare_type = welfare_type,
.reporting_level = reporting_level,
.version = version,
.ppp_version = ppp_version,
.release_version = release_version,
.format = format
req <- build_request(
country = country,
year = year,
povline = povline,
popshare = popshare,
fill_gaps = fill_gaps,
group_by = group_by,
welfare_type = welfare_type,
reporting_level = reporting_level,
version = version,
ppp_version = ppp_version,
release_version = release_version,
format = format,
server = server,
api_version = api_version,
endpoint = endpoint
)
u <- build_url(server, endpoint, api_version)
# Send query
res <- httr::GET(u, query = args, httr::user_agent(pipr_user_agent))
# Perform request
res <- req |>
httr2::req_perform()

# Parse result
out <- parse_response(res, simplify)
Expand All @@ -136,20 +139,21 @@ get_wb <- function(year = "all",
format <- match.arg(format)

# Build query string
args <- build_args(
.country = "all",
.year = year,
.povline = povline,
.group_by = "wb",
.version = version,
.ppp_version = ppp_version,
.release_version = release_version,
.format = format
req <- build_request(
year = year,
povline = povline,
group_by = "wb",
version = version,
ppp_version = ppp_version,
release_version = release_version,
format = format,
server = server,
api_version = api_version,
endpoint = "pip-grp"
)
u <- build_url(server, "pip-grp", api_version)

# Send query
res <- httr::GET(u, query = args, httr::user_agent(pipr_user_agent))
# Perform request
res <- req |>
httr2::req_perform()

# Parse result
out <- parse_response(res, simplify)
Expand Down
12 changes: 8 additions & 4 deletions R/other.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ check_api <- function(api_version = "v1", server = NULL) {
#' }
get_versions <- function(api_version = "v1", server = NULL, simplify = TRUE) {
check_internet()
u <- build_url(server, "versions", api_version)
res <- httr::GET(u, httr::user_agent(pipr_user_agent))
req <- build_request(server = server,
api_version = api_version,
endpoint = "versions")
res <- httr2::req_perform(req)
parse_response(res, simplify = simplify)
}

Expand All @@ -41,7 +43,9 @@ get_versions <- function(api_version = "v1", server = NULL, simplify = TRUE) {
#' }
get_pip_info <- function(api_version = "v1", server = NULL) {
check_internet()
u <- build_url(server, "pip-info", api_version)
res <- httr::GET(u, httr::user_agent(pipr_user_agent))
req <- build_request(server = server,
api_version = api_version,
endpoint = "pip-info")
res <- httr2::req_perform(req)
parse_response(res, simplify = FALSE)$content
}
Loading