Get Started

Learn how to use this library.

Installation

            
              //root build.gradle
              allprojects {
                    repositories {
                        maven { url 'https://jitpack.io' }
                    }
                }
              //add dependency
              dependencies {
                    implementation 'com.github.jamesdeperio:RetrofitKit:v1.0.5'

    //required dependencies
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'

    //add rxjava adapter
    implementation 'com.squareup.retrofit2:adapter-rxjava2:x.x.x'

    //or add Kotlin coroutine's adapter
    implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:x.x.x'

    //then add your converter factory library
}

Usage

Here are the available classes that you can use to your project:


SerializationFormatFactory.Builder()

This builder class is use to apply multiple converter.

Method Description
setXMLConverterFactory(?) Set custom converter for xml. Add @XMLFormat annotation to your api call.
setJSONConverterFactory(?) Set custom converter for json. Add @JSONFormat annotation to your api call.
addCustomConverterFactory(?,?) Add custom annotation and converter.

Example:

              
                //kotlin
                //using TikXml library as xml converter
                val tikXMLConverter = TikXmlConverterFactory.create(TikXml.Builder().exceptionOnUnreadXml(false).build())

                //using GSON library as json converter
                val gsonConverter = GsonConverterFactory.create(GsonBuilder().setLenient().create())

                val multipleConverter = SerializationFormatFactory.Builder()
                .setXMLConverterFactory(converterFactory = tikXMLConverter)
                .setJSONConverterFactory(converterFactory = gsonConverter)
                .addCustomConverterFactory(responseFormat = YAMLFormat::class.java, converterFactory = YAMLConverterFactory.create())
                .build()

                //add annotation for custom converter
                @Retention(AnnotationRetention.RUNTIME)
                annotation class YAMLFormat

                interface RestRepository {
                    @GET("something")
                    @JSONFormat
                    fun getResponse1(): Observable<Response>

                    @GET("something")
                    @XMLFormat
                    fun getResponse2(): Call<Response>

                    @GET("something")
                    @YAMLFormat
                    fun getResponse3(): Call<Response>
              }
              
            

RetrofitManager

This base class is to apply lazy retrofit configuration.

Method Return Type Description
initCacheSize() Int Override cache size to your network call.
initBaseURL() String Override base url for api.
initWriteTimeOut() Long Override to change write timeout.
initReadTimeOut() Long Override to change read timeout.
initConnectTimeOut() Long Override to change connection timeout.
initConverterFactory() Converter.Factory Override to use your own custom converter library.
initCallAdapterFactory() CallAdapter.Factory Override to use rx java latest library for call adapter.
isPrintLogEnabled() Boolean Override to allow log printing or not.
interceptorConfiguration(builder) OkHttpClient.Builder Override to add more configuration to OkHttpClient.
create(?) Object Add your repository interface to assign as api.

Example:

              
                //kotlin
                class NetworkManager(context: Context) : RetrofitManager(context=context) {
                    var restRepository:RestRepository = create(RestRepository::class.java) as RestRepository //provide your api interface
                    override fun initBaseURL(): String = "http://www.baseURL.com/"
                    override fun initCacheSize(): Int = 0
                    override fun initConnectTimeOut(): Long = 60
                    override fun initReadTimeOut(): Long = 60
                    override fun initWriteTimeOut(): Long = 60
                    override fun initConverterFactory(): Converter.Factory = multipleConverter
                    override fun initCallAdapterFactory(): CallAdapter.Factory = RxJava2CallAdapterFactory.create()
                    override fun isPrintLogEnabled(): Boolean = true
                    override fun OkHttpClient.Builder.interceptorConfiguration(builder: OkHttpClient.Builder): OkHttpClient.Builder
                    = builder.addInterceptor(INTERCEPTOR)
                }

                val networkManager = NetworkManager(context)
                networkManager.getResponse1()
                  .subscribeOn(Schedulers.io())
                  .observeOn(Schedulers.newThread())
                  .subscribe{ /* response*/ }
              
            

Feel free to ask and add issue on my github repository! Cheers!