Added KDoc
This commit is contained in:
parent
3e3c3db20f
commit
bfcbafc05a
12 changed files with 87 additions and 6 deletions
|
@ -52,6 +52,9 @@ import java.util.logging.Level
|
||||||
import java.util.logging.Logger
|
import java.util.logging.Logger
|
||||||
import java.util.stream.Collectors
|
import java.util.stream.Collectors
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements the [JokeAPI](https://jokeapi.dev/).
|
||||||
|
*/
|
||||||
class JokeApi {
|
class JokeApi {
|
||||||
companion object {
|
companion object {
|
||||||
private const val API_URL = "https://v2.jokeapi.dev/"
|
private const val API_URL = "https://v2.jokeapi.dev/"
|
||||||
|
@ -60,6 +63,11 @@ class JokeApi {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val logger: Logger by lazy { Logger.getLogger(JokeApi::class.java.simpleName) }
|
val logger: Logger by lazy { Logger.getLogger(JokeApi::class.java.simpleName) }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use to make a direct API call.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#endpoints) for more details.
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
@Throws(HttpErrorException::class, IOException::class)
|
@Throws(HttpErrorException::class, IOException::class)
|
||||||
|
@ -93,6 +101,11 @@ class JokeApi {
|
||||||
return fetchUrl(urlBuilder.toString())
|
return fetchUrl(urlBuilder.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use to retrieve a joke.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#joke-endpoint) for more details.
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@Throws(HttpErrorException::class, IOException::class)
|
@Throws(HttpErrorException::class, IOException::class)
|
||||||
fun getRawJoke(
|
fun getRawJoke(
|
||||||
|
@ -170,6 +183,11 @@ class JokeApi {
|
||||||
return apiCall(JOKE_ENDPOINT, path, params)
|
return apiCall(JOKE_ENDPOINT, path, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use to retrieve a joke using a [configuration][JokeConfig].
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#joke-endpoint) for more details.
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@Throws(HttpErrorException::class, IOException::class)
|
@Throws(HttpErrorException::class, IOException::class)
|
||||||
fun getRawJoke(config: JokeConfig): String {
|
fun getRawJoke(config: JokeConfig): String {
|
||||||
|
@ -194,7 +212,7 @@ class JokeApi {
|
||||||
|
|
||||||
val connection = URL(url).openConnection() as HttpURLConnection
|
val connection = URL(url).openConnection() as HttpURLConnection
|
||||||
connection.setRequestProperty(
|
connection.setRequestProperty(
|
||||||
"User-Agent", "Mozilla/5.0 (Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0"
|
"User-Agent", "Mozilla/5.0 (Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
if (connection.responseCode in 200..399) {
|
if (connection.responseCode in 200..399) {
|
||||||
|
@ -265,6 +283,13 @@ class JokeApi {
|
||||||
return httpException
|
return httpException
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use to retrieve a [Joke].
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#joke-endpoint) for more details.
|
||||||
|
*
|
||||||
|
* @param splitNewLine Split newline within joke.
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@Throws(JokeException::class, HttpErrorException::class, IOException::class)
|
@Throws(JokeException::class, HttpErrorException::class, IOException::class)
|
||||||
fun getJoke(
|
fun getJoke(
|
||||||
|
@ -327,6 +352,11 @@ class JokeApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use to retrieve a [Joke] using a [configuration][JokeConfig].
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#joke-endpoint) for more details.
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@Throws(JokeException::class, HttpErrorException::class, IOException::class)
|
@Throws(JokeException::class, HttpErrorException::class, IOException::class)
|
||||||
fun getJoke(config: JokeConfig): Joke {
|
fun getJoke(config: JokeConfig): Joke {
|
||||||
|
|
|
@ -39,6 +39,11 @@ import net.thauvin.erik.jokeapi.models.IdRange
|
||||||
import net.thauvin.erik.jokeapi.models.Language
|
import net.thauvin.erik.jokeapi.models.Language
|
||||||
import net.thauvin.erik.jokeapi.models.Type
|
import net.thauvin.erik.jokeapi.models.Type
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joke Configuration.
|
||||||
|
*
|
||||||
|
* Use the [Builder] to create a new configuration.
|
||||||
|
*/
|
||||||
class JokeConfig private constructor(
|
class JokeConfig private constructor(
|
||||||
val categories: Set<Category>,
|
val categories: Set<Category>,
|
||||||
val language: Language,
|
val language: Language,
|
||||||
|
@ -51,6 +56,13 @@ class JokeConfig private constructor(
|
||||||
val safe: Boolean,
|
val safe: Boolean,
|
||||||
val splitNewLine: Boolean,
|
val splitNewLine: Boolean,
|
||||||
) {
|
) {
|
||||||
|
/**
|
||||||
|
* Use to [build] a new configuration.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#joke-endpoint) for more details.
|
||||||
|
*
|
||||||
|
* @param splitNewLine Split newline within joke.
|
||||||
|
*/
|
||||||
data class Builder(
|
data class Builder(
|
||||||
var categories: Set<Category> = setOf(Category.ANY),
|
var categories: Set<Category> = setOf(Category.ANY),
|
||||||
var language: Language = Language.ENGLISH,
|
var language: Language = Language.ENGLISH,
|
||||||
|
|
|
@ -34,6 +34,11 @@ package net.thauvin.erik.jokeapi.exceptions
|
||||||
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown whenever a server error occurs.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#status-codes) for more details.
|
||||||
|
*/
|
||||||
class HttpErrorException @JvmOverloads constructor(
|
class HttpErrorException @JvmOverloads constructor(
|
||||||
val statusCode: Int,
|
val statusCode: Int,
|
||||||
message: String,
|
message: String,
|
||||||
|
|
|
@ -32,6 +32,11 @@
|
||||||
|
|
||||||
package net.thauvin.erik.jokeapi.exceptions
|
package net.thauvin.erik.jokeapi.exceptions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown whenever an error occurs.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#errors) for more details.
|
||||||
|
*/
|
||||||
class JokeException @JvmOverloads constructor(
|
class JokeException @JvmOverloads constructor(
|
||||||
val error: Boolean,
|
val error: Boolean,
|
||||||
val internalError: Boolean,
|
val internalError: Boolean,
|
||||||
|
|
|
@ -33,7 +33,9 @@
|
||||||
package net.thauvin.erik.jokeapi.models
|
package net.thauvin.erik.jokeapi.models
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Categories and aliases.
|
* The supported joke categories and aliases, use [ANY] for all.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#categories) for more details.
|
||||||
*/
|
*/
|
||||||
enum class Category(val value: String) {
|
enum class Category(val value: String) {
|
||||||
ANY("Any"),
|
ANY("Any"),
|
||||||
|
|
|
@ -33,7 +33,9 @@
|
||||||
package net.thauvin.erik.jokeapi.models
|
package net.thauvin.erik.jokeapi.models
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Blacklist flags.
|
* The supported blacklist flags, use [ALL] to prevent all.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#flags-param) for more details.
|
||||||
*/
|
*/
|
||||||
enum class Flag(val value: String) {
|
enum class Flag(val value: String) {
|
||||||
NSFW("nsfw"),
|
NSFW("nsfw"),
|
||||||
|
|
|
@ -33,7 +33,9 @@
|
||||||
package net.thauvin.erik.jokeapi.models
|
package net.thauvin.erik.jokeapi.models
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Response formats.
|
* The supported response formats.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#format-param) for more details.
|
||||||
*/
|
*/
|
||||||
enum class Format(val value: String) {
|
enum class Format(val value: String) {
|
||||||
JSON("json"), XML("xml"), YAML("yaml"), TEXT("txt"), TXT(TEXT.value)
|
JSON("json"), XML("xml"), YAML("yaml"), TEXT("txt"), TXT(TEXT.value)
|
||||||
|
|
|
@ -32,4 +32,9 @@
|
||||||
|
|
||||||
package net.thauvin.erik.jokeapi.models
|
package net.thauvin.erik.jokeapi.models
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to specify a joke's ID or range of IDs.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#idrange-param) for more details.
|
||||||
|
*/
|
||||||
data class IdRange(val start: Int = -1, val end: Int = -1)
|
data class IdRange(val start: Int = -1, val end: Int = -1)
|
||||||
|
|
|
@ -32,6 +32,9 @@
|
||||||
|
|
||||||
package net.thauvin.erik.jokeapi.models
|
package net.thauvin.erik.jokeapi.models
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to store a joke's data.
|
||||||
|
*/
|
||||||
data class Joke(
|
data class Joke(
|
||||||
val error: Boolean,
|
val error: Boolean,
|
||||||
val category: Category,
|
val category: Category,
|
||||||
|
|
|
@ -33,19 +33,26 @@
|
||||||
package net.thauvin.erik.jokeapi.models
|
package net.thauvin.erik.jokeapi.models
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Supported languages.
|
* The supported languages.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#lang) for more details.
|
||||||
*/
|
*/
|
||||||
enum class Language(val value: String) {
|
enum class Language(val value: String) {
|
||||||
ENGLISH("en"),
|
ENGLISH("en"),
|
||||||
EN(ENGLISH.value),
|
EN(ENGLISH.value),
|
||||||
|
|
||||||
CZECH("cs"),
|
CZECH("cs"),
|
||||||
CS(CZECH.value),
|
CS(CZECH.value),
|
||||||
|
|
||||||
GERMAN("de"),
|
GERMAN("de"),
|
||||||
DE(GERMAN.value),
|
DE(GERMAN.value),
|
||||||
|
|
||||||
SPANISH("es"),
|
SPANISH("es"),
|
||||||
ES(SPANISH.value),
|
ES(SPANISH.value),
|
||||||
|
|
||||||
FRENCH("fr"),
|
FRENCH("fr"),
|
||||||
FR(FRENCH.value),
|
FR(FRENCH.value),
|
||||||
|
|
||||||
PORTUGUESE("pt"),
|
PORTUGUESE("pt"),
|
||||||
PT(PORTUGUESE.value)
|
PT(PORTUGUESE.value)
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,11 @@
|
||||||
|
|
||||||
package net.thauvin.erik.jokeapi.models
|
package net.thauvin.erik.jokeapi.models
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The URL Parameters.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#url-parameters) for more details.
|
||||||
|
*/
|
||||||
object Parameter {
|
object Parameter {
|
||||||
const val AMOUNT = "amount"
|
const val AMOUNT = "amount"
|
||||||
const val CONTAINS = "contains"
|
const val CONTAINS = "contains"
|
||||||
|
@ -45,4 +50,5 @@ object Parameter {
|
||||||
const val BLACKLIST_FLAGS = FLAGS
|
const val BLACKLIST_FLAGS = FLAGS
|
||||||
const val ID_RANGE = RANGE
|
const val ID_RANGE = RANGE
|
||||||
const val SAFE_MODE = SAFE
|
const val SAFE_MODE = SAFE
|
||||||
|
const val SEARCH = CONTAINS
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,9 @@
|
||||||
package net.thauvin.erik.jokeapi.models
|
package net.thauvin.erik.jokeapi.models
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joke types.
|
* The supported joke types, use [ALL] for all.
|
||||||
|
*
|
||||||
|
* Sse the [JokeAPI Documentation](https://jokeapi.dev/#type-param) for more details.
|
||||||
*/
|
*/
|
||||||
enum class Type(val value: String) {
|
enum class Type(val value: String) {
|
||||||
SINGLE("single"),
|
SINGLE("single"),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue