Converter type parameter to a single value
This commit is contained in:
parent
39faa04174
commit
781535bce2
2 changed files with 25 additions and 35 deletions
|
@ -45,7 +45,7 @@ import java.util.stream.Collectors
|
||||||
class JokeApi {
|
class JokeApi {
|
||||||
companion object {
|
companion object {
|
||||||
private const val API_URL = "https://v2.jokeapi.dev/joke/"
|
private const val API_URL = "https://v2.jokeapi.dev/joke/"
|
||||||
val logger: Logger = Logger.getLogger(JokeApi::class.java.simpleName)
|
val logger: Logger by lazy { Logger.getLogger(JokeApi::class.java.simpleName) }
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
|
@ -54,7 +54,7 @@ class JokeApi {
|
||||||
categories: Set<Category> = setOf(Category.ANY),
|
categories: Set<Category> = setOf(Category.ANY),
|
||||||
language: Language = Language.ENGLISH,
|
language: Language = Language.ENGLISH,
|
||||||
flags: Set<Flag> = emptySet(),
|
flags: Set<Flag> = emptySet(),
|
||||||
type: Set<Type> = emptySet(),
|
type: Type = Type.ALL,
|
||||||
format: Format = Format.JSON,
|
format: Format = Format.JSON,
|
||||||
search: String = "",
|
search: String = "",
|
||||||
idRange: IdRange = IdRange(),
|
idRange: IdRange = IdRange(),
|
||||||
|
@ -88,8 +88,8 @@ class JokeApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type
|
// Type
|
||||||
if (type.isNotEmpty()) {
|
if (type != Type.ALL) {
|
||||||
urlParams.add("type=" + type.stream().map(Type::value).collect(Collectors.joining(",")))
|
urlParams.add("type=${type.value}")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format
|
// Format
|
||||||
|
@ -136,13 +136,12 @@ class JokeApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(JokeException::class, IOException::class)
|
@Throws(JokeException::class, IOException::class)
|
||||||
private fun fetchUrl(url: String) : String {
|
private fun fetchUrl(url: String): String {
|
||||||
logger.log(Level.FINE, url)
|
logger.log(Level.FINE, url)
|
||||||
|
|
||||||
val connection = URL(url).openConnection() as HttpURLConnection
|
val connection = URL(url).openConnection() as HttpURLConnection
|
||||||
connection.setRequestProperty(
|
connection.setRequestProperty(
|
||||||
"User-Agent",
|
"User-Agent", "Mozilla/5.0 (Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0"
|
||||||
"Mozilla/5.0 (Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (connection.responseCode in 200..399) {
|
if (connection.responseCode in 200..399) {
|
||||||
|
@ -154,24 +153,19 @@ class JokeApi {
|
||||||
} else {
|
} else {
|
||||||
when (connection.responseCode) {
|
when (connection.responseCode) {
|
||||||
400 -> throw IOException(
|
400 -> throw IOException(
|
||||||
"400: Bad Request",
|
"400: Bad Request", IOException(
|
||||||
IOException(
|
"The request you have sent to JokeAPI is formatted incorrectly and cannot be processed"
|
||||||
"The request you have sent to JokeAPI is formatted incorrectly and cannot be" +
|
|
||||||
" processed"
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
403 -> throw IOException(
|
403 -> throw IOException(
|
||||||
"4o3: Forbidden",
|
"4o3: Forbidden", IOException(
|
||||||
IOException(
|
"You have been added to the blacklist due to malicious behavior and are not allowed" + " to send requests to JokeAPI anymore"
|
||||||
"You have been added to the blacklist due to malicious behavior and are not allowed" +
|
|
||||||
" to send requests to JokeAPI anymore"
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
404 -> throw IOException(
|
404 -> throw IOException(
|
||||||
"404: Not Found",
|
"404: Not Found", IOException("The URL you have requested couldn't be found")
|
||||||
IOException("The URL you have requested couldn't be found")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
413 -> throw IOException(
|
413 -> throw IOException(
|
||||||
|
@ -180,26 +174,20 @@ class JokeApi {
|
||||||
)
|
)
|
||||||
|
|
||||||
428 -> throw IOException(
|
428 -> throw IOException(
|
||||||
"429: Too Many Requests",
|
"429: Too Many Requests", IOException(
|
||||||
IOException(
|
"You have exceeded the limit of 120 requests per minute and have to wait a bit" + " until you are allowed to send requests again"
|
||||||
"You have exceeded the limit of 120 requests per minute and have to wait a bit" +
|
|
||||||
" until you are allowed to send requests again"
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
500 -> throw IOException(
|
500 -> throw IOException(
|
||||||
"500: Internal Server Error",
|
"500: Internal Server Error", IOException(
|
||||||
IOException(
|
"There was a general internal error within JokeAPI. You can get more info from" + " the properties in the response text"
|
||||||
"There was a general internal error within JokeAPI. You can get more info from" +
|
|
||||||
" the properties in the response text"
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
523 -> throw IOException(
|
523 -> throw IOException(
|
||||||
"523: Origin Unreachable",
|
"523: Origin Unreachable", IOException(
|
||||||
IOException(
|
"The server is temporarily offline due to maintenance or a dynamic IP update." + " Please be patient in this case."
|
||||||
"The server is temporarily offline due to maintenance or a dynamic IP update." +
|
|
||||||
" Please be patient in this case."
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -215,7 +203,7 @@ class JokeApi {
|
||||||
categories: Set<Category> = setOf(Category.ANY),
|
categories: Set<Category> = setOf(Category.ANY),
|
||||||
language: Language = Language.ENGLISH,
|
language: Language = Language.ENGLISH,
|
||||||
flags: Set<Flag> = emptySet(),
|
flags: Set<Flag> = emptySet(),
|
||||||
type: Set<Type> = emptySet(),
|
type: Type = Type.ALL,
|
||||||
search: String = "",
|
search: String = "",
|
||||||
idRange: IdRange = IdRange(),
|
idRange: IdRange = IdRange(),
|
||||||
safe: Boolean = false
|
safe: Boolean = false
|
||||||
|
@ -244,12 +232,13 @@ class JokeApi {
|
||||||
jokes.addAll(json.getString("joke").split("\n"))
|
jokes.addAll(json.getString("joke").split("\n"))
|
||||||
}
|
}
|
||||||
val enabledFlags = mutableSetOf<Flag>()
|
val enabledFlags = mutableSetOf<Flag>()
|
||||||
val flagObject = json.getJSONObject("flags")
|
val jsonFlags = json.getJSONObject("flags")
|
||||||
for (flag in Flag.values()) {
|
Flag.values().filter { it != Flag.ALL }.forEach {
|
||||||
if (flag != Flag.ALL && flagObject.getBoolean(flag.value)) {
|
if (jsonFlags.has(it.value) && jsonFlags.getBoolean(it.value)) {
|
||||||
enabledFlags.add(flag)
|
enabledFlags.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Joke(
|
return Joke(
|
||||||
error = false,
|
error = false,
|
||||||
category = Category.valueOf(json.getString("category").uppercase()),
|
category = Category.valueOf(json.getString("category").uppercase()),
|
||||||
|
|
|
@ -37,5 +37,6 @@ package net.thauvin.erik.jokeapi
|
||||||
*/
|
*/
|
||||||
enum class Type(val value: String) {
|
enum class Type(val value: String) {
|
||||||
SINGLE("single"),
|
SINGLE("single"),
|
||||||
TWOPART("twopart")
|
TWOPART("twopart"),
|
||||||
|
ALL("${SINGLE.value},${TWOPART.value}")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue