Joke API Exercise
This exercise can be done little by little. Start by getting one joke from one API and then add more APIs. You will have to create DTOs for each API and then a mega DTO that contains all the data.
- Create a program to get data from 7 differenct web services (APIs) at the same time.
- Get the returned value from the web service and save it in DTOs.
- Collect all the DTO data in a mega DTO and print it out in a nice format.
- Examples of APIs to use:
- icanhazdadjoke (this needs a header
Accept: application/json
) - chucknorris
- kanye.rest
- whatdoestrumpthink
- jokeapi
- geek-jokes
- official-joke-api
You might want to use the following code to get the ball rolling:
```java String[] urls = new String[]{ "https://icanhazdadjoke.com/", "https://api.chucknorris.io/jokes/random", "https://api.kanye.rest", "https://api.whatdoestrumpthink.com/api/v1/quotes/random", "https://v2.jokeapi.dev/joke/Coding?type=single", "https://geek-jokes.sameerkumar.website/api?format=json", "https://official-joke-api.appspot.com/random_joke" }; ```
- icanhazdadjoke (this needs a header
-
First you fetch and convert each joke to a DTO one at a time. Create a timer to mesure the time it takes to fetch all jokes.
- Then you fetch all jokes at the same time (by using threads) and measure the time it takes to fetch all jokes. Hopefully this speeds things up. You need to work with the
ExecutorService
andFuture
classes.
Json example from Icanhazdadjoke
{
"id": "Xny5MZvHQCd",
"joke": "Can I watch the TV? Dad: Yes, but don’t turn it on.",
"status": 200
}
Json example from Chuck Norris
{
"categories": [],
"created_at": "2020-01-05 13:42:19.576875",
"icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
"id": "1",
"updated_at": "2020-01-05 13:42:19.576875",
"url": "https://api.chucknorris.io/jokes/1",
"value": "Chuck Norris can divide by zero."
}
Json example from Kanye Rest
{
"quote": "I feel like I'm too busy writing history to read it."
}
Json example from What Does Trump Think
{
"message": "The beauty of me is that I'm very rich.",
"nlp_attributes": {
"quote_structure": [
[
"Determiner",
"Noun",
"Preposition",
"Pronoun",
"Copula",
"Determiner",
"Noun",
"Adverb",
"Adjective"
]
]
}
}
Json example from Joke API
{
"category": "Programming",
"type": "single",
"joke": "Why do programmers prefer dark mode? Because light attracts bugs.",
"flags": {
"nsfw": false,
"religious": false,
"political": false,
"racist": false,
"sexist": false
},
"id": 1,
"error": false
}
Json example from Geek Jokes
{
"joke": "What does a subatomic duck say? Quark."
}
Json example from Official Joke API
{
"type": "general",
"setup": "What was a more important invention than the first telephone?",
"punchline": "The second one.",
"id": 262
}