Assignment – Web APIs

response<-GET("https://api.nytimes.com/svc/archive/v1/2018/3.json", query = list(api_key = key, order = "by-title", offset = 20))
response[2]
## $status_code
## [1] 200
getArchive<-function(year, month, rows) {
  stopifnot(!any(is.na(c(year, month))))
  stopifnot(year >= 1851)
  stopifnot(month >= 1 & month <= 12)
  stopifnot(rows >= 1)

  url<-sprintf("https://api.nytimes.com/svc/archive/v1/%d/%d.json?api-key=%s", year, month, key)
  response_df<-fromJSON(url,flatten = TRUE)$response$docs
  
  resp = response_df %>%
    select(Headline = headline.main) %>%
    mutate(Headline = stri_trans_totitle(Headline))
  
  stopifnot(is.data.frame(resp))
  head(resp, rows)
}

getArchive(2015, 4, 24)
##                                                                             Headline
## 1                                      Backlash Against Indiana Religious Exceptions
## 2                                                              Clip: ‘Felix & Meira’
## 3           &#8216;Broadchurch&#8217; Season 2 Episode 5 Recap: The Center Collapses
## 4                      &#8216;The Americans&#8217; Recap: The Moment Finally Arrives
## 5                             Denim Jackets That Are Far From Old-Fashioned And More
## 6                What Is Morgellons? Singer Joni Mitchell&#8217;S Disputed Diagnosis
## 7                                                                Another Arrangement
## 8                                              Love And Loss On The Road To Arkansas
## 9                          Jeb Bush Seems To Shift Tone In His Praise Of Indiana Law
## 10                                         The Indictment Of Senator Robert Menendez
## 11                                           Pictures Of The Day: Iraq And Elsewhere
## 12                                                         Filing On Hsbc Compliance
## 13            Verbatim: Menendez Repeats, &#8216;I&#8217;M Not Going Anywhere&#8217;
## 14           Cruz, In Iowa, Commends Indiana For &#8216;Doing The Right Thing&#8217;
## 15                             Review: In ‘Furious 7,’ A Franchise Continues To Roar
## 16                                           California Governor Imposes Water Order
## 17         In A Nod To Past And Future, Clinton Pushes Child Development In Brooklyn
## 18                              Liquidity Traps, Local And Global (Somewhat Wonkish)
## 19                                            Officials On Iran Nuclear Negotiations
## 20                                                             Your Evening Briefing
## 21                                                A Friendly Pillow Fight, Worldwide
## 22                             &#8216;Scrapbooking&#8217; Our Children Into Facebook
## 23 A New Exhibition Celebrates Le Corbusier, Through The Eyes Of Those He Influenced
## 24                                             Verbatim: Booker Stands With Menendez