Adds a UserAgent to all HTTP requests.
This commit is contained in:
@@ -1,7 +1,12 @@
|
|||||||
package com.vitorpamplona.amethyst.service
|
package com.vitorpamplona.amethyst.service
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.vitorpamplona.amethyst.BuildConfig
|
||||||
|
import okhttp3.Interceptor
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.Response
|
||||||
|
import java.io.IOException
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.net.Proxy
|
import java.net.Proxy
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
@@ -48,11 +53,24 @@ object HttpClient {
|
|||||||
.readTimeout(duration)
|
.readTimeout(duration)
|
||||||
.connectTimeout(duration)
|
.connectTimeout(duration)
|
||||||
.writeTimeout(duration)
|
.writeTimeout(duration)
|
||||||
|
.addInterceptor(DefaultContentTypeInterceptor())
|
||||||
.followRedirects(true)
|
.followRedirects(true)
|
||||||
.followSslRedirects(true)
|
.followSslRedirects(true)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DefaultContentTypeInterceptor : Interceptor {
|
||||||
|
@Throws(IOException::class)
|
||||||
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
|
val originalRequest: Request = chain.request()
|
||||||
|
val requestWithUserAgent: Request = originalRequest
|
||||||
|
.newBuilder()
|
||||||
|
.header("User-Agent", "Amethyst/${BuildConfig.VERSION_NAME}")
|
||||||
|
.build()
|
||||||
|
return chain.proceed(requestWithUserAgent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun getHttpClientForRelays(): OkHttpClient {
|
fun getHttpClientForRelays(): OkHttpClient {
|
||||||
if (this.defaultHttpClient == null) {
|
if (this.defaultHttpClient == null) {
|
||||||
this.defaultHttpClient = getHttpClient(defaultTimeout)
|
this.defaultHttpClient = getHttpClient(defaultTimeout)
|
||||||
|
|||||||
Reference in New Issue
Block a user