Assignment NY Times API Codebase

library(httr)
library(jsonlite)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter()  masks stats::filter()
✖ purrr::flatten() masks jsonlite::flatten()
✖ dplyr::lag()     masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
api_key <- "kx8nMjwTxiKIKvFo0J0wSo7jaaGIflr55t9MqtfGg6WdcMrV"

base_url <- "https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json"

response <- GET(url = base_url, query = list("api-key" = api_key))

status_code(response) == 200
[1] TRUE
content_raw <- content(response, as = "text", encoding = "UTF-8")
data_json <- fromJSON(content_raw)

books_df <- as_tibble(data_json$results$books)

top_5_fantasy <- books_df %>%
  select(rank, title, author, description, weeks_on_list, amazon_product_url) %>%
  head(5)

print(top_5_fantasy)
# A tibble: 5 × 6
   rank title             author    description weeks_on_list amazon_product_url
  <int> <chr>             <chr>     <chr>               <int> <chr>             
1     1 JUDGE STONE       Viola Da… Judge Mary…             2 https://www.amazo…
2     2 THE CORRESPONDENT Virginia… Letters fr…            21 https://www.amazo…
3     3 BLOODLUST         Sandra B… The mutual…             1 https://www.amazo…
4     4 MY HUSBAND'S WIFE Alice Fe… In an old …             8 https://www.amazo…
5     5 INNAMORATA        Ava Reid  Passion an…             1 https://www.amazo…