The library Load in

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(openxlsx)
library(dplyr)
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
## 
##     flatten
library(httr)
library(lares)
## Warning: package 'lares' was built under R version 4.1.3

The Prelims

Signing up for a NYT Developer account

Testing it with postman

I really like Postman as a simple API tester, it really easily allows for parameterisation and testing of APIs, not to mention great logging and debugging.

Here is my first test of the NYT API

NYT API TEST

A side note to keying

test <- get_creds("service1")
key <- test$key

This is just a simple keying use for R

JSON…Oh My!

As we can see from the test string above, we successfully get a reasonably well formed json response. It does a great job. From there, we can test the same string in R and attempt a basic read into a dataframe. First things first, lets handle keying

Json_address = sprintf("https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=%s&api-key=%s", "godfather", key)
json_raw <- fromJSON(Json_address)
json_table <- as.data.frame(json_raw,)
json_table

Let’s Parameterize our request string

search_term = "godfather"
url = sprintf("https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=%s&api-key=%s", search_term, key)

Let’s modularize our call

Step 1-Let’s take our search term and key and make them variables, then try and pull the same table.

search_term = "godfather"
api_url = sprintf("https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=%s&api-key=%s", search_term, key)
json_raw <- fromJSON(api_url)
json_table <- as.data.frame(json_raw,)
json_table

Step 2-Making a function

search_term = "godfather"
getNytData <- function(search_term, key) {
api_url = sprintf("https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=%s&api-key=%s", search_term, key)
api_url
json_raw <- fromJSON(api_url)
json_table <- as.data.frame(json_raw,)
return(json_table)
}
getNytData(search_term, key)

The Function works!

So at this point, we have a function that works, a key, and best of all its all secure! At this point we are complete but there are still a few things I want to try!

The things to play with

  1. can you import an image from a link in R Studio:

NYT API TEST

YES!

  1. How I would make this better Ideally I’d love to spin up a container, keep the secret as an env, and then launch it in that container, but this project doesn’t require that degree of complexity!

Conclusion

This project is finished as I connected to an API & loaded it into a dataframe!