commonMain.io.ktor.client.plugins.HttpClientPlugin.kt Maven / Gradle / Ivy
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.client.plugins
import io.ktor.client.*
import io.ktor.util.*
import kotlin.native.concurrent.*
internal val PLUGIN_INSTALLED_LIST = AttributeKey("ApplicationPluginRegistry")
/**
* Base interface representing a [HttpClient] plugin.
*/
public interface HttpClientPlugin {
/**
* The [AttributeKey] for this plugin.
*/
public val key: AttributeKey
/**
* Builds a [TPlugin] by calling the [block] with a [TConfig] config instance as receiver.
*/
public fun prepare(block: TConfig.() -> Unit = {}): TPlugin
/**
* Installs the [plugin] class for a [HttpClient] defined at [scope].
*/
public fun install(plugin: TPlugin, scope: HttpClient)
}
/**
* Returns a [plugin] installed in this client. Returns `null` if the plugin was not previously installed.
*/
public fun HttpClient.pluginOrNull(plugin: HttpClientPlugin): F? =
attributes.getOrNull(PLUGIN_INSTALLED_LIST)?.getOrNull(plugin.key)
/**
* Returns a [plugin] installed in [HttpClient].
*
* @throws [IllegalStateException] if [plugin] is not installed.
*/
public fun HttpClient.plugin(plugin: HttpClientPlugin): F {
return pluginOrNull(plugin) ?: throw IllegalStateException(
"Plugin $plugin is not installed. Consider using `install(${plugin.key})` in client config first."
)
}