From eb470f7d14a02c8ce2b2fae875bd664bf70fbb02 Mon Sep 17 00:00:00 2001 From: slam Date: Sat, 16 Mar 2024 22:26:13 +0800 Subject: [PATCH] feat(BE-174): integrate anthropic api - code refactor by using Koin --- .../com/tddworks/anthropic/api/Anthropic.kt | 49 ++++++++++++++++--- .../anthropic/api/internal/AnthropicApi.kt | 18 +------ .../kotlin/com/tddworks/anthropic/di/Koin.kt | 6 +-- .../tddworks/anthropic/api/AnthropicTest.kt | 11 +++-- 4 files changed, 52 insertions(+), 32 deletions(-) diff --git a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.kt b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.kt index f908d04..f5180de 100644 --- a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.kt +++ b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.kt @@ -1,7 +1,14 @@ package com.tddworks.anthropic.api import com.tddworks.anthropic.api.internal.AnthropicApi +import com.tddworks.anthropic.api.messages.api.AnthropicConfig import com.tddworks.anthropic.api.messages.api.Messages +import com.tddworks.anthropic.api.messages.api.internal.DefaultMessagesApi +import com.tddworks.anthropic.di.initKoin +import com.tddworks.common.network.api.ktor.api.HttpRequester +import com.tddworks.common.network.api.ktor.internal.createHttpClient +import com.tddworks.common.network.api.ktor.internal.default +import org.koin.dsl.module interface Anthropic : Messages { companion object { @@ -14,11 +21,37 @@ interface Anthropic : Messages { } fun Anthropic( - apiKey: String = "CONFIGURE_ME", - baseUrl: String = Anthropic.BASE_URL, - anthropicVersion: String = "2023-06-01", -): Anthropic = AnthropicApi( - apiKey = apiKey, - apiURL = baseUrl, - anthropicVersion = anthropicVersion -) \ No newline at end of file + apiKey: () -> String = { "CONFIGURE_ME" }, + baseUrl: () -> String = { Anthropic.BASE_URL }, + anthropicVersion: () -> String = { "2023-06-01" }, +): Anthropic { + + initKoin( + module = module { + single { + HttpRequester.default( + createHttpClient( + url = baseUrl() + ) + ) + } + single { + DefaultMessagesApi( + anthropicConfig = AnthropicConfig( + apiKey = apiKey, + anthropicVersion = anthropicVersion + + ), + jsonLenient = get(), + requester = get() + ) + } + } + ) + + return AnthropicApi( + apiKey = apiKey(), + apiURL = baseUrl(), + anthropicVersion = anthropicVersion() + ) +} \ No newline at end of file diff --git a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/internal/AnthropicApi.kt b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/internal/AnthropicApi.kt index 3d52d5c..846abb5 100644 --- a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/internal/AnthropicApi.kt +++ b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/internal/AnthropicApi.kt @@ -1,29 +1,15 @@ package com.tddworks.anthropic.api.internal import com.tddworks.anthropic.api.Anthropic -import com.tddworks.anthropic.api.messages.api.AnthropicConfig import com.tddworks.anthropic.api.messages.api.Messages -import com.tddworks.anthropic.api.messages.api.internal.DefaultMessagesApi -import com.tddworks.common.network.api.ktor.api.HttpRequester -import com.tddworks.common.network.api.ktor.internal.createHttpClient -import com.tddworks.common.network.api.ktor.internal.default +import com.tddworks.di.getInstance class AnthropicApi( private val apiKey: String, private val apiURL: String, private val anthropicVersion: String, -) : Anthropic, Messages by DefaultMessagesApi( - AnthropicConfig( - apiKey = { apiKey }, - anthropicVersion = { anthropicVersion } - ), - HttpRequester.default( - createHttpClient( - url = apiURL - ) - ) -) { +) : Anthropic, Messages by getInstance() { override fun apiKey(): String { return apiKey } diff --git a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt index 72bfe4f..c8ca9e5 100644 --- a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt +++ b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt @@ -9,13 +9,13 @@ import org.koin.dsl.module expect fun platformModule(): Module -fun initKoin(enableNetworkLogs: Boolean = false, appDeclaration: KoinAppDeclaration = {}) = +fun initKoin(module: Module, appDeclaration: KoinAppDeclaration = {}) = startKoin { appDeclaration() - modules(commonModule(enableNetworkLogs = enableNetworkLogs)) + modules(commonModule() + module) } -fun commonModule(enableNetworkLogs: Boolean) = module { +fun commonModule() = module { singleOf(::createJson) } diff --git a/anthropic-client/anthropic-client-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/AnthropicTest.kt b/anthropic-client/anthropic-client-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/AnthropicTest.kt index c1250bc..55d81a9 100644 --- a/anthropic-client/anthropic-client-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/AnthropicTest.kt +++ b/anthropic-client/anthropic-client-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/AnthropicTest.kt @@ -1,16 +1,17 @@ package com.tddworks.anthropic.api -import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test +import org.koin.test.junit5.AutoCloseKoinTest -class AnthropicTest { +class AnthropicTest : AutoCloseKoinTest() { @Test fun `should return overridden settings`() { val target = Anthropic( - baseUrl = "http://localhost:8080", - apiKey = "1234", - anthropicVersion = "2024-03-01" + baseUrl = { "http://localhost:8080" }, + apiKey = { "1234" }, + anthropicVersion = { "2024-03-01" } ) assertEquals("http://localhost:8080", target.baseUrl())