Call an activity from Koin module - android

I'm trying to logout user in case of a 401 response from any API call in the project.
I'm using Koin Module. Here's the code snippet of Koin Module:
single {
retrofit(get())
}
single {
client()
}
private fun retrofit(okHttpClient: OkHttpClient): Retrofit {
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl(Config.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
}
private fun client(): OkHttpClient {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
return OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.addInterceptor(object : Interceptor {
#Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request: Request = chain.request()
val response: Response = chain.proceed(request)
// todo deal with the issues the way you need to
if (response.code == 200) {
//Open LogIn Activity
}
return response
}
}).build()
}
How can I call activity from within the interceptor? Or is there any other preferred way to do this?
Any help is appreciated.

Related

baseurl must end in /

I have weather api to parse data for 10 days
All it's good , but I have problem with retrofit now , I have app crashes , my URL(with API) have / in the end.
But still don't working.
Also I have dependency injection for retrofit.
Goal is to get data from api.
Hope , that you will help me to resolve this problem.
package const
const val BASE_URL = "https://api.weatherapi.com/v1/forecast" +
".json?key=a9f9d57b6e064f16b28141346231001&q=London&days=10&aqi=no&alerts=no/" // error here
const val apikey = "a9f9d57b6e064f16b28141346231001"
const val WeatherDays = 10
interface WeatherServiceAPI {
#GET("forecast.json")
suspend fun Weatherday(
#Query("days") days : Int
) : WeatherResponse
#GET("forecast.json")
suspend fun searchcitybycoord(#Query("lat")lat:String) : List<WeatherLocationDTO>
#GET("forecast.json")
suspend fun searchingbyCity(#Query("q") name: String) : List<WeatherLocationDTO>
companion object{
operator fun invoke(
connectivityInterceptor: Interceptor
):WeatherServiceAPI{
val requestInterceptor = Interceptor{
chain -> val url = chain.request()
.url
.newBuilder()
.addQueryParameter("key", apikey)
.build()
val request = chain.request()
.newBuilder()
.url(url)
.build()
return#Interceptor chain.proceed(request)
}
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(requestInterceptor)
.addInterceptor(connectivityInterceptor)
.build()
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl("https://api.weatherapi.com/v1/") // error line
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WeatherServiceAPI::class.java)
}
}
}
#Provides
#Singleton
fun providerepository(api:WeatherServiceAPI):ForecastRepository{
return ForecastRepositoryImpl(api)
}
#Provides
#Singleton
fun provideWeatherApiService(retrofit: Retrofit) =
retrofit.create(WeatherServiceAPI::class.java)
#Provides
#Singleton
fun provideRetrofit ( okHttpClient: OkHttpClient) = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(okHttpClient)
.build()
#Provides
#Singleton
fun provideOkhttpClient(interceptor: Interceptor): OkHttpClient {
val httpBuilder = OkHttpClient.Builder().addInterceptor(interceptor)
return httpBuilder.build()
}
#Provides
#Singleton
fun provideinterceptor():Interceptor{
return Interceptor {
val request =it.request().newBuilder()
val actualRequest = request.build()
it.proceed(actualRequest)
}
}
Given what else you have in the code, your base URL should be https://api.weatherapi.com/v1/.
forecast.json comes from the #GET annotations, and the query parameters will need to come from #Query-annotated parameters to your Retrofit interface functions.

OkHttp client generates diffrent URL for retrofit

I am developing a simple app to fetch now playing movie details from https://www.themoviedb.org/ API.
This is the URL to which I need to perform the API call.
https://api.themoviedb.org/3/movie/now_playing?api_key=<<api_key>>
I am using retrofit to make the API call like this.
#GET("/movie/now_playing")
fun getNowPlayingMovies(): Single<List<MovieData>>
and I am using Base Url and API key as variables in the client class.
private val BASE_URL = "https://api.themoviedb.org/3/"
private val API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
Since the best way to add the api key as a query parameter to the URL is by using a request Interceptor , I created an intercepted to intercept the request and add the api key as a query parameter.
private fun getInterceptor() : Interceptor{
if (requestInterceptor == null){
requestInterceptor = Interceptor{
val url = it.request()
.url
.newBuilder()
.addQueryParameter("api_key" , API_KEY)
.build()
val request = it.request()
.newBuilder()
.url(url)
.build()
return#Interceptor it.proceed(request)
}
}
return requestInterceptor
}
Then added this interceptor along with logging interceptor to the OkHttp Client.
private fun getOkHttpClient() : OkHttpClient{
var httLog : HttpLoggingInterceptor = HttpLoggingInterceptor()
httLog.setLevel(HttpLoggingInterceptor.Level.BODY)
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(getInterceptor()).addInterceptor(httLog)
.connectTimeout(60 , TimeUnit.SECONDS)
.build()
return okHttpClient
}
And then build the retrofit client. In which I add the base URL.
var retrofit : Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getOkHttpClient())
.addConverterFactory(getGsonConverterFactory())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
The problem is ,
the entire URL made using the BASE_URL and the api as a Query Paramter , Should be like this
https://api.themoviedb.org/3/movie/now_playing?api_key=**********
But in the logging interceptor I am getting this as the url
https://api.themoviedb.org/movie/now_playing?api_key=**********
which has a different BASE_URL than the one I provided in retrofit. It is giving me a 404 error.
I cannot seem to figure out why this is causing.
I logged on different places and I believe the request Interceptor is intercepting a different URL than the BASE_URL.
This is my entire codebase, it would be very helpful if there are any other improvements in the code , I am new to android development and Kotlin. Thank You.
interface MoviesApiServiceRx {
#GET("/movie/now_playing")
fun getNowPlayingMovies(): Single<List<MovieData>>
}
class MoviesRetrofitClient() {
private val BASE_URL = "https://api.themoviedb.org/3/"
private val API_KEY = "9a976526fce8c29aaa35eb4a1e654d3c"
private var moviesApiServiceRx : MoviesApiServiceRx
private var gsonConverterFactory : GsonConverterFactory
private var requestInterceptor : Interceptor
init {
moviesApiServiceRx = getMoviesApiServiceRx()
gsonConverterFactory = getGsonConverterFactory()
requestInterceptor = getInterceptor()
}
private fun getInterceptor() : Interceptor{
if (requestInterceptor == null){
requestInterceptor = Interceptor{
val url = it.request()
.url
.newBuilder()
.addQueryParameter("api_key" , API_KEY)
.build()
val request = it.request()
.newBuilder()
.url(url)
.build()
return#Interceptor it.proceed(request)
}
}
return requestInterceptor
}
private fun getGsonConverterFactory() : GsonConverterFactory{
if (gsonConverterFactory == null){
gsonConverterFactory = GsonConverterFactory.create();
}
return gsonConverterFactory
}
private fun getOkHttpClient() : OkHttpClient{
var httLog : HttpLoggingInterceptor = HttpLoggingInterceptor()
httLog.setLevel(HttpLoggingInterceptor.Level.BODY)
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(getInterceptor()).addInterceptor(httLog)
.connectTimeout(60 , TimeUnit.SECONDS)
.build()
return okHttpClient
}
private fun getMoviesApiServiceRx() : MoviesApiServiceRx{
if (moviesApiServiceRx == null){
var retrofit : Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getOkHttpClient())
.addConverterFactory(getGsonConverterFactory())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
moviesApiServiceRx = retrofit.create(MoviesApiServiceRx::class.java)
}
return moviesApiServiceRx
}
fun getNowPlayingMovies(): Single<List<MovieData>> {
return getMoviesApiServiceRx().
getNowPlayingMovies()
}
}
Change
#GET("/movie/now_playing")
to
#GET("movie/now_playing")
The / at the beginning refers to the root so it will replace whatever there is after the service name.
two options
interface MoviesApiServiceRx {
#GET("/3/movie/now_playing")
fun getNowPlayingMovies(): Single<List<MovieData>>
}
private val BASE_URL = "https://api.themoviedb.org/"
or
interface MoviesApiServiceRx {
#GET("movie/now_playing")
fun getNowPlayingMovies(): Single<List<MovieData>>
}
private val BASE_URL = "https://api.themoviedb.org/3/"

