Created
October 1, 2019 05:52
-
-
Save Jaosrikate/0afec594283bd08813905928a4a9620b to your computer and use it in GitHub Desktop.
Retrofit2/OkHttp3 CookieJar Interceptor [kotlin]
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
// Retrofit2/OkHttp3 CookieJar Interceptor kotlin | |
// JaoSrikate | |
import android.util.Log | |
import android.webkit.CookieManager | |
import io.reactivex.Observable | |
import okhttp3.* | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Call | |
import retrofit2.Response | |
import retrofit2.Retrofit | |
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory | |
import retrofit2.converter.gson.GsonConverterFactory | |
import retrofit2.http.* | |
import java.util.concurrent.TimeUnit | |
abstract class ApiHelper { | |
companion object { | |
var TIMEOUT_MILLIS = 20000 | |
fun create(): YourServices { | |
val retrofit = Retrofit.Builder() | |
.addCallAdapterFactory( | |
RxJava2CallAdapterFactory.create() | |
) | |
.addConverterFactory( | |
GsonConverterFactory.create() | |
) | |
.baseUrl(BuildConfig.ENDPOINT_URL) | |
.client(setupOkhttpInterceptor()) | |
.build() | |
return retrofit.create(YourServices::class.java) | |
} | |
fun setupOkhttpInterceptor(): OkHttpClient { | |
var cookieManager: CookieManager | |
val interceptor = HttpLoggingInterceptor() | |
interceptor.level = HttpLoggingInterceptor.Level.BODY | |
val builder = OkHttpClient.Builder() | |
.followRedirects(false) | |
.cookieJar(object : CookieJar { | |
/** | |
* @param url | |
* @param cookies list of cookies get in api response | |
*/ | |
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) { | |
cookieManager = CookieManager.getInstance() | |
for (cookie in cookies) { | |
cookieManager.setCookie(url.toString(), cookie.toString()) | |
Log.e( | |
"ok", | |
"saveFromResponse : Cookie url : " + url.toString() + cookie.toString() | |
) | |
} | |
} | |
/** | |
* @param url | |
* | |
* adding cookies with request | |
*/ | |
override fun loadForRequest(url: HttpUrl): List<Cookie> { | |
cookieManager = CookieManager.getInstance() | |
val cookies: ArrayList<Cookie> = ArrayList() | |
if (cookieManager.getCookie(url.toString()) != null) { | |
val splitCookies = | |
cookieManager.getCookie(url.toString()).split("[,;]".toRegex()) | |
.dropLastWhile { it.isEmpty() }.toTypedArray() | |
for (i in splitCookies.indices) { | |
cookies.add(Cookie.parse(url, splitCookies[i].trim { it <= ' ' })!!) | |
Log.e( | |
"ok", | |
"loadForRequest :Cookie.add :: " + Cookie.parse( | |
url, | |
splitCookies[i].trim { it <= ' ' })!! | |
) | |
} | |
} | |
return cookies | |
} | |
}) | |
.followSslRedirects(false) | |
.cache(null) | |
.addInterceptor { chain -> | |
val request: Request = chain.request().newBuilder() | |
.addHeader("Content-Type", "application/json") | |
.build() | |
chain.proceed(request) | |
} | |
.addInterceptor(interceptor) | |
.connectTimeout(TIMEOUT_MILLIS.toLong(), TimeUnit.MILLISECONDS) | |
.readTimeout(TIMEOUT_MILLIS.toLong(), TimeUnit.MILLISECONDS) | |
.writeTimeout(TIMEOUT_MILLIS.toLong(), TimeUnit.MILLISECONDS) | |
val client = builder.build() | |
return client | |
} | |
} | |
//Just Example | |
interface YourServices { | |
@POST("public/cognitive-v1-service/cognitive-api/vision/thaiocr") | |
fun ocrVerify( | |
@Header("app-meta") meta: String, | |
@Header("client_id") clientID: String, | |
@Header("client_secret") clientSecret: String, | |
@Body faceVerifyRequest: FaceVerrifyRequest | |
) | |
: Observable<Response<OcrVerifyResponse>> | |
@POST("/api/v1/authn?") | |
fun primaryAuth( | |
@Body loginOauthRequest: LoginPrimaryAuthRequest | |
) | |
: Call<LoginPrimaryAuthResponse> | |
@GET("oauth2/default/v1/authorize") //code 302 is Success | |
fun userAuthorize( | |
@Query("client_id") clientID: String, | |
@Query("redirect_uri") redireUri: String, | |
@Query("response_type") responseType: String, | |
@Query("state") state: String, | |
@Query("scope") scope: String, | |
@Query("nonce") nonce: String, | |
@Query("sessionToken") sessionToken: String | |
) | |
: Call<LoginPrimaryAuthResponse> | |
@GET("oauth2/default/v1/authorize") //code 302 is Success | |
fun userAuthorize( | |
@QueryMap params: Map<String, String> | |
) | |
: Call<LoginPrimaryAuthResponse> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment