Created
December 1, 2018 10:53
-
-
Save shaon2016/93d9f6de43f8b775893f46861d0aa204 to your computer and use it in GitHub Desktop.
This file contains 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
package com.lastblade.common.restclient | |
import android.content.Context | |
import com.durbinlabs.duplate.common.utils.U | |
import com.osellers.common.BuildConfig | |
import com.lastblade.common.utils.C | |
import com.lastblade.common.utils.D | |
import com.lastblade.common.utils.Lg | |
import io.reactivex.Observable | |
import io.reactivex.Observer | |
import io.reactivex.android.schedulers.AndroidSchedulers | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.disposables.Disposable | |
import io.reactivex.schedulers.Schedulers | |
import okhttp3.OkHttpClient | |
import okhttp3.ResponseBody | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Response | |
import retrofit2.Retrofit | |
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory | |
import retrofit2.converter.gson.GsonConverterFactory | |
import retrofit2.http.POST | |
object Rester { | |
private val TAG = Rester::class.simpleName | |
@Suppress("KDocUnresolvedReference") | |
enum class API { | |
MY_JSON | |
} | |
private interface Auth { | |
@POST("/my_json/") | |
fun getMyJson(): Observable<Response<ResponseBody>> | |
} | |
/*@JvmStatic | |
fun execute(context: Context, api: API, params: Array<Any>?, callback: ResterCallBack<Response<ResponseBody>>, | |
isToPromptNet: Boolean = false, noInternetSilentCallBack: NoInternetSilentCallBack? = null) { | |
if (!U.isNetConnected(context)) { | |
noInternetSilentCallBack?.notifyHasInternet(false) | |
if (isToPromptNet) | |
D.showNoInternetDialog(context) { execute(context, api, params, callback, isToPromptNet, noInternetSilentCallBack) } | |
return | |
} else noInternetSilentCallBack?.notifyHasInternet(true) | |
val retrofit = RestApiClient.getRetrofit(context) | |
val call = when (api) { | |
API.MY_JSON -> retrofit.create(Auth::class.java).getMyJson() | |
} | |
*//*call?.enqueue(object : Callback<ResponseBody> { | |
override fun onFailure(call: Call<ResponseBody>, e: Throwable) { | |
callback.onError(e) | |
e.printStackTrace() | |
} | |
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) { | |
callback.onResponse(response) | |
Lg.d(TAG, response.body()?.string()) | |
} | |
})*//* | |
call.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(object : Observer<retrofit2.Response<ResponseBody>> { | |
override fun onComplete() { | |
} | |
override fun onSubscribe(d: Disposable) { | |
RxJavaDisposableManager.add(d) | |
} | |
override fun onNext(response: retrofit2.Response<ResponseBody>) { | |
callback.onResponse(response) | |
Lg.d(TAG, response.body()?.string()) | |
} | |
override fun onError(e: Throwable) { | |
callback.onError(e) | |
e.printStackTrace() | |
} | |
}) | |
} | |
*/ | |
@JvmStatic | |
fun execute( | |
context: Context, api: API, params: Array<Any>?, | |
onSuccess: (success: retrofit2.Response<ResponseBody>) -> Unit, | |
isToPromptNet: Boolean = false, noInternetSilentCallBack: NoInternetSilentCallBack? = null | |
) { | |
if (!U.isNetConnected(context)) { | |
noInternetSilentCallBack?.notifyHasInternet(false) | |
if (isToPromptNet) | |
D.showNoInternetDialog(context) { | |
execute( | |
context, | |
api, | |
params, | |
onSuccess, | |
isToPromptNet, | |
noInternetSilentCallBack | |
) | |
} | |
return | |
} else noInternetSilentCallBack?.notifyHasInternet(true) | |
val retrofit = RestApiClient.getRetrofit(context) | |
val call = when (api) { | |
API.MY_JSON -> retrofit.create(Auth::class.java).getMyJson() | |
} | |
/*call?.enqueue(object : Callback<ResponseBody> { | |
override fun onFailure(call: Call<ResponseBody>, e: Throwable) { | |
callback.onError(e) | |
e.printStackTrace() | |
} | |
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) { | |
callback.onResponse(response) | |
Lg.d(TAG, response.body()?.string()) | |
} | |
})*/ | |
call.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(object : Observer<retrofit2.Response<ResponseBody>> { | |
override fun onComplete() { | |
} | |
override fun onSubscribe(d: Disposable) { | |
RxJavaDisposableManager.add(d) | |
} | |
override fun onNext(response: retrofit2.Response<ResponseBody>) { | |
onSuccess(response) | |
Lg.d(TAG, response.body()?.string()) | |
} | |
override fun onError(e: Throwable) { | |
e.printStackTrace() | |
} | |
}) | |
} | |
// A rest api client which works with rxjava | |
private object RestApiClient { | |
private var retrofit: Retrofit? = null | |
private var okHttpClient: OkHttpClient? = null | |
private var loggingInterceptor: HttpLoggingInterceptor? = null | |
private var rxAdapter = | |
RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()) | |
fun getRetrofit(context: Context): Retrofit { | |
if (retrofit == null) { | |
retrofit = create(context) | |
} | |
return retrofit!! | |
} | |
private fun create(context: Context): Retrofit { | |
// Level.BODY | |
/*shows all the information of a request. We just want to see info when app | |
* is in debug mode*/ | |
loggingInterceptor = HttpLoggingInterceptor() | |
if (BuildConfig.DEBUG) | |
loggingInterceptor!!.level = HttpLoggingInterceptor.Level.BODY | |
else loggingInterceptor!!.level = HttpLoggingInterceptor.Level.BASIC | |
okHttpClient = OkHttpClient.Builder().addInterceptor(loggingInterceptor!!).build() | |
return Retrofit.Builder() | |
.baseUrl(C.SERVER_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.addCallAdapterFactory(rxAdapter) | |
.client(okHttpClient!!) | |
.build() | |
} | |
} | |
object RxJavaDisposableManager { | |
private var compositeDisposable: CompositeDisposable? = null | |
fun add(disposable: Disposable) { | |
getCompositeDisposable().add(disposable) | |
} | |
fun dispose() { | |
getCompositeDisposable().dispose() | |
} | |
private fun getCompositeDisposable(): CompositeDisposable { | |
if (compositeDisposable == null || compositeDisposable!!.isDisposed) { | |
compositeDisposable = CompositeDisposable() | |
} | |
return compositeDisposable!! | |
} | |
} | |
/** | |
* Created by touhid on 7/10/16. | |
* | |
* @author touhid | |
*/ | |
interface NoInternetSilentCallBack { | |
fun notifyHasInternet(isNetOn: Boolean) | |
} | |
/** | |
* Created by Shaon on 7/10/18. | |
* | |
* @author Shaon | |
*/ | |
interface ResterCallBack<T> { | |
fun onResponse(response: T) | |
fun onError(e: Throwable) | |
} | |
/** | |
* Created by touhid on 7/10/16. | |
* | |
* @author touhid | |
*/ | |
interface RetryCallBackForNoInternet { | |
fun retry() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment