A simple Kotlin/Java library to retrieve jokes from Sv443's JokeAPI. https://github.com/ethauvin/jokeapi
Find a file
2023-06-29 11:35:43 -07:00
.circleci No JDK 19 on CircleCI, switching to 17 2022-09-26 16:59:02 -07:00
.github/workflows Converted utility classes to objects 2023-02-03 00:09:32 -08:00
.idea Updated dependencies 2023-06-29 11:35:43 -07:00
gradle/wrapper Updated dependencies 2023-06-29 11:35:43 -07:00
src Switched to UrlEncoder 2023-02-23 12:17:21 -08:00
.editorconfig Initial commit. 2022-09-21 02:50:11 -07:00
.gitattributes Initial commit. 2022-09-21 02:50:11 -07:00
.gitignore Initial commit. 2022-09-21 02:50:11 -07:00
build.gradle.kts Updated dependencies 2023-06-29 11:35:43 -07:00
detekt-baseline.xml Upgraded to Kotlin 1.8.10 2023-02-03 01:02:08 -08:00
gradlew Updated dependencies 2023-06-29 11:35:43 -07:00
gradlew.bat Updated dependencies 2023-06-29 11:35:43 -07:00
LICENSE.txt Updated copyright 2023-01-28 21:39:47 -08:00
pom.xml Switched to UrlEncoder 2023-02-23 12:17:21 -08:00
README.md Minor cleanup 2023-02-18 00:59:26 -08:00
settings.gradle.kts Initial commit. 2022-09-21 02:50:11 -07:00

License (3-Clause BSD) Kotlin Nexus Snapshot Release Maven Central

Quality Gate Status GitHub CI CircleCI

JokeAPI for Kotlin, Java and Android

A simple library to retrieve jokes from Sv443's JokeAPI.

Examples (TL;DR)

import net.thauvin.erik.jokeapi.getJoke

val joke = getJoke()
val safe = getJoke(safe = true)
val pun = getJoke(categories = setOf(Category.PUN))

The parameters match the joke endpoint.

A Joke class instance is returned, matching the response:

data class Joke(
    val category: Category,
    val type: Type,
    val joke: List<String>,
    val flags: Set<Flag>,
    val id: Int,
    val safe: Boolean,
    val lang: Language
)

To retrieve multiple jokes:

val frenchJokes = getJokes(amount = 2, type = Type.TWOPART, lang = Language.FR)
frenchJokes.forEach {
    println(it.joke.joinToString("\n"))
    println("-".repeat(46))
}

If an error occurs, a JokeException is thrown, matching the JokeAPI errors:

class JokeException(
    val internalError: Boolean,
    val code: Int,
    message: String,
    val causedBy: List<String>,
    val additionalInfo: String,
    val timestamp: Long,
    cause: Throwable? = null
) : Exception(message, cause)

If an HTTP error occurs an HttpErrorException is thrown, with its message and cause matching the JokeAPI status codes:

class HttpErrorException(
    val statusCode: Int,
    message: String,
    cause: Throwable? = null
) : IOException(message, cause)

Java

To make it easier to use the library with Java, a configuration builder is available:

var config = new JokeConfig.Builder()
        .type(Type.SINGLE)
        .safe(true)
        .build();
var joke = JokeApi.getJoke(config);
joke.getJoke().forEach(System.out::println);

Gradle, Maven, etc.

To use with Gradle, include the following dependency in your build file:

repositories {
    mavenCentral()
    maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots") } // only needed for SNAPSHOT
}

dependencies {
    implementation("net.thauvin.erik:jokeapi:0.9-SNAPSHOT")
}

Instructions for using with Maven, Ivy, etc. can be found on Maven Central.

Raw Jokes

You can also retrieve one or more raw (unprocessed) jokes in all supported formats.

For example for YAML:

var joke = getRawJokes(format = Format.YAML, idRange = IdRange(22))
println(joke)
error: false
category: "Programming"
type: "single"
joke: "If Bill Gates had a dime for every time Windows crashed ... Oh wait, he does."
flags:
  nsfw: false
  religious: false
  political: false
  racist: false
  sexist: false
  explicit: false
id: 22
safe: true
lang: "en"

Extending

A generic apiCall() function is available to access other JokeAPI endpoints.

For example to retrieve the French language code:

val lang = JokeApi.apiCall(
    endPoint = "langcode",
    path = "french",
    params = mapOf(Parameter.FORMAT to Format.YAML.value)
)
println(lang)
error: false
code: "fr"