NY Times API

Data 607, Assignment 7, Approach

Approach:

I started out by creating and account on https://developer.nytimes.com/. Getting an API key meant I needed to register an app. I named my app FavBooks. I wanted to get the basic requirements to start the homework out of the way, so after some refreshing on some of my skills, I was able to store the API Key in system environment and then load it here using request and req_url_query function.
Once I have an ok status, as can be seen from the following code, I’d now work on creating and cleaning the dataset.

library(httr2)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("DT")
readRenviron("Sys.env")

Sys.getenv("API_KEY") != ""
[1] TRUE
req<- request("https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json") |> req_url_query(`api-key` = Sys.getenv("API_KEY"))

resp <- req_perform(req)
books <- resp_body_json(resp)$results$books
data.frame(
  Rank = sapply(books, function(x) x$rank),
  Title = sapply(books, function(x) x$title),
  Author = sapply(books, function(x) x$author)
)
   Rank                            Title                     Author
1     1                    THRESHING DAY             Rebecca Yarros
2     2             MISS WOLCOTT'S GHOST               Louise Penny
3     3       A MARRIED LITTLE CHRISTMAS               Abby Jimenez
4     4                 THE FAMILY CROSS            James Patterson
5     5                   CHAIN REACTION Lee Child and Andrew Child
6     6              THE FRENCH ILLUSION               John Grisham
7     7                        THE WITCH            Freida McFadden
8     8             WITH HEARTS OF FLAME               Briar Boleyn
9     9                THE CALAMITY CLUB           Kathryn Stockett
10   10                          PARTITA         Barbara Kingsolver
11   11                     HOLLOW BONES               Jodi Picoult
12   12                      PECK & PECK              Bonnie Garmus
13   13 THE DUNGEON ANARCHIST'S COOKBOOK              Matt Dinniman
14   14       THE GATE OF THE FERAL GODS              Matt Dinniman
15   15                    MERCY'S CHILD         Richard Paul Evans