Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Updating mozilla api to use ktor (#562)
Browse files Browse the repository at this point in the history
- Updating recs api to use ktor
- Cleaning up dependencies

---------

Co-authored-by: John Oberhauser <j.git-global@obez.io>
  • Loading branch information
JohnOberhauser and John Oberhauser authored May 26, 2024
1 parent ba2fccd commit df37ca7
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 75 deletions.
5 changes: 1 addition & 4 deletions core/database/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ android {
dependencies {
implementation(project(":core:model"))

implementation(libs.androidx.navigation.compose)
implementation(libs.koin.core)
implementation(libs.koin.androidx.compose)
implementation(libs.koin.android)

implementation(libs.androidx.room.runtime)
ksp(libs.androidx.room.compiler)
Expand All @@ -29,5 +27,4 @@ dependencies {
implementation(libs.kotlinx.datetime)

implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.serialization.converter)
}
4 changes: 0 additions & 4 deletions core/network/mastodon/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ dependencies {

implementation(libs.kotlinx.datetime)

implementation(libs.androidx.navigation.compose)
implementation(libs.koin.core)
implementation(libs.koin.androidx.compose)
implementation(libs.koin.android)

implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.serialization.converter)
}
11 changes: 5 additions & 6 deletions core/network/mozilla/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ android {
dependencies {
implementation(project(":core:common"))

api(libs.retrofit)

implementation(libs.square.okhttp)
implementation(libs.square.okhttp.logging)

implementation(libs.ktor.core)
implementation(libs.ktor.okhttp)
implementation(libs.ktor.content.negotiation)
implementation(libs.ktor.json)

implementation(libs.kotlinx.datetime)

implementation(libs.androidx.navigation.compose)
implementation(libs.koin.core)
implementation(libs.koin.androidx.compose)
implementation(libs.koin.android)

implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.serialization.converter)
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,68 @@
package social.firefly.core.network.mozilla

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.HttpClientEngineConfig
import io.ktor.client.engine.HttpClientEngineFactory
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.engine.okhttp.OkHttpConfig
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.core.qualifier.named
import org.koin.dsl.module
import retrofit2.Retrofit
import social.firefly.core.network.mozilla.ktor.RecommendationApiImpl
import java.util.concurrent.TimeUnit

val mozillaNetworkModule =
module {

single(named(RECCS_CLIENT)) {
OkHttpClient.Builder()
.readTimeout(OKHTTP_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(OKHTTP_TIMEOUT, TimeUnit.SECONDS)
.addNetworkInterceptor(
HttpLoggingInterceptor().apply {
level =
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor.Level.BASIC
} else {
HttpLoggingInterceptor.Level.NONE
}
},
)
.build()
}
single(
named(RECCS_SERVICE),
) {
Retrofit.Builder()
.baseUrl("https://mozilla.social/")
.client(get(qualifier = named(RECCS_CLIENT)))
.addConverterFactory(json.asConverterFactory(contentType = "application/json".toMediaType()))
.build()
val mozillaNetworkModule = module {

single<HttpClientEngineFactory<OkHttpConfig>> {
OkHttp
}

single<HttpClientEngine> {
get<HttpClientEngineFactory<OkHttpConfig>>().create {
config {
readTimeout(OKHTTP_TIMEOUT, TimeUnit.SECONDS)
connectTimeout(OKHTTP_TIMEOUT, TimeUnit.SECONDS)
}

addNetworkInterceptor(
HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) {
HttpLoggingInterceptor.Level.BASIC
} else {
HttpLoggingInterceptor.Level.NONE
}
}
)
}
single {
Retrofit.Builder()
.baseUrl("https://mozilla.social/")
.client(get(qualifier = named(AUTHORIZED_CLIENT)))
.addConverterFactory(json.asConverterFactory(contentType = "application/json".toMediaType()))
.build()
}

single<HttpClientConfig<HttpClientEngineConfig>> {
HttpClientConfig<HttpClientEngineConfig>().apply {
install(ContentNegotiation) {
json(Json {
ignoreUnknownKeys = true
})
}
expectSuccess = true
defaultRequest {
url("https://mozilla.social/")
}
}
single { get<Retrofit>(named(RECCS_SERVICE)).create(RecommendationApi::class.java) }
}

private var json: Json = Json { ignoreUnknownKeys = true }
private const val AUTHORIZED_CLIENT = "authorizedClient"
private const val RECCS_CLIENT = "reccsClient"
private const val RECCS_SERVICE = "reccsService"
single {
HttpClient(
get<HttpClientEngine>(),
get<HttpClientConfig<HttpClientEngineConfig>>(),
)
}

single<RecommendationApi> { RecommendationApiImpl(get()) }
}

private const val OKHTTP_TIMEOUT = 30L
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package social.firefly.core.network.mozilla

import retrofit2.http.GET
import retrofit2.http.Query
import social.firefly.core.network.mozilla.model.NetworkRecommendations

interface RecommendationApi {
@GET("/content-feed/Ff/v1/discover/")

suspend fun getRecommendations(
@Query("locale") locale: String,
@Query("count") count: Int = 24,
@Query("image_sizes[]") imageSizes: String = "200x",
// @Query("consumer_key") consumerKey: String,
locale: String,
count: Int = 24,
imageSizes: String = "200x",
): NetworkRecommendations
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package social.firefly.core.network.mozilla.ktor

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.http.URLProtocol
import io.ktor.http.path
import social.firefly.core.network.mozilla.RecommendationApi
import social.firefly.core.network.mozilla.model.NetworkRecommendations

class RecommendationApiImpl(
private val client: HttpClient,
) : RecommendationApi {
override suspend fun getRecommendations(
locale: String,
count: Int,
imageSizes: String
): NetworkRecommendations = client.get {
url {
protocol = URLProtocol.HTTPS
path("content-feed/Ff/v1/discover/")
parameters.apply {
append("locale", locale)
append("count", count.toString())
append("image_sizes[]", imageSizes)
}
}
}.body()
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ val mozillaUsecaseModule =
analyticsModule,
navigationModule,
)

single { GetRecommendations(get()) }
}
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version = "0.6.0" }
kotlinx-serialization-converter = { module = "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter", version = "1.0.0" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version = "1.6.3" }
ktor-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
ktor-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
Expand All @@ -100,7 +99,6 @@ mockk = { module = "io.mockk:mockk", version = "1.13.11" }
mozilla-components-service-glean = { module = "org.mozilla.components:service-glean", version = "118.0" }
protobuf-kotlin-lite = { module = "com.google.protobuf:protobuf-kotlin-lite", version.ref = "protobuf" }
protobuf-protoc = { module = "com.google.protobuf:protoc", version.ref = "protobuf" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version = "2.11.0" }
square-okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
square-okhttp-logging = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "okhttp" }
unifiedPush-androidConnector = { module = "com.github.UnifiedPush:android-connector", version = "2.4.0" }
Expand Down

0 comments on commit df37ca7

Please sign in to comment.