Skip to content

Commit

Permalink
Feat: [:core:network] - Migrated to KMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagarjuna0033 committed Jan 14, 2025
1 parent 7520dd3 commit ab30694
Show file tree
Hide file tree
Showing 39 changed files with 796 additions and 678 deletions.
1 change: 0 additions & 1 deletion androidApp/dependencies/prodDebugRuntimeClasspath.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ io.ktor:ktor-websocket-serialization:3.0.1
io.ktor:ktor-websockets-jvm:3.0.1
io.ktor:ktor-websockets:3.0.1
io.michaelrocks:libphonenumber-android:8.13.35
io.reactivex.rxjava2:rxjava:2.2.21
jakarta.inject:jakarta.inject-api:2.0.1
javax.inject:javax.inject:1
net.bytebuddy:byte-buddy-agent:1.14.8
Expand Down
2 changes: 2 additions & 0 deletions androidApp/dependencies/prodReleaseRuntimeClasspath.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ com.squareup.okhttp3:okhttp:4.12.0
com.squareup.okio:okio-jvm:3.9.1
com.squareup.okio:okio:3.9.1
com.squareup.retrofit2:adapter-rxjava2:2.11.0
com.squareup.okio:okio-jvm:3.6.0
com.squareup.okio:okio:3.6.0
com.squareup.retrofit2:converter-gson:2.11.0
com.squareup.retrofit2:retrofit:2.11.0
dev.chrisbanes.snapper:snapper:0.2.2
Expand Down
4 changes: 2 additions & 2 deletions core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ android {

dependencies {
api(projects.core.common)
api(projects.core.model)
api(projects.core.network)
// api(projects.core.model)
// api(projects.core.network)
api(projects.core.database)
api(projects.core.datastore)

Expand Down
80 changes: 60 additions & 20 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,77 @@
*
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md
*/

plugins {
alias(libs.plugins.mifos.android.library)
alias(libs.plugins.mifos.android.hilt)
alias(libs.plugins.mifos.kmp.library)
alias(libs.plugins.ktorfit)
id("kotlinx-serialization")
id("com.google.devtools.ksp")
}

android {
namespace = "org.mifos.mobile.core.network"
defaultConfig {
consumerProguardFiles("consumer-rules.pro")
}
testOptions {
unitTests {
isReturnDefaultValues = true
isIncludeAndroidResources = true
}
}
}

dependencies {
api(projects.core.common)
api(projects.core.model)
api(projects.core.datastore)
kotlin {
sourceSets {
commonMain.dependencies {
// api(projects.core.common)
implementation(projects.core.model)
// implementation(projects.core.datastore)
implementation(libs.kotlinx.serialization.json)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.json)
implementation(libs.ktor.client.logging)
implementation(libs.ktor.client.serialization)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.client.auth)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.ktorfit.lib)
implementation(libs.squareup.okio)
// TODO remove this dependency from here after common module successfully migrated to
// KMP
api(libs.kermit.logging)
}
androidMain.dependencies {
implementation(libs.ktor.client.okhttp)
implementation(libs.koin.android)
}

//Square dependencies
implementation(libs.squareup.retrofit2) {
// exclude Retrofit’s OkHttp peer-dependency module and define your own module import
exclude(module = "okhttp")
}
implementation(libs.squareup.retrofit.adapter.rxjava)
implementation(libs.squareup.retrofit.converter.gson)
implementation(libs.squareup.logging.interceptor)
implementation(libs.squareup.okhttp)
nativeMain.dependencies {
implementation(libs.ktor.client.darwin)
}

implementation(libs.jetbrains.kotlin.jdk7)
val desktopMain by getting {
dependencies {
implementation(libs.ktor.client.okhttp)
}
}
jsMain.dependencies {
implementation(libs.ktor.client.js)
}
wasmJsMain.dependencies {
implementation(libs.ktor.client.js)
}
}
}

testImplementation(libs.junit)
testImplementation(libs.mockito.core)
implementation(libs.mockito.core)
}
dependencies {
add("kspCommonMainMetadata", libs.ktorfit.ksp)
add("kspAndroid", libs.ktorfit.ksp)
add("kspJs", libs.ktorfit.ksp)
add("kspWasmJs", libs.ktorfit.ksp)
add("kspDesktop", libs.ktorfit.ksp)
add("kspIosX64", libs.ktorfit.ksp)
add("kspIosArm64", libs.ktorfit.ksp)
add("kspIosSimulatorArm64", libs.ktorfit.ksp)
}
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
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import org.mifos.mobile.core.network.services.RegistrationService
import org.mifos.mobile.core.network.services.SavingAccountsListService
import org.mifos.mobile.core.network.services.ThirdPartyTransferService
import org.mifos.mobile.core.network.services.UserDetailsService
import javax.inject.Inject

class BaseApiManager @Inject constructor(
class BaseApiManager(
private val authenticationService: AuthenticationService,
private val clientsService: ClientService,
private val savingAccountsListService: SavingAccountsListService,
Expand Down
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 }
}
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
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()
}
}
}
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))
}
}
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")
Loading

0 comments on commit ab30694

Please sign in to comment.