Skip to content

Commit

Permalink
customized user agent
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
shadoWalker7 committed Jun 6, 2019
1 parent ebad0bb commit 9cd12ba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ayannetworking/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "0.1.1"
versionName "0.2.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class AyanApi(

private var apiInterface: ApiInterface? = null


fun aaa(defaultBaseUrl: String, timeout: Long) =
(apiInterface ?: RetrofitClient.getInstance(defaultBaseUrl, timeout).create(ApiInterface::class.java).also {
apiInterface = it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ir.ayantech.ayannetworking.networking

import android.os.Build
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand Down Expand Up @@ -31,11 +32,28 @@ class RetrofitClient private constructor() {
okHttpClientBuilder.readTimeout(timeout, TimeUnit.SECONDS)
okHttpClientBuilder.writeTimeout(timeout, TimeUnit.SECONDS)
okHttpClientBuilder.proxy(Proxy.NO_PROXY)
okHttpClientBuilder.addInterceptor {
val userAgentRequest = it.request()
.newBuilder()
.header("User-Agent", getFormattedDeviceInfo())
.build()
it.proceed(userAgentRequest)
}
return okHttpClient ?: okHttpClientBuilder.build().also { okHttpClient = it }
}

fun cancelCalls() {
okHttpClient?.dispatcher()?.cancelAll()
}

private fun getFormattedDeviceInfo(): String {
val information = listOf(
"BuildVersion:(${Build.VERSION.RELEASE})",
"Brand:(${Build.BRAND})",
"Model:(${Build.MODEL})",
"Device:(${Build.DEVICE})"
)
return information.joinToString(separator = " ")
}
}
}

0 comments on commit 9cd12ba

Please sign in to comment.