Assignment 6: Learning a New API

Author

Noah Garczewski

Assignment 6 - Learning a New API

About the Joke API

The API I selected for this assignment is called JokeAPI. It is capable of providing jokes in various categories, languages, and types. Because I am a computer science student, I am particularly interested in the Programming category of jokes.

Potential uses for this API include: generating a new “daily joke” to display on a website, programmatically generated a comedy routine (not recommended), or incorporating jokes or Easter eggs in software development projects.

Using JokeAPI

JokeAPI offers a variety of GET endpoints and a single POST endpoint (for submitting joke suggestions). For this assignment, I will focus on the joke/ endpoint, which returns up to 10 jokes at a time.

Since this is a publicly available Quarto document, I will not execute API calls here. However, I’ve included all of the code I used in my API request so that it can be viewed.

Constructing my Request

The following code block demonstrates how I constructed my API request. I began with the base URL, then added the joke endpoint and my desired category of Programming. A variety of categories are available, including Any for users who don’t want any particular kind of joke. Because I am a computer scientist, I want programming-related jokes.

I then constructed a query string composed of several parameters. Available parameters include language, joke type, number of jokes, blacklist flags to exclude, strings to include within the joke itself, response formats, etc.

For my request, I included the following parameters: - Language: English - Type: Single (one-liner) - Amount: 10 jokes - Blacklist: all filters selected

After selecting my parameters, I pasted them to form a query string, then pasted the query string with the base URL, endpoint, and category to form my complete API request URL.

base_url <- "https://v2.jokeapi.dev"

endpoint <- "/joke"
category <- "/Programming"

language <- "?lang=en"
type <- "type=single"
amount <- "amount=10"
blacklist <- "blacklistFlags=nsfw,religious,political,racist,sexist,explicit"

query_string <- paste(language, type, amount, blacklist, sep = "&")

url <- paste(base_url, endpoint, category, query_string, sep = "")

Calling JokeAPI to Retrieve a Data Frame

Once I created my URL, I made a GET request with it and piped the result into the content() and fromJSON() functions to format the resulting output appropriately.

Once I have the data frame in JSON format, I select the desired series, in this case jokes and the desired field from that series, in this case joke, which contains the joke itself. Finally, I save the jokes in a data frame called joke_df.

joke_df <-
  url %>%
  GET() %>% 
  content(as = "text",
          encoding = "UTF-8") %>%
  fromJSON() %>%
  use_series(jokes) %>%
  select(joke)

The Resulting Data

Since this is a publicly available Quarto document, I will not demonstrate an API call here. Instead, I saved the resulting data frame as a CSV, which I then (invisibly) imported into a jokes data frame at the beginning of this document.

The code block below is a for loop which neatly prints the contents of jokes with a blank line between each joke for separation.

for (j in jokes$joke) {
  writeLines(j)
  cat("\n")
}
Have a great weekend!
I hope your code behaves the same on Monday as it did on Friday.

A programmer puts two glasses on his bedside table before going to sleep.
A full one, in case he gets thirsty, and an empty one, in case he doesn't.

If Bill Gates had a dime for every time Windows crashed ... Oh wait, he does.

ASCII silly question, get a silly ANSI.

"Knock, knock."
"Who's there?"

[very long pause]

"Java."

A byte walks into a bar looking miserable.
The bartender asks it: "What's wrong buddy?"
"Parity error." it replies. 
"Ah that makes sense, I thought you looked a bit off."

Eight bytes walk into a bar.
The bartender asks, "Can I get you anything?"
"Yeah," reply the bytes.
"Make us a double."

I've got a really good UDP joke to tell you but I don’t know if you'll get it.

Judge: "I sentence you to the maximum punishment..."
Me (thinking): "Please be death, please be death..."
Judge: "Learn Java!"
Me: "Damn."

A SQL statement walks into a bar and sees two tables.
It approaches, and asks "may I join you?"