Skip to content
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

Migrate network calls from Retrofit to Ktor #19

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.konan.properties.Properties

plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/com/alexmumo/common/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.alexmumo.common

object Constants {
const val BASE_URL = "https://newsapi.org/v2/"
const val NEWS_API_KEY = "f982987c31514f529685997518bdade9"
const val NEWS_API_KEY = ""
const val PAGE_SIZE = 100
const val PAGE = 1
const val COUNTRY_CODE = "us"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ import com.alexmumo.common.Constants.NEWS_API_KEY
import com.alexmumo.common.Constants.PAGE
import com.alexmumo.common.Constants.PAGE_SIZE
import com.alexmumo.network.response.NewsResponse
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query

/*
*
* */
interface NewsApi {
interface NewsService {
@GET("everything")
suspend fun searchNews(
@Query("q") q: String,
@Query("pageSize") pageSize: Int = PAGE_SIZE,
@Query("page") page: Int = PAGE,
@Query("apiKey") apiKey: String = NEWS_API_KEY
): Response<NewsResponse>
): NewsResponse

@GET("top-headlines")
suspend fun getTopHeadLines(
Expand All @@ -42,5 +38,5 @@ interface NewsApi {
@Query("pageSize") pageSize: Int = PAGE_SIZE,
@Query("page") page: Int = PAGE,
@Query("apiKey") apiKey: String = NEWS_API_KEY
): Response<NewsResponse>
): NewsResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 News-App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alexmumo.network.api

import com.alexmumo.network.response.NewsResponse
import io.ktor.client.HttpClient
import io.ktor.client.request.get

class NewsApiImpl(private val httpClient: HttpClient) : NewsService {
override suspend fun searchNews(
q: String,
pageSize: Int,
page: Int,
apiKey: String
): NewsResponse {
TODO()
}

override suspend fun getTopHeadLines(
country: String,
category: String,
pageSize: Int,
page: Int,
apiKey: String
): NewsResponse {
TODO("Not yet implemented")
}
}
9 changes: 8 additions & 1 deletion core/network/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.konan.properties.Properties

plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
Expand All @@ -10,7 +12,9 @@ android {
compileSdk = 33

defaultConfig {
// buildConfigField("String", "APIKEY", localProperties.getProperty("APIKEY"))
val properties = Properties()
properties.load(project.rootProject.file("local.properties").inputStream())
//buildConfigField("String", "API_KEY", properties.getProperty("API_KEY"))
minSdk = 26
targetSdk = 33

Expand Down Expand Up @@ -48,6 +52,9 @@ dependencies {
implementation(libs.android.appcompat)
testImplementation(libs.junit)

// Ktor
implementation(libs.bundles.ktor)

// Coroutine
implementation(libs.bundles.coroutine)

Expand Down
42 changes: 42 additions & 0 deletions core/network/src/main/java/com/alexmumo/network/api/NewsService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 News-App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alexmumo.network.api

import com.alexmumo.common.Constants.NEWS_API_KEY
import com.alexmumo.common.Constants.PAGE
import com.alexmumo.common.Constants.PAGE_SIZE
import com.alexmumo.network.response.NewsResponse
import retrofit2.http.GET
import retrofit2.http.Query

interface NewsService {
@GET("everything")
suspend fun searchNews(
@Query("q") q: String,
@Query("pageSize") pageSize: Int = PAGE_SIZE,
@Query("page") page: Int = PAGE,
@Query("apiKey") apiKey: String = NEWS_API_KEY
): NewsResponse

@GET("top-headlines")
suspend fun getTopHeadLines(
@Query("country") country: String,
@Query("category") category: String,
@Query("pageSize") pageSize: Int = PAGE_SIZE,
@Query("page") page: Int = PAGE,
@Query("apiKey") apiKey: String = NEWS_API_KEY
): NewsResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 News-App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alexmumo.network.api

import com.alexmumo.network.response.NewsResponse
import io.ktor.client.HttpClient
import io.ktor.client.request.get

class NewsApiImpl(private val httpClient: HttpClient) : NewsService {
override suspend fun searchNews(
q: String,
pageSize: Int,
page: Int,
apiKey: String
): NewsResponse {
TODO()
}

override suspend fun getTopHeadLines(
country: String,
category: String,
pageSize: Int,
page: Int,
apiKey: String
): NewsResponse {
TODO("Not yet implemented")
}
}
56 changes: 23 additions & 33 deletions core/network/src/main/java/com/alexmumo/network/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,42 @@
package com.alexmumo.network.di

import com.alexmumo.common.Constants.BASE_URL
import com.alexmumo.network.BuildConfig
import com.alexmumo.network.api.NewsApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.DefaultRequest
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logging
import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.serialization.json.Json
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
@Singleton
fun providesNewsApi(okHttpClient: OkHttpClient): NewsApi {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(NewsApi::class.java)
}

@Provides
@Singleton
fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor {
val level = if (BuildConfig.DEBUG) {
HttpLoggingInterceptor.Level.BASIC
} else HttpLoggingInterceptor.Level.NONE
return HttpLoggingInterceptor().also {
it.level = level
}
}
fun provideDispatcher(): CoroutineDispatcher = Dispatchers.Default

@Provides
@Singleton
fun provideOkhttpInterceptor(httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
val client = OkHttpClient.Builder()
.callTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.addInterceptor(httpLoggingInterceptor)
return client.build()
fun provideHttpClient(): HttpClient {
return HttpClient(Android) {
install(Logging) {
level = LogLevel.ALL
}
install(DefaultRequest) {
url(BASE_URL)
}
install(ContentNegotiation) {
json(Json)
}
}
}
}
22 changes: 22 additions & 0 deletions core/network/src/main/java/com/alexmumo/network/util/Resource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 News-App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alexmumo.network.util

sealed class Resource<T>(val data: T? = null, val error: String? = null) {
class Success<T>(data: T) : Resource<T>(data = data)
class Error<T>(error: String) : Resource<T>(error = error)
class Loading<T> : Resource<T>()
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ kotlin.code.style=official
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.enableJetifier=true
android.enableJetifier=true
12 changes: 12 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ chunker = "4.0.0"
mock = "1.13.8"
hilt = "2.44"
hilt-compose = "1.0.0"
ktor-version = "2.3.8"

[plugins]
android-application = {id = "com.android.application", version.ref = "androidGradle"}
Expand Down Expand Up @@ -128,6 +129,16 @@ androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
splash-screen = {module = "androidx.core:core-splashscreen", version.ref ="splash"}

# Ktor
ktor-core = {module = "io.ktor:ktor-client-core", version.ref = "ktor-version"}
ktor-cio = {module = "io.ktor:ktor-client-cio", version.ref = "ktor-version"}
ktor-mock = {module = "io.ktor:ktor-client-mock", version.ref = "ktor-version"}
ktor-android = {module = "io.ktor:ktor-client-android", version.ref = "ktor-version"}
ktor-logging = {module = "io.ktor:ktor-client-logging", version.ref = "ktor-version"}
ktor-serialization = {module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor-version"}
content-negotiation = {module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor-version"}


[bundles]
compose = ["coil-compose", "compose-activity", "compose-navigation", "compose-material", "compose-navigation-testing", "compose-ui", "compose-ui-test-junit", "compose-ui-test-manifest", "compose-ui-tooling", "compose-ui-tooling-preview", "compose-ui-util"]
room = ["room-compiler", "room-ktx", "room-paging", "room-runtime", "room-testing"]
Expand All @@ -138,6 +149,7 @@ coroutines = ["coroutines-test", "coroutines-android", "coroutines-core"]
paging = ["paging-compose", "paging-runtime"]
coroutine = ["coroutines-core","coroutines-android", "coroutines-test"]
networking = ["retrofit-gson", "retrofit-version", "okhttp3-version", "okhttp-interceptor"]
ktor = ["ktor-cio", "ktor-core", "ktor-mock", "ktor-android", "ktor-logging", "ktor-serialization", "content-negotiation"]



Expand Down
Loading