Skip to content

Commit 5f74aa9

Browse files
authored
Merge pull request #69 from yml-org/refactor/CM-1418/create-core
refactor: create core module
2 parents 032cfdb + d3dd11f commit 5f74aa9

File tree

57 files changed

+412
-257
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+412
-257
lines changed

settings.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ dependencyResolutionManagement {
1717
rootProject.name = "ychat-sdk"
1818
include(":ychat")
1919
include(":sample:android")
20-
include(":sample:jvm")
20+
include(":sample:jvm")
21+
include(":ychat-core")

ychat-core/build.gradle.kts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
plugins {
2+
kotlin("multiplatform")
3+
id("com.android.library")
4+
id("io.gitlab.arturbosch.detekt")
5+
id("org.jlleitschuh.gradle.ktlint")
6+
id("org.jetbrains.kotlinx.kover")
7+
}
8+
9+
kover {
10+
verify {
11+
rule {
12+
name = "Minimal line coverage rate in percents"
13+
bound {
14+
minValue = 90
15+
}
16+
}
17+
}
18+
}
19+
20+
kotlin {
21+
explicitApi()
22+
android()
23+
jvm()
24+
listOf(
25+
iosX64(),
26+
iosArm64(),
27+
iosSimulatorArm64()
28+
).forEach {
29+
it.binaries.framework {
30+
baseName = "YChatCore"
31+
}
32+
}
33+
34+
sourceSets {
35+
val commonMain by getting {
36+
dependencies {
37+
api(Dependencies.Network.KTOR_NEGOTIATION)
38+
api(Dependencies.Network.KTOR_SERIALIZATION)
39+
api(Dependencies.Network.KTOR_CORE)
40+
api(Dependencies.Network.KTOR_LOGGING)
41+
api(Dependencies.DI.KOIN_CORE)
42+
}
43+
}
44+
val commonTest by getting {
45+
dependencies {
46+
implementation(kotlin("test"))
47+
implementation(Dependencies.Test.MOCKK_COMMON)
48+
implementation(Dependencies.Test.KTOR)
49+
implementation(Dependencies.Test.KOIN)
50+
}
51+
}
52+
val androidMain by getting {
53+
dependencies {
54+
implementation(Dependencies.Network.KTOR_OKHTTP)
55+
}
56+
}
57+
val iosX64Main by getting
58+
val iosArm64Main by getting
59+
val iosSimulatorArm64Main by getting
60+
val iosMain by creating {
61+
dependsOn(commonMain)
62+
iosX64Main.dependsOn(this)
63+
iosArm64Main.dependsOn(this)
64+
iosSimulatorArm64Main.dependsOn(this)
65+
dependencies {
66+
implementation(Dependencies.Network.KTOR_IOS)
67+
}
68+
}
69+
val iosTest by creating {
70+
dependsOn(commonTest)
71+
}
72+
val jvmMain by getting {
73+
dependencies {
74+
implementation(Dependencies.Network.KTOR_OKHTTP)
75+
}
76+
}
77+
val jvmTest by getting {
78+
dependencies {
79+
implementation(Dependencies.Test.MOCKK_JVM)
80+
}
81+
}
82+
}
83+
}
84+
85+
android {
86+
namespace = "co.yml.ychat.core"
87+
compileSdk = Config.COMPILE_SDK_VERSION
88+
defaultConfig {
89+
minSdk = Config.MIN_SDK_VERSION
90+
targetSdk = Config.TARGET_SDK_VERSION
91+
}
92+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package co.yml.ychat.core.model
2+
3+
public actual typealias FileBytes = ByteArray
4+
5+
public actual fun FileBytes.toByteArray(): ByteArray {
6+
return this
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package co.yml.ychat.core.network.factories
2+
3+
import io.ktor.client.engine.HttpClientEngine
4+
import io.ktor.client.engine.okhttp.OkHttp
5+
6+
public actual object HttpEngineFactory {
7+
public actual fun getEngine(): HttpClientEngine {
8+
return OkHttp.create()
9+
}
10+
}

ychat/src/commonMain/kotlin/co/yml/ychat/data/exception/ChatGptException.kt renamed to ychat-core/src/commonMain/kotlin/co/yml/ychat/core/exceptions/YChatException.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package co.yml.ychat.data.exception
1+
package co.yml.ychat.core.exceptions
22

3-
class ChatGptException(
3+
public class YChatException(
44
message: String? = null,
55
cause: Throwable? = null,
6-
var statusCode: Int? = null,
6+
public var statusCode: Int? = null,
77
) : Exception(message, cause) {
88

9-
constructor(
9+
public constructor(
1010
cause: Throwable?,
1111
statusCode: Int? = null
1212
) : this(message = null, cause = cause, statusCode = statusCode)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package co.yml.ychat.core.model
2+
3+
public expect class FileBytes
4+
5+
public expect fun FileBytes.toByteArray(): ByteArray
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package co.yml.ychat.core.network.extensions
2+
3+
import co.yml.ychat.core.exceptions.YChatException
4+
import co.yml.ychat.core.network.infrastructure.ApiResult
5+
import io.ktor.client.call.body
6+
import io.ktor.client.plugins.ResponseException
7+
import io.ktor.client.statement.HttpResponse
8+
import io.ktor.client.statement.bodyAsText
9+
import io.ktor.http.isSuccess
10+
import io.ktor.util.toMap
11+
12+
public suspend inline fun <reified T> HttpResponse.toApiResult(): ApiResult<T> {
13+
val headers = this.headers.toMap()
14+
val statusCode = this.status.value
15+
return if (!this.status.isSuccess()) {
16+
val errorMessage = this.bodyAsText()
17+
val exception = YChatException(errorMessage, statusCode = statusCode)
18+
ApiResult(null, headers, statusCode, exception)
19+
} else {
20+
ApiResult(this.body<T>(), headers, statusCode, null)
21+
}
22+
}
23+
24+
public fun <T> ResponseException.toApiResult(): ApiResult<T> {
25+
return ApiResult(
26+
statusCode = this.response.status.value,
27+
exception = YChatException(this.cause, this.response.status.value)
28+
)
29+
}
30+
31+
public fun <T> Throwable.toApiResult(): ApiResult<T> {
32+
return ApiResult(
33+
statusCode = null,
34+
exception = YChatException(this.cause)
35+
)
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package co.yml.ychat.core.network.factories
2+
3+
import io.ktor.client.HttpClient
4+
5+
public interface HttpClientFactory {
6+
public fun getHttpClient(): HttpClient
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package co.yml.ychat.core.network.factories
2+
3+
import io.ktor.client.engine.HttpClientEngine
4+
5+
public expect object HttpEngineFactory {
6+
public actual fun getEngine(): HttpClientEngine
7+
}

ychat/src/commonMain/kotlin/co/yml/ychat/data/infrastructure/ApiExecutor.kt renamed to ychat-core/src/commonMain/kotlin/co/yml/ychat/core/network/infrastructure/ApiExecutor.kt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package co.yml.ychat.data.infrastructure
1+
package co.yml.ychat.core.network.infrastructure
22

3-
import io.ktor.client.HttpClient
3+
import co.yml.ychat.core.network.extensions.toApiResult
4+
import co.yml.ychat.core.network.factories.HttpClientFactory
45
import io.ktor.client.plugins.ResponseException
56
import io.ktor.client.request.forms.FormPart
67
import io.ktor.client.request.forms.formData
@@ -16,7 +17,9 @@ import io.ktor.http.HttpMethod
1617
import io.ktor.utils.io.errors.IOException
1718
import kotlin.collections.set
1819

19-
internal class ApiExecutor(private val httpClient: HttpClient) {
20+
public class ApiExecutor(private val httpClientFactory: HttpClientFactory) {
21+
22+
private val httpClient by lazy { httpClientFactory.getHttpClient() }
2023

2124
private lateinit var endpoint: String
2225

@@ -26,39 +29,39 @@ internal class ApiExecutor(private val httpClient: HttpClient) {
2629

2730
private var query: HashMap<String, String> = HashMap()
2831

29-
private val formParts = mutableListOf<FormPart<*>>()
32+
public val formParts: MutableList<FormPart<*>> = mutableListOf()
3033

31-
fun setEndpoint(endpoint: String): ApiExecutor {
34+
public fun setEndpoint(endpoint: String): ApiExecutor {
3235
this.endpoint = endpoint
3336
return this
3437
}
3538

36-
fun setHttpMethod(httpMethod: HttpMethod): ApiExecutor {
39+
public fun setHttpMethod(httpMethod: HttpMethod): ApiExecutor {
3740
this.httpMethod = httpMethod
3841
return this
3942
}
4043

41-
fun setBody(body: Any): ApiExecutor {
44+
public fun setBody(body: Any): ApiExecutor {
4245
this.body = body
4346
return this
4447
}
4548

46-
fun addQuery(key: String, value: String): ApiExecutor {
49+
public fun addQuery(key: String, value: String): ApiExecutor {
4750
this.query[key] = value
4851
return this
4952
}
5053

51-
fun addQuery(key: String, value: List<String>): ApiExecutor {
54+
public fun addQuery(key: String, value: List<String>): ApiExecutor {
5255
this.query[key] = value.joinToString(",")
5356
return this
5457
}
5558

56-
fun <T : Any> addFormPart(key: String, value: T): ApiExecutor {
59+
public fun <T : Any> addFormPart(key: String, value: T): ApiExecutor {
5760
formParts += FormPart(key, value)
5861
return this
5962
}
6063

61-
fun addFormPart(key: String, fileName: String, value: ByteArray): ApiExecutor {
64+
public fun addFormPart(key: String, fileName: String, value: ByteArray): ApiExecutor {
6265
val headers = Headers.build {
6366
append(HttpHeaders.ContentType, ContentType.Application.OctetStream.contentType)
6467
append(HttpHeaders.ContentDisposition, "filename=$fileName")
@@ -67,7 +70,7 @@ internal class ApiExecutor(private val httpClient: HttpClient) {
6770
return this
6871
}
6972

70-
suspend inline fun <reified T> execute(): ApiResult<T> {
73+
public suspend inline fun <reified T> execute(): ApiResult<T> {
7174
return try {
7275
val response = if (formParts.isEmpty()) executeRequest() else executeRequestAsForm()
7376
return response.toApiResult()
@@ -78,15 +81,15 @@ internal class ApiExecutor(private val httpClient: HttpClient) {
7881
}
7982
}
8083

81-
private suspend fun executeRequest(): HttpResponse {
84+
public suspend fun executeRequest(): HttpResponse {
8285
return httpClient.request(endpoint) {
8386
url { query.forEach { parameters.append(it.key, it.value) } }
8487
method = httpMethod
85-
setBody(this@ApiExecutor.body)
88+
this.setBody(this@ApiExecutor.body)
8689
}
8790
}
8891

89-
private suspend fun executeRequestAsForm(): HttpResponse {
92+
public suspend fun executeRequestAsForm(): HttpResponse {
9093
return httpClient.submitFormWithBinaryData(
9194
url = endpoint,
9295
formData = formData { formParts.forEach { append(it) } }

0 commit comments

Comments
 (0)