File tree Expand file tree Collapse file tree 7 files changed +143
-0
lines changed
english/dictionaryapi-dev
main/kotlin/by/jprof/telegram/bot/english/dictionaryapi_dev
test/kotlin/by/jprof/telegram/bot/english/dictionaryapi_dev Expand file tree Collapse file tree 7 files changed +143
-0
lines changed Original file line number Diff line number Diff line change 1+ = English / dictionaryapi.dev
2+
3+ https://dictionaryapi.dev[dictionaryapi.dev] client.
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ include(":english")
3838include(" :english:language-rooms" )
3939include(" :english:language-rooms:dynamodb" )
4040include(" :english:urban-dictionary" )
41+ include(" :english:dictionaryapi-dev" )
4142include(" :english:urban-word-of-the-day" )
4243include(" :english:urban-word-of-the-day:dynamodb" )
4344include(" :english:urban-word-of-the-day-formatter" )
You can’t perform that action at this time.
0 commit comments