-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: [:core:network] - Migrated to KMP
- Loading branch information
1 parent
7520dd3
commit ab30694
Showing
39 changed files
with
796 additions
and
678 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
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
51 changes: 51 additions & 0 deletions
51
core/network/src/androidMain/kotlin/org/mifos/mobile/core/network/KtorHttpClient.android.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,51 @@ | ||
/* | ||
* Copyright 2025 Mifos Initiative | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md | ||
*/ | ||
package org.mifos.mobile.core.network | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.engine.okhttp.OkHttp | ||
import io.ktor.client.plugins.HttpTimeout | ||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
import io.ktor.client.plugins.logging.DEFAULT | ||
import io.ktor.client.plugins.logging.LogLevel | ||
import io.ktor.client.plugins.logging.Logger | ||
import io.ktor.client.plugins.logging.Logging | ||
import io.ktor.serialization.kotlinx.json.json | ||
import kotlinx.serialization.json.Json | ||
import co.touchlab.kermit.Logger.Companion as KermitLogger | ||
|
||
actual val ktorHttpClient: HttpClient | ||
get() = HttpClient(OkHttp) { | ||
install(HttpTimeout) { | ||
socketTimeoutMillis = 60_000 | ||
requestTimeoutMillis = 60_000 | ||
} | ||
|
||
install(Logging) { | ||
logger = Logger.DEFAULT | ||
level = LogLevel.ALL | ||
logger = object : Logger { | ||
override fun log(message: String) { | ||
KermitLogger.d(tag = "KtorClient", messageString = message) | ||
} | ||
} | ||
} | ||
|
||
install(ContentNegotiation) { | ||
json( | ||
Json { | ||
prettyPrint = true | ||
isLenient = true | ||
ignoreUnknownKeys = true | ||
explicitNulls = false | ||
}, | ||
) | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
core/network/src/commonMain/kotlin/org/mifos/mobile/core/network/DataManager.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,39 @@ | ||
/* | ||
* Copyright 2024 Mifos Initiative | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md | ||
*/ | ||
package org.mifos.mobile.core.network | ||
|
||
class DataManager( | ||
private val ktorfitClient: KtorfitClient, | ||
) { | ||
|
||
internal val authenticationApi by lazy { ktorfitClient.authenticationApi } | ||
|
||
internal val beneficiaryApi by lazy { ktorfitClient.beneficiaryApi } | ||
|
||
internal val clientsApi by lazy { ktorfitClient.clientsApi } | ||
|
||
internal val loanAccountsListApi by lazy { ktorfitClient.loanAccountsListApi } | ||
|
||
internal val savingAccountsListApi by lazy { ktorfitClient.savingAccountsListApi } | ||
|
||
internal val recentTransactionsApi by lazy { ktorfitClient.recentTransactionsApi } | ||
|
||
internal val clientChargeApi by lazy { ktorfitClient.clientChargeApi } | ||
|
||
internal val thirdPartyTransferApi by lazy { ktorfitClient.thirdPartyTransferApi } | ||
|
||
internal val registrationApi by lazy { ktorfitClient.registrationApi } | ||
|
||
internal val notificationApi by lazy { ktorfitClient.notificationApi } | ||
|
||
internal val userDetailsApi by lazy { ktorfitClient.userDetailsApi } | ||
|
||
internal val guarantorApi by lazy { ktorfitClient.guarantorApi } | ||
} |
14 changes: 14 additions & 0 deletions
14
core/network/src/commonMain/kotlin/org/mifos/mobile/core/network/KtorHttpClient.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,14 @@ | ||
/* | ||
* Copyright 2025 Mifos Initiative | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md | ||
*/ | ||
package org.mifos.mobile.core.network | ||
|
||
import io.ktor.client.HttpClient | ||
|
||
expect val ktorHttpClient: HttpClient |
86 changes: 86 additions & 0 deletions
86
core/network/src/commonMain/kotlin/org/mifos/mobile/core/network/KtorfitClient.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,86 @@ | ||
/* | ||
* Copyright 2025 Mifos Initiative | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md | ||
*/ | ||
package org.mifos.mobile.core.network | ||
|
||
import de.jensklingenberg.ktorfit.Ktorfit | ||
import io.ktor.client.HttpClient | ||
import org.mifos.mobile.core.network.services.createAuthenticationService | ||
import org.mifos.mobile.core.network.services.createBeneficiaryService | ||
import org.mifos.mobile.core.network.services.createClientChargeService | ||
import org.mifos.mobile.core.network.services.createClientService | ||
import org.mifos.mobile.core.network.services.createGuarantorService | ||
import org.mifos.mobile.core.network.services.createLoanAccountsListService | ||
import org.mifos.mobile.core.network.services.createNotificationService | ||
import org.mifos.mobile.core.network.services.createRecentTransactionsService | ||
import org.mifos.mobile.core.network.services.createRegistrationService | ||
import org.mifos.mobile.core.network.services.createSavingAccountsListService | ||
import org.mifos.mobile.core.network.services.createThirdPartyTransferService | ||
import org.mifos.mobile.core.network.services.createUserDetailsService | ||
import org.mifos.mobile.core.network.utils.FlowConverterFactory | ||
|
||
class KtorfitClient( | ||
ktorfit: Ktorfit, | ||
) { | ||
|
||
internal val authenticationApi by lazy { ktorfit.createAuthenticationService() } | ||
|
||
internal val beneficiaryApi by lazy { ktorfit.createBeneficiaryService() } | ||
|
||
internal val clientsApi by lazy { ktorfit.createClientService() } | ||
|
||
internal val loanAccountsListApi by lazy { ktorfit.createLoanAccountsListService() } | ||
|
||
internal val savingAccountsListApi by lazy { ktorfit.createSavingAccountsListService() } | ||
|
||
internal val recentTransactionsApi by lazy { ktorfit.createRecentTransactionsService() } | ||
|
||
internal val clientChargeApi by lazy { ktorfit.createClientChargeService() } | ||
|
||
internal val thirdPartyTransferApi by lazy { ktorfit.createThirdPartyTransferService() } | ||
|
||
internal val registrationApi by lazy { ktorfit.createRegistrationService() } | ||
|
||
internal val notificationApi by lazy { ktorfit.createNotificationService() } | ||
|
||
internal val userDetailsApi by lazy { ktorfit.createUserDetailsService() } | ||
|
||
internal val guarantorApi by lazy { ktorfit.createGuarantorService() } | ||
|
||
class Builder internal constructor() { | ||
private lateinit var baseURL: String | ||
private lateinit var httpClient: HttpClient | ||
|
||
fun baseURL(baseURL: String): Builder { | ||
this.baseURL = baseURL | ||
return this | ||
} | ||
|
||
fun httpClient(ktorHttpClient: HttpClient): Builder { | ||
this.httpClient = ktorHttpClient | ||
return this | ||
} | ||
|
||
fun build(): KtorfitClient { | ||
val ktorfitBuilder = Ktorfit.Builder() | ||
.httpClient(httpClient) | ||
.baseUrl(baseURL) | ||
.converterFactories(FlowConverterFactory()) | ||
.build() | ||
|
||
return KtorfitClient(ktorfitBuilder) | ||
} | ||
} | ||
|
||
companion object { | ||
fun builder(): Builder { | ||
return Builder() | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
core/network/src/commonMain/kotlin/org/mifos/mobile/core/network/di/NetworkModule.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,45 @@ | ||
/* | ||
* Copyright 2024 Mifos Initiative | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md | ||
*/ | ||
package org.mifos.mobile.core.network.di | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.plugins.auth.Auth | ||
import org.koin.dsl.module | ||
import org.mifos.mobile.core.datastore.PreferencesHelper | ||
import org.mifos.mobile.core.network.DataManager | ||
import org.mifos.mobile.core.network.KtorfitClient | ||
import org.mifos.mobile.core.network.ktorHttpClient | ||
import org.mifos.mobile.core.network.utils.BaseURL | ||
import org.mifos.mobile.core.network.utils.KtorInterceptor | ||
|
||
val NetworkModule = module { | ||
|
||
single<HttpClient>(KtorClient) { | ||
val preferencesRepository = get<PreferencesHelper>() | ||
|
||
ktorHttpClient.config { | ||
install(Auth) | ||
install(KtorInterceptor) { | ||
getToken = { preferencesRepository.authToken } | ||
} | ||
} | ||
} | ||
|
||
single<KtorfitClient>(MifosClient) { | ||
KtorfitClient.builder() | ||
.httpClient(get(KtorBaseClient)) | ||
.baseURL(BaseURL().url) | ||
.build() | ||
} | ||
|
||
single { | ||
DataManager(ktorfitClient = get(MifosClient)) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
core/network/src/commonMain/kotlin/org/mifos/mobile/core/network/di/Qualifier.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,16 @@ | ||
/* | ||
* Copyright 2025 Mifos Initiative | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md | ||
*/ | ||
package org.mifos.mobile.core.network.di | ||
|
||
import org.koin.core.qualifier.named | ||
|
||
val MifosClient = named("MifosClient") | ||
val KtorClient = named("KtorClient") | ||
val KtorBaseClient = named("KtorBaseClient") |
Oops, something went wrong.