# Exercise 1: Get a single cat fact
# Make sure required packages are installed first
install.packages("httr")
install.packages("jsonlite")

# Load packages
library(httr)
library(jsonlite)

# Get a cat fact
response <- GET("https://catfact.ninja/fact")
raw_data <- content(response, as = "text")
cat_data <- fromJSON(raw_data)

print("Here's your cat fact:")
print(cat_data$fact)

# Exercise 2: Get a random "quote" (advice)
library(httr)
library(jsonlite)

# Request a random piece of advice
response <- GET("https://api.adviceslip.com/advice")

# Parse JSON directly
if (status_code(response) == 200) {
  advice_data <- content(response, as = "parsed", type = "application/json")
  
  # Print the advice
  print("Here's your advice:")
  print(advice_data$slip$advice)
} else {
  print("Sorry, could not retrieve a quote. Please try again later.")
}


# Exercise 3: Get multiple cat facts
response <- GET("https://catfact.ninja/facts?limit=3")
raw_data <- content(response, as = "text")
facts_data <- fromJSON(raw_data)
facts_list <- facts_data$data

print("Here are 3 cat facts:")
print(paste("Fact 1:", facts_list$fact[1]))
print(paste("Fact 2:", facts_list$fact[2]))
print(paste("Fact 3:", facts_list$fact[3]))
