hrbrmstr / webhose

:hammer: Tools to Work with the 'webhose.io' 'API' in R
12 stars 3 forks source link

Analizing `fetch_post()` #3

Open OmarGonD opened 6 years ago

OmarGonD commented 6 years ago

Hi @hrbrmstr, first of all thank you for trying to help others to learn how to work with APIs and R Packages. I've always wanted to understand how to connect to an API, so I think this is my chance.

Ps. thanks @ottlngr for complementing this work.

So, after analizing the code for I have 2 questions:

1.- I've looked around the code for fetch_post() but cannot understand how do yo make the http conection. I was hopping to see some htt::GET() but there isn't one.

There are calls to the httr library function in the filter_post(). I was wondering, why there isn't any in the fetch_post()?

2.- Could you explain to me the use of 'quiet'? (!interactive())

In the simpliest terms, a NOT interactive session is when R is doing some operations? (like a for loop, and we just need to wait? -so, when we see the red bottom?)

if (res[["moreResultsAvailable"]] > 0) {
      if (!quiet) message("Fetching next 100 records...")
      i <- i + 1
      from <- from + 100
    } else {
      break
    }
hrbrmstr commented 6 years ago

(I'll be able to comment better in the AM. Great questions that I can def help answer)

ottlngr commented 6 years ago

Hey @OmarGonD , let me answer your questions.

I've looked around the code for fetch_post() but cannot understand how do yo make the http conection. I was hopping to see some htt::GET() but there isn't one. There are calls to the httr library function in the filter_post(). I was wondering, why there isn't any in the fetch_post()?

You're right, fetch_posts() does not make any HTTP calls itself. What it does is to forward the query specified in fetch_posts() to filter_posts() - which does a HTTP call using httr::GET() as you already noticed. You may ask why this task is split into two functions.

Probably @hrbrmstr chose that option to have two separate functions, each designed for only one task and optimized for one task. filter_posts() receives your query, makes a GET requests and returns the parsed result. Not more but not less.

Now, fetch_posts() calls filter_posts(), receives the returned result and ensures you get all data returned by the query by recalling filter_posts() until the moreResultsAvailable flag is 0.

Does that answer your question?

Could you explain to me the use of 'quiet'? (!interactive())

See this page for understanding what interactive() does: https://stat.ethz.ch/R-manual/R-devel/library/base/html/interactive.html

So if R is running interactively, interactive() returns TRUE. Setting quiet = !interactive() sets quiet = FALSE (because of the negation operator !) to ensure messages are printed to a user sitting in front of an interactive R session.

Does that help?