0

I want to call an Api that gives me all my shop orders. I have a total of 86.000 orders where the ID of the first order is 2 and the ID of the most recent order is 250.000. Orders obviously are not count consecutive, due to some reason i dont know yet, but doesnt matter.

I started a simple script with a for loop where the ID gets updated in every loop, like this:

library(jsonlite)
library(httr)

user = "my_name"
token = "xyz"

y = 0
urls = rep("api.com/orders/", 250000)

for(i in urls){

y = y + 1

url = paste0(i, y)

a = httr::GET(url, authenticate(user, token))
a_content = httr:content(a, as = "text", encoding = "UTF-8")
a_json = jsonlite::fromJSON(a_content, flatten = T)
...
}

Problem here is, whenever there is an ID with no order, the loop stops with "{\"success\":false,\"message\":\"Order by id 1 not found\"}" So i somehow have to expand the code with some if else statements, like 'if the id does not match an order proceed to the next order'. Also i want to write all orders into a new list. Any help apprichiated.

1 Answers1

0

Maybe tryCatch can solve the problem. This url_exists function posted by user @hrbrmstr is called in the loop below.
Without testing, I would do something along the lines of:

base_url <- "api.com/orders/"
last_order_number <- 250000

for(i in seq.int(last_order_number)){

  url = paste0(base_url, i)
  if(url_exists(url)){
    a <- httr::GET(url, authenticate(user, token))
    a_content <- httr:content(a, as = "text", encoding = "UTF-8")
    a_json <- jsonlite::fromJSON(a_content, flatten = T)
  }

}
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • The Problem with this approach is that ``url_exists()`` will give me ``FALSE`` for all my urls, since my urls need authentication in the first place to accept a request. –  Aug 19 '20 at 10:52
  • @lucaskr You can adapt the function to accept an authentication function. – Rui Barradas Aug 19 '20 at 12:46