'How to handle a list of S:3 objects in a for loop when making API calls

I am trying to create a function which can return a data.frame consisting of data from various customers. This is done through an API call, but the challenge is to handle the data types that the API call returns. I have done this before for one customer at a time with no problem. But, things turn difficult when I want to loop over multiple customers.

The far from finished code looks like:

vClients = c("1234", "4321")

clientTransactions <- function(vClients) {
  
  #Create a vector of URL strings: 
  urlClients = paste0("http:///api/clients/",vClients,"/transactions?")
  
  #Use the URLS in API calls to get data for each client

  for (i in 1:length(urlClients)) {
    
    getClients = httr::GET(urlClients[i], accept_json()) #Returns a list [10](S3:Response)
    getClients_content = httr::content(getClients[i], as = "text") #This step is wrong 
    getClients_json = jsonlite::fromJSON(getClients_content[i]) #This is where i want to end up with a data.frame of data for each client
    
  }
  
}

Once again, I know the function is far from complete. But, in order for me to get any further I need to understand how to loop over the "getClients" object. If we look for a single customer we have:

getClients = paste0("http:///api/clients/1234/transactions?")
getClients_content = httr::GET(getClients, accept_json())
getClients_content

Response [http://api/clients/1234/transactions?]
  Date: 2022-02-16 10:02
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 801 kB

But, when I try to do it for multiple web URLs in the urlClients it does not work. For example, if I write getClients[I] It just returns the web_url. Not the "response object".

So I guess my question is, how can I deal with this list of S:3 respones in my function and loop?

enter image description here]

Any ideas or other thoughts would be much appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source