Skip to content

Commit

Permalink
fix timeout error
Browse files Browse the repository at this point in the history
  • Loading branch information
memishood committed Mar 20, 2021
1 parent 24b52b0 commit 7adf212
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions app/src/main/java/com/covidapp/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

/**
Expand All @@ -17,17 +19,36 @@ import javax.inject.Singleton
@Module
object NetworkModule {
private const val BASE_URL = "https://covid-api-backend.herokuapp.com/"
private const val CALL_TIMEOUT = 45 * 1000L
private const val CONNECT_TIMEOUT = 25 * 1000L
private const val READ_TIMEOUT = 45 * 1000L

/**
* create a http client for retrofit
*/
@Provides
@Singleton
fun httpClientProvides() : OkHttpClient {
return OkHttpClient.Builder()
.callTimeout(CALL_TIMEOUT, TimeUnit.MILLISECONDS)
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
.build()
}

/**
* create a retrofit instance
* @param httpClient from dependency injection
* @see httpClientProvides
*/
@Provides
@Singleton
fun retrofitProvides() : Retrofit {
fun retrofitProvides(httpClient: OkHttpClient) : Retrofit {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build()
}

/**
Expand Down

0 comments on commit 7adf212

Please sign in to comment.