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
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
test <- get_creds("service1")
key <- test$key
This is just a simple keying use for R
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
search_term = "godfather"
url = sprintf("https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=%s&api-key=%s", search_term, key)
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
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)
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!
NYT API TEST
YES!
This project is finished as I connected to an API & loaded it into a dataframe!