How can i reinitialise the Singleton Component in Hilt dagger?

I have a retrofit module in my project, Before login i want to use retrofit without headers, But after login i want to use retrofit with headers using Hilt Dagger. How can i do this?
#Module
#InstallIn(SingletonComponent::class)
object RetrofitDi {
#Provides
fun getBasePath(): String {
return "http://abcd.com/"
}
#Provides
fun providesLoggingInterceptor(): HttpLoggingInterceptor {
return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
}
#Provides
fun providesOkHttpClients(#ApplicationContext context: Context, sharedPreference: SharedPreference, httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
val okhttpClient = OkHttpClient.Builder()
okhttpClient.addInterceptor(httpLoggingInterceptor)
okhttpClient.callTimeout(60, TimeUnit.SECONDS)
okhttpClient.connectTimeout(60, TimeUnit.SECONDS)
okhttpClient.writeTimeout(60, TimeUnit.SECONDS)
okhttpClient.readTimeout(60, TimeUnit.SECONDS)
val token = sharedPreference.getStringData(SharedPreference.AUTH_KEY)
val user_id = sharedPreference.getStringData(SharedPreference.USER_ID)
if (BuildConfig.DEBUG) {
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS)
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
okhttpClient.addInterceptor(interceptor)
okhttpClient.addInterceptor(Interceptor { chain ->
val response = chain.proceed(chain.request())
if (!response.isSuccessful) {
when (response.code) {
CommonUtils.ALREADY_LOGGED_IN -> {
sharedPreference.setBoolean(SharedPreference.IS_LOGGED_IN, false)
sharedPreference.clear()
context.getCacheDir().delete()
val intent = Intent(context, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
}
}
response
})
}
okhttpClient.addInterceptor(Interceptor { chain: Interceptor.Chain ->
val builder1 = chain.request().newBuilder()
var request: Request? = null
if (!token.isEmpty()) {
builder1.addHeader("auth_key", "" + token)
}
if (!user_id.isEmpty()) {
builder1.addHeader("user_id", "" + user_id)
}
request = builder1.build()
chain.proceed(request)
})
return okhttpClient.build()
}
#Provides
fun providesGSONConvertorFactory(): Converter.Factory {
return GsonConverterFactory.create()
}
#Provides
fun providesRetrofit(baseUrl: String, convertor: Converter.Factory, okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(convertor).client(okHttpClient).build()
}
#Provides
fun providesApiService(retrofit: Retrofit): ApiService {
return retrofit.create(ApiService::class.java)
}
}
Singleton instance created once in hilt-dagger but token and user_id will be available after login. After login i will need new okhttpclient. I did it without DI. But don't know how to deal with hilt-dagger.
Better way to do this is to have your API service methods annotated with #Headers("Token-required") for the APIs which requires token. Then in your interceptor method check for this header as:
if (request.header("Token-required") != null) {
request = request.newBuilder()
.addHeader("token", "your token value")
.build()
}

How to Log, the URL I am accessing retrofit

I am using retrofit to communicate with the BE and I want to the log the URL i am hitting and the body I am sending and the response I am getting (like 401, 404).
This is to know if I am hitting the right url what is the header, body and the actual URL to verify I am sending all the right info required
I implemented HttpLoggingInterceptor but that does not log it, or I am logging it the right way
can you suggest please.
object RetrofitBuilder {
private const val BASE_URL = "https://xxxx.xxxxxx.com/"
private fun getRetrofit(): Retrofit {
var interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build() //Doesn't require the adapter
}
val apiService: ApiService = getRetrofit().create(ApiService::class.java)
}
AppService
#POST
suspend fun postData(#Url url: String, #Body Data: Data, #Header("Autzn") authHeader: String)
ApiHelper
//NOTE: here i am using the different URL as but not the baseUrl defined on the top
apiService.postData("https://abcd.com/msg/oauth2/123456", Data,
"token")
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
Please suggest how to log the url info
thanks
R
just give HttpLoggingInterceptor.Logger interface.
logging interceptor should be like this.
val loggingInterceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Timber.tag("retrofit").d(message)
}
})
This is How I'm logging url in logs
val retrofitApi: RetrofitInterface
get() {
if (retrofitInterface == null) {
val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BODY
val okHttpClient = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(logger).build()
val retrofit = Retrofit.Builder()
.baseUrl(REST_HOST)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder()
.setLenient()
.create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
retrofitInterface = retrofit.create(RetrofitInterface::class.java)
}
return this.retrofitInterface!!
}
I suggest you to use HttpLoggingInterceptor with log level, logging can be heavy task especially with images(printing raw bytes), and with big images(40mb+) can throw OOM exception.
So use logging for debug purpose.
public HttpLoggingInterceptor loggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor
= new HttpLoggingInterceptor(message -> Log.d(TAG, message));
loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
return loggingInterceptor;
}

Can not change retrofit base url more than once with reflection

I am using reflection for change retrofit base url dynamically but I can change once in activity, after that I can not change it. Why it is not working when I try to change multiple times.
Set dynamic base url using Retrofit 2.0 and Dagger 2
I do not know the domain I just know the end points, I get the domain adress from user.
Just use different modules and api for different URLs. And then use #Named annotation to determine the correct api.
Api module with HOST url
#Module
class ApiModule {
#Provides
#Singleton
#Named("Interceptor")
fun provideInterceptor(context: Context, sharedPrefsStorage: SharedPrefsStorage, #Named("ApiAuth") apiAuth: ApiAuth,realmProvider: DbProvider<Realm>): Interceptor {
return ApiInterceptor(context, sharedPrefsStorage, apiAuth,realmProvider)
}
#Provides
#Singleton
#Named("HttpClient")
fun provideOkHttpClient(#Named("Interceptor") interceptor: Interceptor): OkHttpClient {
val builder = OkHttpClient.Builder()
builder.addInterceptor(interceptor)
builder.readTimeout(30, TimeUnit.SECONDS)
builder.connectTimeout(30, TimeUnit.SECONDS)
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
builder.addInterceptor(loggingInterceptor)
return builder.build()
}
#Provides
#Singleton
#Named("Retrofit")
fun provideRetrofitBuilder(#Named("HttpClient") okHttpClient: OkHttpClient): Retrofit.Builder {
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(
GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.baseUrl(BuildConfig.HOST)
}
#Provides
#Singleton
#Named("Api")
fun provideApi(#Named("Retrofit") builder: Retrofit.Builder): Api {
return builder.build().create<Api>(Api::class.java)
}
companion object {
val TAG = ApiModule::class.java.simpleName
}
}
ApiAuthModule with HOST_2 url
#Module
class ApiAuthModule {
#Provides
#Singleton
#Named("InterceptorAuth")
fun provideInterceptor(): Interceptor {
return ApiAuthInterceptor()
}
#Provides
#Singleton
#Named("HttpClientAuth")
fun provideOkHttpClient(#Named("InterceptorAuth") interceptor: Interceptor): OkHttpClient {
val builder = OkHttpClient.Builder()
builder.addInterceptor(interceptor)
builder.readTimeout(5, TimeUnit.SECONDS)
builder.connectTimeout(5, TimeUnit.SECONDS)
if (BuildConfig.DEBUG) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
builder.addInterceptor(loggingInterceptor)
}
return builder.build()
}
#Provides
#Singleton
#Named("RetrofitAuth")
fun provideRetrofitBuilder(#Named("HttpClientAuth") okHttpClient: OkHttpClient): Retrofit.Builder {
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(
GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.baseUrl(BuildConfig.HOST_2)
}
#Provides
#Singleton
#Named("ApiAuth")
fun provideApi(#Named("RetrofitAuth") builder: Retrofit.Builder): ApiAuth {
return builder.build().create<ApiAuth>(ApiAuth::class.java)
}
companion object {
val TAG = ApiModule::class.java.simpleName
}
}
Usage in AppModule:
#Provides
#Singleton
fun worksRepository(#Named("Api") api: Api, #Named("ApiAuth") api2: ApiAuth): IWorksRepository {
return WorksRepository(api, api2)
}

Categories

Resources