Skip to content

Commit

Permalink
Add UserAgent to HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlooker committed Jul 2, 2024
1 parent c1400ae commit 1624da4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
10 changes: 10 additions & 0 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ plugins {
android {
namespace = "com.looker.network"

defaultConfig {
buildConfigField(
type = "String",
name = "VERSION_NAME",
value = "\"${DefaultConfig.versionName}\""
)
}
buildTypes {
release {
isMinifyEnabled = true
Expand All @@ -17,6 +24,9 @@ android {
isMinifyEnabled = true
}
}
buildFeatures {
buildConfig = true
}
}

dependencies {
Expand Down
35 changes: 24 additions & 11 deletions core/network/src/main/java/com/looker/network/KtorDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.ktor.client.engine.okhttp.OkHttpConfig
import io.ktor.client.network.sockets.ConnectTimeoutException
import io.ktor.client.network.sockets.SocketTimeoutException
import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.UserAgent
import io.ktor.client.plugins.onDownload
import io.ktor.client.request.head
import io.ktor.client.request.headers
Expand All @@ -41,17 +42,14 @@ import java.net.Proxy

internal class KtorDownloader : Downloader {

private var client = HttpClient(OkHttp) { timeoutConfig() }
private var client = client(null)
set(newClient) {
field.close()
field = newClient
}

override fun setProxy(proxy: Proxy) {
client = HttpClient(OkHttp) {
timeoutConfig()
engine { this.proxy = proxy }
}
client = client(proxy)
}

override suspend fun headCall(
Expand Down Expand Up @@ -102,14 +100,29 @@ internal class KtorDownloader : Downloader {
}
}

companion object {
private companion object {

fun client(proxy: Proxy?): HttpClient {
return HttpClient(OkHttp) {
userAgentConfig()
timeoutConfig()
engine { this.proxy = proxy }
}
}

const val USER_AGENT =
"Droid-ify, Mode: ${BuildConfig.BUILD_TYPE}, Version: ${BuildConfig.VERSION_NAME}"

fun HttpClientConfig<OkHttpConfig>.userAgentConfig() = install(UserAgent) {
agent = USER_AGENT
}

private fun HttpClientConfig<OkHttpConfig>.timeoutConfig() = install(HttpTimeout) {
fun HttpClientConfig<OkHttpConfig>.timeoutConfig() = install(HttpTimeout) {
connectTimeoutMillis = CONNECTION_TIMEOUT
socketTimeoutMillis = SOCKET_TIMEOUT
}

private fun createRequest(
fun createRequest(
url: String,
headers: HeadersBuilder.() -> Unit,
fileSize: Long? = null,
Expand All @@ -126,23 +139,23 @@ internal class KtorDownloader : Downloader {
}
}

private suspend infix fun ByteReadChannel.saveTo(target: File) =
suspend infix fun ByteReadChannel.saveTo(target: File) =
withContext(Dispatchers.IO) {
while (!isClosedForRead && isActive) {
val packet = readRemaining(DEFAULT_BUFFER_SIZE.toLong())
packet.appendTo(target)
}
}

private suspend fun ByteReadPacket.appendTo(file: File) =
suspend fun ByteReadPacket.appendTo(file: File) =
withContext(Dispatchers.IO) {
while (!isEmpty && isActive) {
val bytes = readBytes()
file.appendBytes(bytes)
}
}

private fun HttpResponse.asNetworkResponse(): NetworkResponse =
fun HttpResponse.asNetworkResponse(): NetworkResponse =
if (status.isSuccess() || status == HttpStatusCode.NotModified) {
NetworkResponse.Success(status.value, lastModified(), etag())
} else {
Expand Down

0 comments on commit 1624da4

Please sign in to comment.