K Johann- Assignment 6 Learning a New API

Author

Kaitlyn Johann

1. Explanation of Service & Usage Cases - Cat API

I chose to use the Cat API for this assignment. This service provides people with a quick, convenient way to look at various breeds of cats. Among the breed names, you will find the origin, a brief description, images of the breed, and more. This information is all linked to a Wikipedia website for further inspection.

2a. Setting up the API on the Website

First click the associated website link. Once on the Cat API website, scroll down to the very bottom of the website. You should see “The Cat API”. Click “Get your API key” shown underneath this. It will redirect you to further up the page.

Next, click “Get free access” underneath the free option on the left hand side. Put in your email information and submit the request for the API key. You should receive an email in the following few minutes with your personal key.

2b. Setting up the API in R

Once you have your personal API key from the Cat API, start setting up your code to create the actual API.


library(tidyverse) # All the tidy things
library(jsonlite)  # Converting json data into data frames
library(magrittr)  # Extracting items from list objects using piping grammar
library(httr)      # Interacting with HTTP verbs
## You will need an API key to interact with the Cat API.
# Define your API key query parameter. 

api_key <- "api_key=live_Zc4nDaX19B3BymmGH0RlwHimMNiqHX6AbFQ3GILKiZn3HITKy13eK8C6ClwJauZ1" 
# Define the base URL (endpoint) path for the cat API. 
cat_api_GET_url <- 
  function(api_key, limit, page, order, has_breeds, breed_ids, category_ids, sub_id) {
  # Inside the curly brackets we perform all the operations we want the function
  # to perform when it is called. 
  # Each of the above input arguments will be provided to the function
  # as objects of the same name.
    
  # Define the endpoint
    cat_endpoint <- "https://api.thecatapi.com/v1/images/search"
    
  # Define your API key query parameter
    api_key <- paste("?api_key=",api_key, sep = "")
    
  # Apply the filtering criteria
    limit <- paste("&limit=",limit, sep = "")
    
    page <- paste("&page=",page, sep = "")
    
    order <- paste("&order=",order, sep = "")

    has_breeds <- paste("&has_breeds=",has_breeds, sep = "")    
    
    breed_ids <- paste("&breed_ids=",breed_ids, sep = "")   
    
    category_ids <- paste("&category_ids=",category_ids, sep = "")
    
    sub_id <- paste("&sub_id=",sub_id, sep = "")
    
  # Creating your final API call URL #
    cat_api_GET <- 
      paste(cat_endpoint,api_key,limit,page,order,has_breeds,breed_ids,category_ids,sub_id,sep = "")
    
  # The return() function is used to identify what the function should output
  # when it is evaluated.
    return(cat_api_GET)
  }

3. Demonstration, Call to the API


final_cat_url <-
  cat_api_GET_url(api_key = "live_Zc4nDaX19B3BymmGH0RlwHimMNiqHX6AbFQ3GILKiZn3HITKy13eK8C6ClwJauZ1",
                  limit = 80,
                  page = 0,
                  order = "RAND", 
                  has_breeds = 1,
                  breed_ids = "",
                  category_ids = "",
                  sub_id = "")
# Create an empty data frame to populate with the data.

cat_df <- data.frame()
## Create a function that turns the URL arguments directly into a data
## frame. Combine the earlier function work on the URL function with the tasks 
## following it creating the data frame:

cat_api_GET_df <-
  function(final_cat_urls){
    # Use the URL to retrieve the JSON data and create the data frame
    cat_df <- 
      final_cat_urls %>% 
      GET(add_headers("x-api-key" = api_key)) %>% 
      content(as = "text",
              encoding = "UTF-8") %>% 
      fromJSON()
    # When finished, have the function return the scorecard data frame output.
    return(cat_df)
  }
## We can perform this task by looping our original function through some number
## of iterations equal to the number of instances required. At the end of each 
## iteration of the loop, the results could be combined with the previous call of ## results to form a single data frame. 

for (i in seq_along(final_cat_url)){
  cat_df <- 
    cat_api_GET_df(final_cat_url[i]) %>%
    bind_rows(cat_df)
 }