Skip to content

Instantly share code, notes, and snippets.

@navi25
Last active January 13, 2019 22:58
Show Gist options
  • Save navi25/7b32e0b875c79c8d45c51fdb7cabc42e to your computer and use it in GitHub Desktop.
Save navi25/7b32e0b875c79c8d45c51fdb7cabc42e to your computer and use it in GitHub Desktop.
//ApiFactory to create TMDB Api
object Apifactory{
//Creating Auth Interceptor to add api_key query in front of all the requests.
private val authInterceptor = Interceptor {chain->
val newUrl = chain.request().url()
.newBuilder()
.addQueryParameter("api_key", AppConstants.tmdbApiKey)
.build()
val newRequest = chain.request()
.newBuilder()
.url(newUrl)
.build()
chain.proceed(newRequest)
}
//OkhttpClient for building http request url
private val tmdbClient = OkHttpClient().newBuilder()
.addInterceptor(authInterceptor)
.build()
fun retrofit() : Retrofit = Retrofit.Builder()
.client(tmdbClient)
.baseUrl("https://api.themoviedb.org/3/")
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
val tmdbApi : TmdbApi = retrofit().create(TmdbApi::class.java)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment