This is a library written in Kotlin, for simplifying api calls with retrofit.
It's as easy as this :
retrofitClient.Get<GetResponseModel>()
.setPath("api/users/2"))
.setResponseHandler(GetResponseModel::class.java,
object : ResponseHandler<GetResponseModel>() {
override fun onSuccess(response: Response<GetResponseModel>) {
super.onSuccess(response)
//Handle Response
}
}).run(this)
Documentation can be found in wiki or just see the exapmle app.
Thanks to these beautifull libraries : OkHttp, RxAndroid, Retrofit, Gson
allprojects {
repositories {
...
maven { url 'https://jitpack.io'
}
}
dependencies {
implementation 'com.github.G-Corporation:RetrofitHelper:`1.1.6`'
}
class Application : Application() {
override fun onCreate() {
super.onCreate()
retrofitClient = RetrofitClient.instance
//api url
.setBaseUrl("https://reqres.in/")
//you can set multiple urls
// .setUrl("example","http://ngrok.io/api/")
//set timeouts
.setConnectionTimeout(4)
.setReadingTimeout(15)
//enable cache
.enableCaching(this)
//add Headers
.addHeader("Content-Type", "application/json")
.addHeader("client", "android")
.addHeader("language", Locale.getDefault().language)
.addHeader("os", android.os.Build.VERSION.RELEASE)
}
companion object {
lateinit var retrofitClient: RetrofitClient
}
}
Sample POST
request:
For more Api call methods see API Calls.
retrofitClient.Post<PostRequestModel, PostResponseModel>()
.setPath("api/users")
//set headers Key-Value or HashMap
// .setRequestHeader()
//set url params Key-Value or HashMap
// .setUrlParams()
.setBody(PostRequestModel("morpheus", "leader"))
.setResponseHandler(PostResponseModel::class.java,
object : ResponseHandler<PostResponseModel>() {
override fun onSuccess(response: Response<PostResponseModel>) {
super.onSuccess(response)
//handle response
log.text = response.body.toString()
}
override fun onBeforeSend() {
super.onBeforeSend()
}
override fun onError(response: Response<PostResponseModel>?) {
super.onError(response)
}
override fun onFailed(e: Throwable?) {
super.onFailed(e)
}
override fun onComplete() {
super.onComplete()
}
})
//DO NOT FORGET TO CALL .run()
.run(this)
}
Aryan Moradi |
Mojtaba Razaghi |