【问题标题】:Pull data from an API response from a loop从循环的 API 响应中提取数据
【发布时间】:2022-08-16 02:11:57
【问题描述】:

我几乎完全想做这个问题:Pulling data from an API response list

但诀窍是,我会从我问about in another question 的这个循环中得到几十个回复。

IE。我有这个数据集:

df<-structure(list(PROTOCOL_ID = c(1, 22, 543, 421, 55, 6), PROTOCOL_NO = c(\"CTSU-E1234\", 
\"BRUOG-j-1234\", \"tp-P-bob61\", \"PFIZER-T\", \"Jimbo\", 
\"INCONGRUENCE\"), LIBRARY = c(\"Non-Oncology\", \"Oncology\", \"Non-Oncology\", 
\"Oncology\", \"Oncology\", \"Non-Oncology\")), row.names = c(NA, 6L), class = \"data.frame\")

并且使用这个 purrr 代码,我已经能够一次成功地 GET 和 PUT 几十个响应:

图书馆(httr)

token<- \"12345\"
base <- \"https://mywebsite.website.com\"
endpoint <- \"/website-api/rest/protocolManagementDetails/\"

UpdateAccountNumbers <- function(protocol){
  
  call2 <- paste(base,endpoint, protocol, sep=\"\") 
  
  call2 <- paste(base,endpoint, protocol, sep=\"\")  
  
  httpResponse <- GET(call2, add_headers(authorization = token))
  results <- fromJSON(content(httpResponse, \"text\"))
  
  results$hospitalAccountNo <- results$internalAccountNo
  
  call2 <- paste(base,endpoint, protocol, sep=\"\") 
  
  httpResponse <- PUT(
    call2, 
    add_headers(authorization = token), 
    body=results, encode = \"json\", 
    verbose()
  )
}

purrr::walk(df$PROTOCOL_ID, UpdateAccountNumbers)

它按预期完美运行并循环通过我的数据框,将“protocol_id”变量插入循环,“GET”是来自该协议的数据,改变我想要的,然后“PUT”是数据返回。它完美地做到了这一点。老实说,到目前为止,我尝试过的每一次尝试都效果很好,我会收到几个“204 No Content”的回复,这意味着效果很好。

但我敢肯定,每隔一段时间它就不能完美地适用于某些协议,并且我可能会得到类似“400 Bad Request”之类的响应。我希望能够跟踪这一点。

理想情况下是这样的:

  • 乔,会使用tryCatch 来跟踪不同的行为(如 400 错误)来完成这项工作吗? stackoverflow.com/a/12195574/8400969
  • @MichaelRoswell 嗯,看起来确实如此。我现在无法想象如何将其压缩到我的代码中,但让我做一些思考/玩
  • 快速浏览一下,用tryCatch 包裹您的GETPUT 行,以及一些处理不同HTTP 错误消息的条件内容可能会这样做......
  • status_code(response) 将为您提供状态
  • 当然,会看看

标签: r rest purrr


【解决方案1】:

我们可以用:

protocolid <- protocolnb <- library_names <- get_codes <- put_codes <- list()

UpdateAccountNumbers <- function(protocol){
  call2 <- paste(base,endpoint, protocol, sep="") 
  call2 <- paste(base,endpoint, protocol, sep="")  
  httpResponse_get <- GET(call2, add_headers(authorization = token))
  results <- fromJSON(content(httpResponse_get, "text"))
  
  results$hospitalAccountNo <- results$internalAccountNo
  
  call2 <- paste(base,endpoint, protocol, sep="") 
  
  httpResponse_put <- PUT(
    call2, 
    add_headers(authorization = token), 
    body=results, encode = "json", 
    verbose()
  )
  
  # save stats 
  protocolid <- append(protocolid, protocol)
  protocolnb <- append(protocolnb, df$PROTOCOL_NO[match(protocol, df$PROTOCOL_ID)])
  library_names <- append(library_names, df$LIBRARY[match(protocol, df$PROTOCOL_ID)])
  get_codes <- append(get_codes, httpResponse_get)
  put_codes <- append(put_codes, httpResponse_put)
}
purrr::walk(df$PROTOCOL_ID, UpdateAccountNumbers)

allresults <- tibble('protocolid'=unlist(protocolid),'protocolnb'=unlist(protocolnb),'library_names'=unlist(library_names), 'get_codes'=unlist(httpResponse_get), 'put_codes'=unlist(httpResponse_put) )

由于该问题不可重现,如果还有其他错误,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2023-02-25
    • 1970-01-01
    相关资源
    最近更新 更多