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

Interceptor added to retrofit client #26

Merged
merged 5 commits into from
Dec 1, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.filmtime.data.network.adapter.NetworkCallAdapterFactory
import io.filmtime.data.network.annotation.TmdbApi
import io.filmtime.data.network.interceptor.TmdbApiKeyInterceptor
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.OkHttpClient
import retrofit2.CallAdapter
import retrofit2.Retrofit
import javax.inject.Singleton
Expand All @@ -24,16 +28,32 @@ object NetworkModule {
}
}

@TmdbApi
@Provides
@Singleton
fun providesTmdbInterceptor(): Interceptor = TmdbApiKeyInterceptor()

@Provides
@Singleton
fun providesTmdbOkHttpClient(@TmdbApi interceptor: Interceptor): OkHttpClient {
val builder = OkHttpClient.Builder()
builder.addInterceptor(interceptor)

return builder.build()
}

@Provides
@Singleton
fun providesRetrofit(
json: Json,
networkCallAdapterFactory: CallAdapter.Factory,
okHttpClient: OkHttpClient,
): Retrofit {
return Retrofit.Builder()
.baseUrl("https://api.themoviedb.org/3/")
.addConverterFactory(json.asConverterFactory(MediaType.get("application/json")))
.addCallAdapterFactory(networkCallAdapterFactory)
.client(okHttpClient)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package io.filmtime.data.network
import io.filmtime.data.network.adapter.NetworkResponse
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface TmdbMoviesService {

@GET("movie/{movie_id}")
suspend fun getMovieDetails(
@Path("movie_id") movieId: Int,
@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY,
): NetworkResponse<TmdbMovieDetailsResponse, TmdbErrorResponse>

@GET("trending/movie/{list_type}")
suspend fun getMovieList(
@Path("list_type") listType: String,
@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY,
): NetworkResponse<TmdbVideoListResponse, TmdbErrorResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package io.filmtime.data.network
import io.filmtime.data.network.adapter.NetworkResponse
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface TmdbShowsService {

@GET("tv/{series_id})")
suspend fun getShowDetails(
@Path("series_id") seriesId: Int,
@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY,
): NetworkResponse<TmdbShowDetailsResponse, TmdbErrorResponse>

@GET("trending/tv/{time_window}")
suspend fun getTrendingShows(
@Path("time_window") timeWindow: String,
@Query("api_key") apiKey: String = BuildConfig.TMDB_API_KEY,
): NetworkResponse<TmdbShowListResponse, TmdbErrorResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.filmtime.data.network.annotation

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class TmdbApi()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.filmtime.data.network.interceptor

import io.filmtime.data.network.BuildConfig
import okhttp3.Interceptor
import okhttp3.Response

class TmdbApiKeyInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
val originalHttpUrl = chain.request().url()

val newHttpUrl = originalHttpUrl
.newBuilder()
.addQueryParameter("api_key", BuildConfig.TMDB_API_KEY)
.build()

val requestBuilder = originalRequest
.newBuilder()
.url(newHttpUrl)

val newRequest = requestBuilder.build()
return chain.proceed(newRequest)
}
}