Home > Software design >  Pull data from an API response from a loop
Pull data from an API response from a loop

Time:08-16

I want to do almost exactly this question: enter image description here

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:

enter image description here

CodePudding user response:

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.

  • Related