Last active
June 12, 2020 20:37
-
-
Save maiconhellmann/d62b9f3db6bc0985ca3553b5c8b5fc64 to your computer and use it in GitHub Desktop.
Htpp client using Koin DI, Retrofit and OkHttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//more plugins here.... | |
apply plugin: 'kotlin-kapt' | |
android { | |
//your stuff... | |
buildTypes { | |
release { | |
//your stuff... | |
buildConfigField "String", "BASE_URL", "\"https://your.api.url//\"" | |
} | |
debug { | |
buildConfigField "String", "BASE_URL", "\"https://your.api.url//\"" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Http client | |
implementation "com.squareup.retrofit2:retrofit:2.3.0" | |
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0" | |
implementation "com.squareup.retrofit2:converter-gson:2.3.0" | |
implementation "com.squareup.okhttp3:logging-interceptor:3.12.1" | |
//Koin DI | |
api "org.koin:koin-android:2.0.1" | |
implementation "org.koin:koin-androidx-viewmodel:2.0.1" | |
//Rx | |
api "io.reactivex.rxjava2:rxjava:2.2.7" | |
api "io.reactivex.rxjava2:rxkotlin:2.2.0" | |
api "io.reactivex.rxjava2:rxandroid:2.1.1" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add to manifest | |
<uses-permission android:name="android.permission.INTERNET"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import okhttp3.OkHttpClient | |
import okhttp3.logging.HttpLoggingInterceptor | |
import org.koin.dsl.module | |
import retrofit2.Retrofit | |
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory | |
import retrofit2.converter.gson.GsonConverterFactory | |
import java.util.concurrent.TimeUnit | |
/* | |
* This file is part of Revoltevaluation. | |
* | |
* Created by maiconhellmann on 09/08/2019 | |
* | |
* (c) 2019 | |
*/ | |
val remoteDataSourceModule = module { | |
//OkHttp | |
factory { providesOkHttpClient() } | |
//Retrofit | |
single { | |
createWebService<YouApiClassNameHere>( | |
okHttpClient = get(), url = BuildConfig.BASE_URL) | |
} | |
//If necessary, provide a data soure | |
factory { | |
RateRemoteDataSource( | |
api = get()) | |
} | |
} | |
fun providesOkHttpClient(): OkHttpClient { | |
val interceptor = HttpLoggingInterceptor().apply { | |
level = HttpLoggingInterceptor.Level.BODY | |
} | |
return OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).addInterceptor(interceptor) | |
.readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).build() | |
} | |
inline fun <reified T> createWebService( | |
okHttpClient: OkHttpClient, url: String | |
): T { | |
return Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()).baseUrl(url).client(okHttpClient) | |
.build().create(T::class.java) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment