-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from yml-org/refactor/CM-1418/create-core
refactor: create core module
- Loading branch information
Showing
57 changed files
with
412 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
id("com.android.library") | ||
id("io.gitlab.arturbosch.detekt") | ||
id("org.jlleitschuh.gradle.ktlint") | ||
id("org.jetbrains.kotlinx.kover") | ||
} | ||
|
||
kover { | ||
verify { | ||
rule { | ||
name = "Minimal line coverage rate in percents" | ||
bound { | ||
minValue = 90 | ||
} | ||
} | ||
} | ||
} | ||
|
||
kotlin { | ||
explicitApi() | ||
android() | ||
jvm() | ||
listOf( | ||
iosX64(), | ||
iosArm64(), | ||
iosSimulatorArm64() | ||
).forEach { | ||
it.binaries.framework { | ||
baseName = "YChatCore" | ||
} | ||
} | ||
|
||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
api(Dependencies.Network.KTOR_NEGOTIATION) | ||
api(Dependencies.Network.KTOR_SERIALIZATION) | ||
api(Dependencies.Network.KTOR_CORE) | ||
api(Dependencies.Network.KTOR_LOGGING) | ||
api(Dependencies.DI.KOIN_CORE) | ||
} | ||
} | ||
val commonTest by getting { | ||
dependencies { | ||
implementation(kotlin("test")) | ||
implementation(Dependencies.Test.MOCKK_COMMON) | ||
implementation(Dependencies.Test.KTOR) | ||
implementation(Dependencies.Test.KOIN) | ||
} | ||
} | ||
val androidMain by getting { | ||
dependencies { | ||
implementation(Dependencies.Network.KTOR_OKHTTP) | ||
} | ||
} | ||
val iosX64Main by getting | ||
val iosArm64Main by getting | ||
val iosSimulatorArm64Main by getting | ||
val iosMain by creating { | ||
dependsOn(commonMain) | ||
iosX64Main.dependsOn(this) | ||
iosArm64Main.dependsOn(this) | ||
iosSimulatorArm64Main.dependsOn(this) | ||
dependencies { | ||
implementation(Dependencies.Network.KTOR_IOS) | ||
} | ||
} | ||
val iosTest by creating { | ||
dependsOn(commonTest) | ||
} | ||
val jvmMain by getting { | ||
dependencies { | ||
implementation(Dependencies.Network.KTOR_OKHTTP) | ||
} | ||
} | ||
val jvmTest by getting { | ||
dependencies { | ||
implementation(Dependencies.Test.MOCKK_JVM) | ||
} | ||
} | ||
} | ||
} | ||
|
||
android { | ||
namespace = "co.yml.ychat.core" | ||
compileSdk = Config.COMPILE_SDK_VERSION | ||
defaultConfig { | ||
minSdk = Config.MIN_SDK_VERSION | ||
targetSdk = Config.TARGET_SDK_VERSION | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
ychat-core/src/androidMain/kotlin/co/yml/ychat/core/model/FileBytes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package co.yml.ychat.core.model | ||
|
||
public actual typealias FileBytes = ByteArray | ||
|
||
public actual fun FileBytes.toByteArray(): ByteArray { | ||
return this | ||
} |
10 changes: 10 additions & 0 deletions
10
ychat-core/src/androidMain/kotlin/co/yml/ychat/core/network/factories/HttpEngineFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package co.yml.ychat.core.network.factories | ||
|
||
import io.ktor.client.engine.HttpClientEngine | ||
import io.ktor.client.engine.okhttp.OkHttp | ||
|
||
public actual object HttpEngineFactory { | ||
public actual fun getEngine(): HttpClientEngine { | ||
return OkHttp.create() | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
.../ychat/data/exception/ChatGptException.kt → ...l/ychat/core/exceptions/YChatException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
ychat-core/src/commonMain/kotlin/co/yml/ychat/core/model/FileBytes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package co.yml.ychat.core.model | ||
|
||
public expect class FileBytes | ||
|
||
public expect fun FileBytes.toByteArray(): ByteArray |
36 changes: 36 additions & 0 deletions
36
ychat-core/src/commonMain/kotlin/co/yml/ychat/core/network/extensions/ApiResultExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package co.yml.ychat.core.network.extensions | ||
|
||
import co.yml.ychat.core.exceptions.YChatException | ||
import co.yml.ychat.core.network.infrastructure.ApiResult | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.ResponseException | ||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.isSuccess | ||
import io.ktor.util.toMap | ||
|
||
public suspend inline fun <reified T> HttpResponse.toApiResult(): ApiResult<T> { | ||
val headers = this.headers.toMap() | ||
val statusCode = this.status.value | ||
return if (!this.status.isSuccess()) { | ||
val errorMessage = this.bodyAsText() | ||
val exception = YChatException(errorMessage, statusCode = statusCode) | ||
ApiResult(null, headers, statusCode, exception) | ||
} else { | ||
ApiResult(this.body<T>(), headers, statusCode, null) | ||
} | ||
} | ||
|
||
public fun <T> ResponseException.toApiResult(): ApiResult<T> { | ||
return ApiResult( | ||
statusCode = this.response.status.value, | ||
exception = YChatException(this.cause, this.response.status.value) | ||
) | ||
} | ||
|
||
public fun <T> Throwable.toApiResult(): ApiResult<T> { | ||
return ApiResult( | ||
statusCode = null, | ||
exception = YChatException(this.cause) | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
ychat-core/src/commonMain/kotlin/co/yml/ychat/core/network/factories/HttpClientFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package co.yml.ychat.core.network.factories | ||
|
||
import io.ktor.client.HttpClient | ||
|
||
public interface HttpClientFactory { | ||
public fun getHttpClient(): HttpClient | ||
} |
7 changes: 7 additions & 0 deletions
7
ychat-core/src/commonMain/kotlin/co/yml/ychat/core/network/factories/HttpEngineFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package co.yml.ychat.core.network.factories | ||
|
||
import io.ktor.client.engine.HttpClientEngine | ||
|
||
public expect object HttpEngineFactory { | ||
public actual fun getEngine(): HttpClientEngine | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
ychat-core/src/commonMain/kotlin/co/yml/ychat/core/network/infrastructure/ApiResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package co.yml.ychat.core.network.infrastructure | ||
|
||
import co.yml.ychat.core.exceptions.YChatException | ||
|
||
/** Encapsulates an outcome from source api. */ | ||
public data class ApiResult<T>( | ||
val body: T? = null, | ||
val headers: Map<String, List<String>> = mapOf(), | ||
val statusCode: Int? = null, | ||
val exception: YChatException? = null, | ||
) { | ||
|
||
/** Return true if the api outcome was successful. */ | ||
val isSuccessful: Boolean = exception == null | ||
|
||
/** Try to get [body], if it is null an [YChatException] will be thrown. */ | ||
public fun getBodyOrThrow(): T { | ||
val exception = exception | ||
?: YChatException("Could not retrieve body from ApiResult.") | ||
return body ?: throw exception | ||
} | ||
|
||
/** Throw an [exception] when [isSuccessful] is false. */ | ||
public fun ensureSuccess() { | ||
if (!isSuccessful) | ||
throw exception ?: YChatException("Api request was not successful.") | ||
} | ||
} |
12 changes: 6 additions & 6 deletions
12
.../yml/ychat/data/storage/ChatLogStorage.kt → .../yml/ychat/core/storage/ChatLogStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package co.yml.ychat.data.storage | ||
package co.yml.ychat.core.storage | ||
|
||
internal class ChatLogStorage { | ||
public class ChatLogStorage { | ||
|
||
private val chatLog: MutableList<String> = mutableListOf() | ||
|
||
fun getChatLog(): String { | ||
public fun getChatLog(): String { | ||
return chatLog.joinToString("\n") | ||
} | ||
|
||
fun buildChatInput(input: String): String { | ||
public fun buildChatInput(input: String): String { | ||
chatLog.add("Human: $input") | ||
return getChatLog() + "\n" + "AI: " | ||
} | ||
|
||
fun removeLastAppendedInput() { | ||
public fun removeLastAppendedInput() { | ||
chatLog.removeLast() | ||
} | ||
|
||
fun appendAnswer(answer: String) { | ||
public fun appendAnswer(answer: String) { | ||
chatLog.add("AI: $answer") | ||
} | ||
} |
Oops, something went wrong.