Skip to content

Use ktor and kotlinx serialization #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rahulsainani
Copy link
Collaborator

What?

On top of #125
– Use ktor instead of retrofit and kotlinx serialization instead of gson for KMP support
– Update MovieRepository to use ktor
– Add ApiResponse to model different api responses to objects, this encourages mapping exceptions to objects at the edge of the system
– Add safeRequest utility that maps to ApiResponse
– Test updates

@rahulsainani rahulsainani changed the base branch from main to improvement/official-kotlin-code-style April 16, 2025 07:11
@rahulsainani rahulsainani changed the base branch from improvement/official-kotlin-code-style to main April 16, 2025 07:11
@rahulsainani rahulsainani marked this pull request as ready for review April 16, 2025 07:11
@rahulsainani rahulsainani force-pushed the use-ktor-and-kotlinx-serialization branch 3 times, most recently from a570505 to 10a10e6 Compare April 16, 2025 11:03
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the repository to use ktor for network requests and kotlinx serialization for JSON parsing in support of Kotlin Multiplatform. Key changes include replacing Retrofit calls with ktor’s safeRequest utility, updating the MoviesRepository and MoviesService implementations accordingly, and reorganizing dependency injection modules.

Reviewed Changes

Copilot reviewed 23 out of 24 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
MoviesRepositoryTest.kt Updated tests using runTest and coEvery with new API response handling.
HttpClientXTest.kt Added tests for the new safeRequest implementation and ApiResponse mapping.
Movies.kt Moved to the new DI package structure.
MoviesService.kt Replaced Retrofit with ktor client calls and safeRequest; updated URL building and content type settings.
MoviesRepository.kt Switched repository implementation to use suspend functions and ApiResponse toEither mapping.
Remaining files Adjusted DI modules, removed unused Retrofit/Retrofit-based APIs and updated package naming.
Files not reviewed (1)
  • app/build.gradle.kts: Language not supported
Comments suppressed due to low confidence (1)

app/src/main/kotlin/com/fernandocejas/sample/features/movies/data/MoviesService.kt:32

  • Verify that using ContentType.Text.Plain matches the API's expected response format; if the endpoint returns JSON, consider using ContentType.Application.Json.
contentType(ContentType.Text.Plain)

@rahulsainani rahulsainani self-assigned this Apr 16, 2025
@rahulsainani rahulsainani force-pushed the use-ktor-and-kotlinx-serialization branch from 10a10e6 to d3456ce Compare April 16, 2025 11:07
Copy link
Owner

@android10 android10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changeset @rahulsainani. Overall seems great but before merging, would be great to discuss the following topics:

  • What is a feature and what is not?
  • The role of the core module and the dependency rule.
  • DI should be at core level and each feature (functionality) brings up its own dependencies that are attached to the DI graph.

Let me know what you think ;) 🚀

@@ -99,7 +100,6 @@ dependencies {
implementation(libs.ktor.client.content.negotiation)
implementation(libs.kotlinx.serialization.json)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.converter.gson)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this! For a long time wanted to get rid of gson :)

import com.fernandocejas.sample.features.movies.di.moviesFeature

fun allFeatures() = listOf(
networkFeature(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not make network and navigation a feature. Unless we have a good reason for it. Think of features as user features. Networking and Navigation belong to core, which is the module shared across features.

Happy to read your thoughts :)

@@ -83,4 +87,10 @@ class Navigator(private val authenticator: Authenticator) {
class Extras(val transitionSharedElement: View)
}


// temporary solution to compile till Navigator is deleted
fun navigationFeature() = object : Feature {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the comment above :)

import org.koin.dsl.module
import io.ktor.client.plugins.logging.Logger as KtorLogger

fun networkFeature() = object : Feature {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this might not become a feature, the DI part should be rethought then. If do not see any issues to continue having a DI core module that includes all the cross cutting dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants