baseurl must end in / - android

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.

Related

Setup OkHttp Proxy at runtime when using Hilt

Hilt network module is setup like below:
#Module
#InstallIn(SingletonComponent::class)
class NetworkModule {
private val TIMEOUT_UNIT = TimeUnit.MILLISECONDS
private val TIMEOUT_MILLIS: Long = 10000
#Provides
fun provideApi(retrofit: Retrofit): ApiService =
retrofit.create(ApiService::class.java)
#Singleton
#Provides
fun provideRetrofit(okHttpClient: OkHttpClient, converter: Converter.Factory): Retrofit =
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(converter)
.build()
#Provides
fun provideOkHttpClient(
loggingInterceptor: HttpLoggingInterceptor,
): OkHttpClient {
val builder = OkHttpClient.Builder()
builder.retryOnConnectionFailure(true)
builder.connectTimeout(3 * TIMEOUT_MILLIS, TIMEOUT_UNIT)
builder.readTimeout(6 * TIMEOUT_MILLIS, TIMEOUT_UNIT)
builder.writeTimeout(12 * TIMEOUT_MILLIS, TIMEOUT_UNIT)
builder.addInterceptor(loggingInterceptor)
if (condition == true) {
builder.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved("localhost", HTTP_PROXY_PORT)))
}
return builder.build()
}
#Provides
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
val loggingInterceptor = HttpLoggingInterceptor()
if (BuildConfig::DEBUG.get()) {
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
} else {
loggingInterceptor.level = HttpLoggingInterceptor.Level.NONE
}
return loggingInterceptor
}
#Provides
fun provideConverterFactory(): Converter.Factory {
return GsonConverterFactory.create()
}
#Provides
fun providesGson(): Gson {
return Gson()
}
}
I want to customize the retrofit instance at runtime with or without the proxy according to some condition. How do I do that?
Koin had unloadModule option for reinitializing modules, does Hilt has any similar feature or behaviour?

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 to solve cyclic dependencies in Dagger (Hilt)

I want to refresh my token using an intercepter but my interceptor needs an API service to make API calls. I am stuck in a dependency cycle.
Here is my ApplicationModule class:
#Module
#InstallIn(ApplicationComponent::class)
class ApplicationModule {
#Provides
fun providerBaseUrl() = AppConstants.BASE_URL
#Provides
#Singleton
fun provideOkHttpClient(authInterceptor: AuthInterceptor,
networkInterceptor: NetworkInterceptor) = if (BuildConfig.DEBUG) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
OkHttpClient.Builder()
.addInterceptor(authInterceptor)
.addInterceptor(loggingInterceptor)
.addInterceptor(networkInterceptor)
// .addInterceptor(refreshTokenInterceptor) // I want to put my interceptor here
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.build()
} else OkHttpClient
.Builder()
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.addInterceptor(authInterceptor)
.addInterceptor(networkInterceptor)
.build()
#Provides
#Singleton
fun provideRetrofit(
okHttpClient: OkHttpClient,
BASE_URL: String
): Retrofit =
Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(BASE_URL)
.client(okHttpClient)
.build()
#Provides
#Singleton
fun provideApiService(retrofit: Retrofit): WebApi = retrofit.create(WebApi::class.java)
#Provides
#Singleton // this is my provider
fun provideRefreshTokenService(webApi: WebApi): RefreshTokenInterceptor {
return RefreshTokenInterceptor(webApi)
}
#Provides
fun provideAuthInterceptor(): AuthInterceptor {
return AuthInterceptor()
}
#Provides
fun provideNetWorkInterceptor(): NetworkInterceptor {
return NetworkInterceptor()
}
}
And this is my RefreshTokenInterceptor:
class RefreshTokenInterceptor #Inject constructor(webApi: WebApi) : Interceptor {
var api = webApi
override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()
val updatedToken = getUpdatedToken(webApi = api)
requestBuilder.header("Authorization", updatedToken)
return chain.proceed(requestBuilder.build())
}
private fun getUpdatedToken(webApi: WebApi): String {
GlobalScope.launch {
val authTokenResponse = webApi.refreshToken()
val newToken = "${authTokenResponse.body()!!.data.token}"
SharedPref.getInstance(AppController.applicationContext()).setUserToken(newToken)
}
return SharedPref.getInstance(AppController.applicationContext()).getUserToken
}
}
I was facing a similar issue and found a solution. You can wrap the api instance in your Token Interceptor with Lazy interface. This will solve your cyclic dependency issue.
Here is a code snippet.
class RefreshTokenInterceptor #Inject constructor(private val webApi: Lazy<WebApi>) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()
val updatedToken = getUpdatedToken(webApi = api.get())
requestBuilder.header("Authorization", updatedToken)
return chain.proceed(requestBuilder.build())
}
private fun getUpdatedToken(webApi: WebApi): String {
GlobalScope.launch {
val authTokenResponse = webApi.refreshToken()
val newToken = "${authTokenResponse.body()!!.data.token}"
SharedPref.getInstance(AppController.applicationContext()).setUserToken(newToken)
}
return SharedPref.getInstance(AppController.applicationContext()).getUserToken
}
}
Look at this example:
#Provides
#Singleton
fun provideDatabase(
#ApplicationContext context: Context,
#RoomCreateCallback callback: RoomDatabase.Callback
) = Room.databaseBuilder(context, TaskDatabase::class.java, Constants.TaskKeys.TASK_DATABASE)
.fallbackToDestructiveMigration()
.addCallback(callback)
.build()
#Provides
fun provideTaskDao(taskDatabase: TaskDatabase) = taskDatabase.taskDao()
#Provides
#Singleton
#RoomCreateCallback
fun provideDatabaseCreateCallback(
taskDatabase: Provider<TaskDatabase>,
#ApplicationScope applicationScope: CoroutineScope
) = object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
val dao = taskDatabase.get().taskDao()
applicationScope.launch {
dao.insert(Task("First task"))
dao.insert(Task("Second task"))
dao.insert(Task("Third task", important = true))
dao.insert(Task("Fourth task", completed = true))
dao.insert(Task("Fifth task"))
dao.insert(Task("Sixth task", completed = true))
dao.insert(Task("Seventh task"))
dao.insert(Task("Eighth task"))
}
}
}
I need RoomDatabase.Callback when I create RoomDatabase, but I also need RoomDatabase when executing this callback. In provideDatabaseCreateCallback() function I wrapped RoomDatabase inside Provider<>.

Re-initialization an object provided by Dagger 2

I have an interface called LoginService which is used with Retrofit. There was no accessToken before the user login. When the user has logged in, the LoginService Instance should be updated with acessToken so that the user could log out from the app. The problem is that LoginService class is not updated even though it is not declared as #Singleton. If the user has closed the app and reopen it again, the LoginService got updated and therefore he could log out of the app. How can I reinitialize the LoginService instance as soon as the accessToken has been updated?
The key here is not the reinitialize the instance but to create a separate service whenever the user logs in.
you need to have two different interfaces one for API's pre-login and other for post login.
So one would be Authapi.kt and the other would be Api.kt
So first we need to create OkHttpBuilder for each service,
#Provides
#Singleton
#Named(NetModule.NO_AUTH_CLIENT)
fun provideNoAuthOkHttpClient(
okHttpClientBuilder: OkHttpClient.Builder
): OkHttpClient {
return okHttpClientBuilder.build()
}
#Provides
#Singleton
#Named(NetModule.AUTH_CLIENT)
fun provideAuthOkHttpClient(
okHttpClientBuilder: OkHttpClient.Builder,
tokenInterceptor: NetworkInterceptor
): OkHttpClient {
return okHttpClientBuilder.addInterceptor(tokenInterceptor).build()
}
Network Interceptor is the class where you update your access token for API.
NetworkInterceptor.kt
class NetworkInterceptor #Inject constructor(
val context: Context,
serverBaseUrl: String,
val moshi: Moshi,
val preferences: PreferenceUtility
) : Interceptor {
private fun Request.Builder.setDefaultHeaders(): Request.Builder {
addHeader("App_version_code", BuildConfig.VERSION_CODE.toString())
addHeader("App_version_name", BuildConfig.VERSION_NAME)
addHeader("Mobile_model", Build.MODEL.toString())
addHeader("OS_version", Build.VERSION.SDK_INT.toString())
addHeader("OS_version_release", Build.VERSION.RELEASE.toString())
if (preferences.customerId != -1L)
addHeader("Client_id", preferences.customerId.toString())
return this
}
private fun makeRequestWithAuthTokenAndTimeStamp(request: Request) = request.newBuilder()
.setDefaultHeaders()
.apply {
val oldHeader = request.header("Authorization")
if (oldHeader.isNullOrBlank()) {
val token = if (preferences.authToken.isBlank() || !preferences.customerAuthenticated) BuildConfig.ANONYMOUS_TOKEN else preferences.authToken
addHeader("Authorization", "Bearer $token")
}
}
.url(request.url())
.method(request.method(), request.body())
.build()
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
// ADD THE TOKEN'S OVER HERE
var response =
chain.proceed(makeRequestWithAuthTokenAndTimeStamp(request))
//You can also add refersh logic here.
return response
}
}
And you NetworkModule.kt will be
#Provides
#Singleton
#Named(NetModule.NO_AUTH_CLIENT)
fun provideNoAuthInterceptorRetrofit(
moshi: Moshi,
#Named(NetModule.NO_AUTH_CLIENT) okHttpClient: OkHttpClient,
debugPreferenceUtility: DebugPreferenceUtility
): Retrofit {
return Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(debugPreferenceUtility.serverBaseUrl)
.client(okHttpClient)
.build()
}
#Provides
#Singleton
#Named(NetModule.AUTH_CLIENT)
fun provideAuthInterceptorRetrofit(
moshi: Moshi,
#Named(NetModule.AUTH_CLIENT) okHttpClient: OkHttpClient,
debugPreferenceUtility: DebugPreferenceUtility
): Retrofit {
return Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(debugPreferenceUtility.serverBaseUrl)
.client(okHttpClient)
.build()
}
#Provides
#Singleton
fun provideApi(#Named(AUTH_CLIENT) retrofit: Retrofit) = retrofit.create(Api::class.java)
#Provides
#Singleton
fun provideAuthApi(#Named(NO_AUTH_CLIENT) retrofit: Retrofit) = retrofit.create(AuthApi::class.java)
So the API's that will be used pre-login will be placed inside AuthApi.kt
like send OTP etc and all the rest API will be placed inside Api.kt and Network Interceptor will take care of adding token.

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