用于从 R 中的多个 URL(带身份验证)检索数据的嵌套函数

Nested function to retrieve data from multiple URLs (with authentication) in R

我下面的代码旨在通过 API 端点通过身份验证检索数据(及其元数据),并将所有元数据 return 检索到数据框中。我想创建一个嵌套函数来为另一个具有相同身份验证的 API 端点重复相同的过程,并将第二个端点的结果附加到第一个端点,放入单个数据帧中(它们都具有相同的数据结构和 headers).我不知道在这个过程中应该放在哪里link_to_endpoint2,如何嵌套,如何追加结果等等

get_data <- function(uid, credentials, root_url) {

  cookie <- credentials$cookie
  token <- credentials$token

  start_time <- Sys.time()
  print (start_time)
  url <- paste0(root_url, 'link_to_endpoint1', uid)
  resp <- httr::GET(url,
                    httr::add_headers(.headers = c(`Content-Type` = "application/json", 
                                                   Cookie = cookie, `X-CSRF-Token` = token)),
                    body = body, 
                    encode = "json")

  httr::warn_for_status(resp)
  resources <- httr::content(resp)
  access_check <- resources$result$error
  assertthat::assert_that(is.null(access_check),
                          msg = 'Access denied')

  resources <- resources$result[[1]]$resources
  res_ids <- purrr::map_chr(resources, 'id')
  res_urls <- purrr::map_chr(resources, 'url')
  res_desc <- purrr::map_chr(resources, 'description')
  res_format <- purrr::map_chr(resources, 'format')
  res_rclass <- purrr::map_chr(resources, 'data_classification_of_file')
  res_rtype <- purrr::map_chr(resources, 'mimetype')
  res_rname <- purrr::map_chr(resources, 'name')
  out <- data.frame(res_ids,
                    res_urls,
                    res_desc,
                    res_format,
                    res_rclass,
                    res_rtype,
                    res_rname,
                    stringsAsFactors = FALSE)
  out$id <- uid
  end_time <- Sys.time()
  print (end_time)
  process_time <- (end_time - start_time)
  print(process_time)
  return(out)
}

您必须测试此解决方案,但您可以尝试

将这些行更改为

# Old line
New line

# get_data <- function(uid, credentials, root_url) {
get_data <- function(uid, credentials, root_url, link) {

# url <- paste0(root_url, 'link_to_endpoint1', uid)
url <- paste0(root_url, link, uid)

然后你可以调用这个函数(假设你已经加载了purrr

links <- c('link_to_endpoint1', 'link_to_endpoint2')
desired <- map_df(links, ~get_data(uid, credentials, root_url, .x))