Reputation: 1036
I want to do almost exactly this question: Pulling data from an API response list
But the trick is, I'm going to have dozens of responses from this loop I asked about in another question.
I.e. I have this dataset:
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")
And using this purrr code, I have been able to successfully GET and PUT dozens of responses at a time:
library(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)
It works perfectly as intended and loops through my dataframe, plugs the "protocol_id" variable into the loop, 'GET's the data from that Protocol, changes what I want to, and then 'PUT's the data back in. It does this perfectly. And honestly so far, every single attempt I've tried has worked great and I'll get several "204 No Content" responses which means it worked great.
But I'm sure that every once and a while it wont work perfectly for certain protocols and I might get a response like "400 Bad Request" or something. And I'd love to be able to keep track of that.
Ideally something like this:
Upvotes: 1
Views: 211
Reputation: 5958
We can use:
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, status_code(httpResponse_get))
put_codes <<- append(put_codes, status_code(httpResponse_put))
}
purrr::walk(df$PROTOCOL_ID, UpdateAccountNumbers)
allresults <- tibble('protocolid'=unlist(protocolid),'protocolnb'=unlist(protocolnb),'library_names'=unlist(library_names), 'get_codes'=unlist(get_codes), 'put_codes'=unlist(put_codes) )
Since the question is not reproducible, let me know if there are remaining errors.
Upvotes: 1