Skip to content

Instantly share code, notes, and snippets.

@pratamawijaya
Created October 11, 2023 11:25
Show Gist options
  • Save pratamawijaya/fc0091796b341cd1e9b8987cf2bbdc83 to your computer and use it in GitHub Desktop.
Save pratamawijaya/fc0091796b341cd1e9b8987cf2bbdc83 to your computer and use it in GitHub Desktop.
import com.google.gson.FieldNamingPolicy
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
object Network {
private fun builder(): Retrofit {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build()
val gson = GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl(BuildConfig.MAIN_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
fun getService(): Routes = builder().create(Routes::class.java)
val requestInterface = Retrofit.Builder()
.baseUrl(BuildConfig.MAIN_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(Routes::class.java)
}
import retrofit2.http.GET
import retrofit2.http.Query
interface Routes {
@GET("top-headlines")
suspend fun getTopHeadlines(
@Query("country") country: String,
@Query("category") category: String
): TopHeadlineResponse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment