mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2025-12-10 06:42:05 +01:00
Simplify Http client factory
This commit is contained in:
@@ -16,7 +16,7 @@ import ca.gosyer.jui.domain.migration.interactor.RunMigrations
|
||||
import ca.gosyer.jui.domain.migration.service.MigrationPreferences
|
||||
import ca.gosyer.jui.domain.reader.service.ReaderPreferences
|
||||
import ca.gosyer.jui.domain.server.Http
|
||||
import ca.gosyer.jui.domain.server.HttpProvider
|
||||
import ca.gosyer.jui.domain.server.httpClient
|
||||
import ca.gosyer.jui.domain.server.service.ServerPreferences
|
||||
import ca.gosyer.jui.domain.source.service.CatalogPreferences
|
||||
import ca.gosyer.jui.domain.ui.service.UiPreferences
|
||||
@@ -26,9 +26,6 @@ import kotlinx.serialization.json.Json
|
||||
import me.tatarka.inject.annotations.Provides
|
||||
|
||||
interface SharedDomainComponent : CoreComponent {
|
||||
// Providers
|
||||
val httpProvider: HttpProvider
|
||||
|
||||
// Factories
|
||||
val migrations: RunMigrations
|
||||
|
||||
@@ -61,6 +58,11 @@ interface SharedDomainComponent : CoreComponent {
|
||||
|
||||
val json: Json
|
||||
|
||||
@AppScope
|
||||
@Provides
|
||||
fun httpFactory(serverPreferences: ServerPreferences, json: Json) =
|
||||
httpClient(serverPreferences, json)
|
||||
|
||||
@get:AppScope
|
||||
@get:Provides
|
||||
val serverPreferencesFactory: ServerPreferences
|
||||
@@ -103,11 +105,6 @@ interface SharedDomainComponent : CoreComponent {
|
||||
val updatePreferencesFactory: UpdatePreferences
|
||||
get() = UpdatePreferences(preferenceFactory.create("update"))
|
||||
|
||||
@get:AppScope
|
||||
@get:Provides
|
||||
val httpFactory: Http
|
||||
get() = httpProvider.get()
|
||||
|
||||
@get:AppScope
|
||||
@get:Provides
|
||||
val libraryUpdateServiceFactory: LibraryUpdateService
|
||||
|
||||
@@ -29,7 +29,6 @@ import io.ktor.http.URLBuilder
|
||||
import io.ktor.http.URLProtocol
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.serialization.json.Json
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import org.lighthousegames.logging.logging
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
@@ -41,79 +40,74 @@ expect val Engine: HttpClientEngineFactory<HttpClientEngineConfig>
|
||||
|
||||
expect fun HttpClientConfig<HttpClientEngineConfig>.configurePlatform()
|
||||
|
||||
class HttpProvider @Inject constructor(
|
||||
private val serverPreferences: ServerPreferences,
|
||||
private val json: Json
|
||||
) {
|
||||
fun get(): Http {
|
||||
return HttpClient(Engine) {
|
||||
configurePlatform()
|
||||
fun httpClient(serverPreferences: ServerPreferences, json: Json): Http {
|
||||
return HttpClient(Engine) {
|
||||
configurePlatform()
|
||||
|
||||
expectSuccess = true
|
||||
expectSuccess = true
|
||||
|
||||
engine {
|
||||
proxy = when (serverPreferences.proxy().get()) {
|
||||
Proxy.NO_PROXY -> null
|
||||
Proxy.HTTP_PROXY -> ProxyBuilder.http(
|
||||
URLBuilder(
|
||||
host = serverPreferences.proxyHttpHost().get(),
|
||||
port = serverPreferences.proxyHttpPort().get()
|
||||
).build()
|
||||
)
|
||||
Proxy.SOCKS_PROXY -> ProxyBuilder.socks(
|
||||
serverPreferences.proxySocksHost().get(),
|
||||
serverPreferences.proxySocksPort().get()
|
||||
)
|
||||
}
|
||||
engine {
|
||||
proxy = when (serverPreferences.proxy().get()) {
|
||||
Proxy.NO_PROXY -> null
|
||||
Proxy.HTTP_PROXY -> ProxyBuilder.http(
|
||||
URLBuilder(
|
||||
host = serverPreferences.proxyHttpHost().get(),
|
||||
port = serverPreferences.proxyHttpPort().get()
|
||||
).build()
|
||||
)
|
||||
Proxy.SOCKS_PROXY -> ProxyBuilder.socks(
|
||||
serverPreferences.proxySocksHost().get(),
|
||||
serverPreferences.proxySocksPort().get()
|
||||
)
|
||||
}
|
||||
when (serverPreferences.auth().get()) {
|
||||
Auth.NONE -> Unit
|
||||
Auth.BASIC -> AuthPlugin {
|
||||
basic {
|
||||
sendWithoutRequest {
|
||||
it.url.protocol == URLProtocol.WS || it.url.protocol == URLProtocol.WSS
|
||||
}
|
||||
credentials {
|
||||
BasicAuthCredentials(
|
||||
serverPreferences.authUsername().get(),
|
||||
serverPreferences.authPassword().get()
|
||||
)
|
||||
}
|
||||
}
|
||||
when (serverPreferences.auth().get()) {
|
||||
Auth.NONE -> Unit
|
||||
Auth.BASIC -> AuthPlugin {
|
||||
basic {
|
||||
sendWithoutRequest {
|
||||
it.url.protocol == URLProtocol.WS || it.url.protocol == URLProtocol.WSS
|
||||
}
|
||||
}
|
||||
Auth.DIGEST -> AuthPlugin {
|
||||
digest {
|
||||
credentials {
|
||||
DigestAuthCredentials(
|
||||
serverPreferences.authUsername().get(),
|
||||
serverPreferences.authPassword().get()
|
||||
)
|
||||
}
|
||||
credentials {
|
||||
BasicAuthCredentials(
|
||||
serverPreferences.authUsername().get(),
|
||||
serverPreferences.authPassword().get()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
install(HttpTimeout) {
|
||||
connectTimeoutMillis = 30.seconds.inWholeMilliseconds
|
||||
requestTimeoutMillis = 30.seconds.inWholeMilliseconds
|
||||
socketTimeoutMillis = 2.minutes.inWholeMilliseconds
|
||||
}
|
||||
install(ContentNegotiation) {
|
||||
json(json)
|
||||
}
|
||||
install(WebSockets)
|
||||
install(Logging) {
|
||||
level = if (BuildKonfig.DEBUG) {
|
||||
LogLevel.HEADERS
|
||||
} else {
|
||||
LogLevel.INFO
|
||||
}
|
||||
logger = object : Logger {
|
||||
val log = logging("HttpClient")
|
||||
override fun log(message: String) {
|
||||
log.info { message }
|
||||
Auth.DIGEST -> AuthPlugin {
|
||||
digest {
|
||||
credentials {
|
||||
DigestAuthCredentials(
|
||||
serverPreferences.authUsername().get(),
|
||||
serverPreferences.authPassword().get()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
install(HttpTimeout) {
|
||||
connectTimeoutMillis = 30.seconds.inWholeMilliseconds
|
||||
requestTimeoutMillis = 30.seconds.inWholeMilliseconds
|
||||
socketTimeoutMillis = 2.minutes.inWholeMilliseconds
|
||||
}
|
||||
install(ContentNegotiation) {
|
||||
json(json)
|
||||
}
|
||||
install(WebSockets)
|
||||
install(Logging) {
|
||||
level = if (BuildKonfig.DEBUG) {
|
||||
LogLevel.HEADERS
|
||||
} else {
|
||||
LogLevel.INFO
|
||||
}
|
||||
logger = object : Logger {
|
||||
val log = logging("HttpClient")
|
||||
override fun log(message: String) {
|
||||
log.info { message }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user