Waiting for an API request to complete

[This article was first published on Recology - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

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

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

Written in Markdown, with help from knitr.

To leave a comment for the author, please follow the link and comment on their blog: Recology - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)