This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating mozilla api to use ktor (#562)
- Updating recs api to use ktor - Cleaning up dependencies --------- Co-authored-by: John Oberhauser <j.git-global@obez.io>
- Loading branch information
1 parent
ba2fccd
commit df37ca7
Showing
9 changed files
with
95 additions
and
75 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
99 changes: 56 additions & 43 deletions
99
...twork/mozilla/src/main/kotlin/social/firefly/core/network/mozilla/MozillaNetworkModule.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 |
---|---|---|
@@ -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 |
11 changes: 4 additions & 7 deletions
11
.../network/mozilla/src/main/kotlin/social/firefly/core/network/mozilla/RecommendationApi.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 |
---|---|---|
@@ -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 | ||
} |
29 changes: 29 additions & 0 deletions
29
...mozilla/src/main/kotlin/social/firefly/core/network/mozilla/ktor/RecommendationApiImpl.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,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() | ||
} |
7 changes: 0 additions & 7 deletions
7
...usecase/mozilla/src/main/kotlin/social/firefly/core/usecase/mozilla/GetRecommendations.kt
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -14,6 +14,4 @@ val mozillaUsecaseModule = | |
analyticsModule, | ||
navigationModule, | ||
) | ||
|
||
single { GetRecommendations(get()) } | ||
} |
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