Skip to content

Commit

Permalink
Support internal API instance for IPUMS developers (#59)
Browse files Browse the repository at this point in the history
Add handling for internal api instance

Co-authored-by: Derek Burk <burkx031@umn.edu>
  • Loading branch information
robe2037 and dtburk authored Oct 12, 2023
1 parent 037bdec commit 02458c7
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions R/api_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ parse_response_error <- function(res) {
# Helper to handle errors and warnings for API requests.
# Called in ipums_api_request()
validate_api_request <- function(res, call = caller_env()) {
is_downloads_request <- fostr_detect(res$url, "downloads/")
is_downloads_request <- fostr_detect(res$url, "downloads")
is_extract_request <- !is_downloads_request &&
fostr_detect(res$url, "extracts/")

Expand Down Expand Up @@ -887,7 +887,7 @@ add_user_auth_header <- function(api_key) {
api_base_url <- function() {
api_instance <- active_api_instance()

if (api_instance == "") {
if (api_instance == "" || api_instance == "internal") {
url <- "https://api.ipums.org/"
} else {
url <- paste0("https://", api_instance, ".api.ipums.org/")
Expand Down Expand Up @@ -946,7 +946,14 @@ extract_request_path <- function(number = NULL) {
if (!rlang::is_null(number)) {
number <- format(number, scientific = FALSE)
}
paste0("extracts/", number)

if (active_api_instance() == "internal") {
path <- paste0("internal-extracts/", number)
} else {
path <- paste0("extracts/", number)
}

path
}

#' Helper to construct URL paths for API metadata endpoints
Expand All @@ -971,7 +978,17 @@ metadata_request_path <- function(collection, ...) {
path_args <- purrr::compact(rlang::list2(...))
path_fields <- names(path_args)

path_args <- c("metadata", collection, rbind(path_fields, unlist(path_args)))
if (active_api_instance() == "internal") {
metadata_path <- "internal-metadata"
} else {
metadata_path <- "metadata"
}

path_args <- c(
metadata_path,
collection,
rbind(path_fields, unlist(path_args))
)

# Avoids extra `/` for unnamed args in `path`
path_args <- path_args[which(path_args != "")]
Expand Down

0 comments on commit 02458c7

Please sign in to comment.