Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

49 lines (36 sloc) 2.02 kB
name layout title date author sourceslug tags
api-token
post
Waiting for an API request to complete
2013-01-26
Scott Chamberlain
_drafts/2013-01-26-api-token.Rmd
R
API
ropensci

Dealing with API tokens in R

In my previous post I showed an example of calling the Phylotastic taxonomic name resolution API Taxosaurus here. When you query their API they give you a token which you use later to retrieve the result (see examples on their page above). However, you don't know when the query will be done, so how do we know when to send the query to rerieve the data?


As the time this takes depends on how big the query is and other things, we don't know when we can get the result. I struggled with this for a bit, but then settled on using a while loop.


So what does this look like? Basically we just keep sending the request for data until we get it.

{% highlight r %} iter <- 0 # make an iterator so each time we call output <- list() # make an empty list to put data into timeout <- "wait" while (timeout == "wait") { iter <- iter + 1 # increase the iterator each time temp <- fromJSON(getURL(retrieve)) # send the request and parse the JSON if (grepl("is still being processed", temp["message"]) == TRUE) { timeout <- "wait" } else { output[[iter]] <- temp # put result from query in the list timeout <- "done" # we got the result so timeout is now done, making the while loop stop } } {% endhighlight %}


Get the .Rmd file used to create this post at my github account - or .md file.

Written in Markdown, with help from knitr.

Jump to Line
Something went wrong with that request. Please try again.