Skip to content

Commit cff9724

Browse files
committed
dictionaryapi.dev API
1 parent f9c892c commit cff9724

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-0
lines changed

english/dictionaryapi-dev/README.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
= English / dictionaryapi.dev
2+
3+
https://dictionaryapi.dev[dictionaryapi.dev] client.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
kotlin("jvm")
3+
kotlin("plugin.serialization")
4+
}
5+
6+
dependencies {
7+
implementation(platform(libs.ktor.bom))
8+
9+
implementation(libs.ktor.client.apache)
10+
implementation(libs.ktor.client.content.negotiation)
11+
implementation(libs.ktor.serialization.kotlinx.json)
12+
implementation(libs.log4j.api)
13+
14+
testImplementation(libs.junit.jupiter.api)
15+
testImplementation(libs.junit.jupiter.params)
16+
testImplementation(libs.mockk)
17+
testRuntimeOnly(libs.junit.jupiter.engine)
18+
testRuntimeOnly(libs.log4j.core)
19+
}
20+
21+
tasks {
22+
val integrationTest by registering(Test::class) {
23+
group = LifecycleBasePlugin.VERIFICATION_GROUP
24+
description = "Runs the integration tests."
25+
shouldRunAfter("test")
26+
outputs.upToDateWhen { false }
27+
useJUnitPlatform {
28+
includeTags("it")
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package by.jprof.telegram.bot.english.dictionaryapi_dev
2+
3+
interface DictionaryAPIDotDevClient {
4+
suspend fun define(term: String): Collection<Word>
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package by.jprof.telegram.bot.english.dictionaryapi_dev
2+
3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.call.body
5+
import io.ktor.client.engine.apache.Apache
6+
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
7+
import io.ktor.client.request.get
8+
import io.ktor.client.request.url
9+
import io.ktor.http.encodeURLPathPart
10+
import io.ktor.serialization.kotlinx.json.json
11+
import java.io.Closeable
12+
import kotlinx.serialization.json.Json
13+
14+
class KtorDictionaryAPIDotDevClient(
15+
private val baseUrl: String = "https://api.dictionaryapi.dev/api/v2"
16+
) : DictionaryAPIDotDevClient, Closeable {
17+
private val client = HttpClient(Apache) {
18+
install(ContentNegotiation) {
19+
json(
20+
Json {
21+
ignoreUnknownKeys = true
22+
}
23+
)
24+
}
25+
}
26+
27+
override suspend fun define(term: String): Collection<Word> =
28+
client.get {
29+
url("$baseUrl/entries/en/${term.encodeURLPathPart()}")
30+
}.body()
31+
32+
override fun close() {
33+
client.close()
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package by.jprof.telegram.bot.english.dictionaryapi_dev
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class Word(
7+
val word: String,
8+
val phonetics: Collection<Phonetic>? = null,
9+
val meanings: Collection<Meaning>? = null,
10+
val license: License? = null,
11+
val sourceUrls: Collection<String>? = null,
12+
)
13+
14+
@Serializable
15+
data class Phonetic(
16+
val text: String? = null,
17+
val audio: String? = null,
18+
val sourceUrl: String? = null,
19+
val license: License? = null,
20+
)
21+
22+
@Serializable
23+
data class Meaning(
24+
val partOfSpeech: String,
25+
val definitions: Collection<Definition>,
26+
val synonyms: Collection<String>? = null,
27+
val antonyms: Collection<String>? = null,
28+
)
29+
30+
@Serializable
31+
data class Definition(
32+
val definition: String,
33+
val example: String? = null,
34+
val synonyms: Collection<String>? = null,
35+
val antonyms: Collection<String>? = null,
36+
)
37+
38+
@Serializable
39+
data class License(
40+
val name: String,
41+
val url: String,
42+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package by.jprof.telegram.bot.english.dictionaryapi_dev
2+
3+
import kotlinx.coroutines.runBlocking
4+
import org.junit.jupiter.api.Assertions.assertTrue
5+
import org.junit.jupiter.api.BeforeAll
6+
import org.junit.jupiter.api.Tag
7+
import org.junit.jupiter.api.Test
8+
import org.junit.jupiter.api.TestInstance
9+
10+
@Tag("it")
11+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
12+
internal class KtorKtorDictionaryAPIDotDevClientIntegrationTest {
13+
private lateinit var sut: KtorDictionaryAPIDotDevClient
14+
15+
@BeforeAll
16+
internal fun setup() {
17+
sut = KtorDictionaryAPIDotDevClient()
18+
}
19+
20+
@Test
21+
fun define() = runBlocking {
22+
val definitions = sut.define("motherfucker")
23+
24+
assertTrue(definitions.isNotEmpty())
25+
}
26+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ include(":english")
3838
include(":english:language-rooms")
3939
include(":english:language-rooms:dynamodb")
4040
include(":english:urban-dictionary")
41+
include(":english:dictionaryapi-dev")
4142
include(":english:urban-word-of-the-day")
4243
include(":english:urban-word-of-the-day:dynamodb")
4344
include(":english:urban-word-of-the-day-formatter")

0 commit comments

Comments
 (0)