Skip to content

Commit d6b9a87

Browse files
KTOR-4924 update API docs for the client (#3176)
1 parent c44dffa commit d6b9a87

15 files changed

+302
-229
lines changed

ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ import kotlinx.coroutines.*
1818
import kotlin.coroutines.*
1919

2020
/**
21-
* Constructs an asynchronous [HttpClient] using optional [block] for configuring this client.
21+
* Creates an asynchronous [HttpClient] with the specified [block] configuration.
2222
*
23-
* The [HttpClientEngine] is selected from the dependencies.
24-
* https://ktor.io/docs/http-client-engines.html
23+
* Note that the client requires an engine for processing network requests.
24+
* The [HttpClientEngine] is selected from the [dependencies](https://ktor.io/docs/client-dependencies.html).
2525
*/
2626
@KtorDsl
2727
public expect fun HttpClient(
2828
block: HttpClientConfig<*>.() -> Unit = {}
2929
): HttpClient
3030

3131
/**
32-
* Constructs an asynchronous [HttpClient] using the specified [engineFactory]
33-
* and an optional [block] for configuring this client.
32+
* Creates an asynchronous [HttpClient] with the specified [HttpClientEngineFactory] and optional [block] configuration.
33+
* Note that a specific platform may require a specific engine for processing requests.
34+
* You can learn more about available engines from [Engines](https://ktor.io/docs/http-client-engines.html).
3435
*/
3536
@KtorDsl
3637
public fun <T : HttpClientEngineConfig> HttpClient(
@@ -51,8 +52,9 @@ public fun <T : HttpClientEngineConfig> HttpClient(
5152
}
5253

5354
/**
54-
* Constructs an asynchronous [HttpClient] using the specified [engine]
55-
* and a [block] for configuring this client.
55+
* Creates an asynchronous [HttpClient] with the specified [HttpClientEngine] and optional [block] configuration.
56+
* Note that a specific platform may require a specific engine for processing requests.
57+
* You can learn more about available engines from [Engines](https://ktor.io/docs/http-client-engines.html).
5658
*/
5759
@KtorDsl
5860
public fun HttpClient(
@@ -61,10 +63,12 @@ public fun HttpClient(
6163
): HttpClient = HttpClient(engine, HttpClientConfig<HttpClientEngineConfig>().apply(block), manageEngine = false)
6264

6365
/**
64-
* Asynchronous client to perform HTTP requests.
66+
* A multiplatform asynchronous HTTP client, which allows you to make requests and handle responses,
67+
* extend its functionality with plugins, such as authentication, JSON serialization, and so on.
6568
*
66-
* This is a generic implementation that uses a specific engine [HttpClientEngine].
67-
* @property engine: [HttpClientEngine] for executing requests.
69+
* You can learn how to create a configure [HttpClient] from
70+
* [Creating and configuring a client](https://ktor.io/docs/create-client.html).
71+
* @property engine [HttpClientEngine] used to execute network requests.
6872
*/
6973
@OptIn(InternalAPI::class)
7074
public class HttpClient(
@@ -88,22 +92,22 @@ public class HttpClient(
8892
public override val coroutineContext: CoroutineContext = engine.coroutineContext + clientJob
8993

9094
/**
91-
* Pipeline used for processing all the requests sent by this client.
95+
* A pipeline used for processing all requests sent by this client.
9296
*/
9397
public val requestPipeline: HttpRequestPipeline = HttpRequestPipeline(userConfig.developmentMode)
9498

9599
/**
96-
* Pipeline used for processing all the responses sent by the server.
100+
* A pipeline used for processing all responses sent by the server.
97101
*/
98102
public val responsePipeline: HttpResponsePipeline = HttpResponsePipeline(userConfig.developmentMode)
99103

100104
/**
101-
* Pipeline used for sending the request.
105+
* A pipeline used for sending a request.
102106
*/
103107
public val sendPipeline: HttpSendPipeline = HttpSendPipeline(userConfig.developmentMode)
104108

105109
/**
106-
* Pipeline used for receiving request.
110+
* A pipeline used for receiving a request.
107111
*/
108112
public val receivePipeline: HttpReceivePipeline = HttpReceivePipeline(userConfig.developmentMode)
109113

@@ -113,12 +117,12 @@ public class HttpClient(
113117
public val attributes: Attributes = Attributes(concurrent = true)
114118

115119
/**
116-
* Client engine config.
120+
* Provides access to the client's engine configuration.
117121
*/
118122
public val engineConfig: HttpClientEngineConfig = engine.config
119123

120124
/**
121-
* Provides events on client lifecycle
125+
* Provides access to the events of the client's lifecycle.
122126
*/
123127
public val monitor: Events = Events()
124128

@@ -188,14 +192,14 @@ public class HttpClient(
188192
}
189193

190194
/**
191-
* Check if the specified [capability] is supported by this client.
195+
* Checks if the specified [capability] is supported by this client.
192196
*/
193197
public fun isSupported(capability: HttpClientEngineCapability<*>): Boolean {
194198
return engine.supportedCapabilities.contains(capability)
195199
}
196200

197201
/**
198-
* Returns a new [HttpClient] copying this client configuration,
202+
* Returns a new [HttpClient] by copying this client's configuration
199203
* and additionally configured by the [block] parameter.
200204
*/
201205
public fun config(block: HttpClientConfig<*>.() -> Unit): HttpClient = HttpClient(

ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClientConfig.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import io.ktor.utils.io.concurrent.*
1212
import kotlin.collections.set
1313

1414
/**
15-
* Mutable configuration used by [HttpClient].
15+
* A mutable [HttpClient] configuration.
16+
* Learn more about the client's configuration from
17+
* [Creating and configuring a client](https://ktor.io/docs/create-client.html).
1618
*/
1719
@KtorDsl
1820
public class HttpClientConfig<T : HttpClientEngineConfig> {
@@ -23,7 +25,9 @@ public class HttpClientConfig<T : HttpClientEngineConfig> {
2325
internal var engineConfig: T.() -> Unit = {}
2426

2527
/**
26-
* Configure engine parameters.
28+
* Allows you to configure engine parameters.
29+
*
30+
* You can learn more from [Engines](https://ktor.io/docs/http-client-engines.html).
2731
*/
2832
public fun engine(block: T.() -> Unit) {
2933
val oldConfig = engineConfig
@@ -34,27 +38,31 @@ public class HttpClientConfig<T : HttpClientEngineConfig> {
3438
}
3539

3640
/**
37-
* Use [HttpRedirect] plugin to automatically follow redirects.
41+
* Specifies whether the client redirects to URLs provided in the `Location` header.
42+
* You can disable redirections by setting this property to `false`.
3843
*/
3944
public var followRedirects: Boolean = true
4045

4146
/**
42-
* Use [defaultTransformers] to automatically handle simple [ContentType].
47+
* Uses [defaultTransformers] to automatically handle simple [ContentType].
4348
*/
4449
public var useDefaultTransformers: Boolean = true
4550

4651
/**
47-
* Terminate [HttpClient.receivePipeline] if status code is not successful (>=300).
52+
* Terminates [HttpClient.receivePipeline] if the status code is not successful (>=300).
53+
* Learn more from [Response validation](https://ktor.io/docs/response-validation.html).
4854
*/
4955
public var expectSuccess: Boolean = false
5056

5157
/**
52-
* Indicate if client should use development mode. In development mode client pipelines have advanced stack traces.
58+
* Indicates whether the client should use [development mode](https://ktor.io/docs/development-mode.html).
59+
* In development mode, the client's pipelines have advanced stack traces.
5360
*/
5461
public var developmentMode: Boolean = PlatformUtils.IS_DEVELOPMENT_MODE
5562

5663
/**
57-
* Installs a specific [plugin] and optionally [configure] it.
64+
* Installs the specified [plugin] and optionally configures it using the [configure] block.
65+
* Learn more from [Plugins](https://ktor.io/docs/http-client-plugins.html).
5866
*/
5967
public fun <TBuilder : Any, TPlugin : Any> install(
6068
plugin: HttpClientPlugin<TBuilder, TPlugin>,
@@ -98,7 +106,7 @@ public class HttpClientConfig<T : HttpClientEngineConfig> {
98106
}
99107

100108
/**
101-
* Clones this [HttpClientConfig] duplicating all the [plugins] and [customInterceptors].
109+
* Clones this [HttpClientConfig] by duplicating all the [plugins] and [customInterceptors].
102110
*/
103111
public fun clone(): HttpClientConfig<T> {
104112
val result = HttpClientConfig<T>()
@@ -107,7 +115,7 @@ public class HttpClientConfig<T : HttpClientEngineConfig> {
107115
}
108116

109117
/**
110-
* Install plugin from [other] client config.
118+
* Installs the plugin from the [other] client's configuration.
111119
*/
112120
public operator fun plusAssign(other: HttpClientConfig<out T>) {
113121
followRedirects = other.followRedirects

ktor-client/ktor-client-core/common/src/io/ktor/client/call/HttpClientCall.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import kotlin.coroutines.*
1818
import kotlin.reflect.*
1919

2020
/**
21-
* A class that represents a single pair of [request] and [response] for a specific [HttpClient].
21+
* A pair of a [request] and [response] for a specific [HttpClient].
2222
*
23-
* @property client: client that executed the call.
23+
* @property client the client that executed the call.
2424
*/
2525
public open class HttpClientCall(
2626
public val client: HttpClient
@@ -35,13 +35,13 @@ public open class HttpClientCall(
3535
public val attributes: Attributes get() = request.attributes
3636

3737
/**
38-
* Represents the [request] sent by the client
38+
* The [request] sent by the client.
3939
*/
4040
public lateinit var request: HttpRequest
4141
protected set
4242

4343
/**
44-
* Represents the [response] sent by the server.
44+
* The [response] sent by the server.
4545
*/
4646
public lateinit var response: HttpResponse
4747
protected set

ktor-client/ktor-client-core/common/src/io/ktor/client/engine/HttpClientEngine.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ internal val CALL_COROUTINE = CoroutineName("call-context")
1919
internal val CLIENT_CONFIG = AttributeKey<HttpClientConfig<*>>("client-config")
2020

2121
/**
22-
* Base interface use to define engines for [HttpClient].
22+
* Serves as the base interface for an [HttpClient]'s engine.
2323
*/
2424
public interface HttpClientEngine : CoroutineScope, Closeable {
2525
/**
26-
* [CoroutineDispatcher] specified for io operations.
26+
* Specifies [CoroutineDispatcher] for I/O operations.
2727
*/
2828
public val dispatcher: CoroutineDispatcher
2929

3030
/**
31-
* Engine configuration
31+
* Provides access to an engine's configuration.
3232
*/
3333
public val config: HttpClientEngineConfig
3434

@@ -48,7 +48,7 @@ public interface HttpClientEngine : CoroutineScope, Closeable {
4848
public suspend fun execute(data: HttpRequestData): HttpResponseData
4949

5050
/**
51-
* Install engine into [HttpClient].
51+
* Installs the engine to [HttpClient].
5252
*/
5353
@InternalAPI
5454
public fun install(client: HttpClient) {
@@ -84,7 +84,7 @@ public interface HttpClientEngine : CoroutineScope, Closeable {
8484
}
8585

8686
/**
87-
* Create call context and use it as a coroutine context to [execute] request.
87+
* Creates a call context and uses it as a coroutine context to [execute] a request.
8888
*/
8989
@OptIn(InternalAPI::class)
9090
private suspend fun executeWithinCallContext(requestData: HttpRequestData): HttpResponseData {
@@ -108,7 +108,7 @@ public interface HttpClientEngine : CoroutineScope, Closeable {
108108
}
109109

110110
/**
111-
* Factory of [HttpClientEngine] with a specific [T] of [HttpClientEngineConfig].
111+
* A factory of [HttpClientEngine] with a specific [T] of [HttpClientEngineConfig].
112112
*/
113113
public interface HttpClientEngineFactory<out T : HttpClientEngineConfig> {
114114
/**
@@ -135,7 +135,7 @@ public fun <T : HttpClientEngineConfig> HttpClientEngineFactory<T>.config(
135135
}
136136

137137
/**
138-
* Create call context with the specified [parentJob] to be used during call execution in the engine. Call context
138+
* Creates a call context with the specified [parentJob] to be used during call execution in the engine. Call context
139139
* inherits [coroutineContext], but overrides job and coroutine name so that call job's parent is [parentJob] and
140140
* call coroutine's name is "call-context".
141141
*/

ktor-client/ktor-client-core/common/src/io/ktor/client/engine/HttpClientEngineBase.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public abstract class HttpClientEngineBase(private val engineName: String) : Htt
3535
}
3636

3737
/**
38-
* Exception that indicates that client engine is already closed.
38+
* An exception indicating that the client's engine is already closed.
3939
*/
4040
public class ClientEngineClosedException(override val cause: Throwable? = null) :
4141
IllegalStateException("Client already closed")
4242

4343
/**
44-
* Close [CoroutineDispatcher] if it's [CloseableCoroutineDispatcher] or [Closeable].
44+
* Closes [CoroutineDispatcher] if it's [CloseableCoroutineDispatcher] or [Closeable].
4545
*/
4646
@OptIn(ExperimentalCoroutinesApi::class)
4747
private fun CoroutineDispatcher.close() {

ktor-client/ktor-client-core/common/src/io/ktor/client/request/HttpRequest.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public interface HttpRequest : HttpMessage, CoroutineScope {
5353
}
5454

5555
/**
56-
* Class for building [HttpRequestData].
56+
* Contains parameters used to make an HTTP request.
57+
*
58+
* Learn more from [Making requests](https://ktor.io/docs/request.html).
5759
*/
5860
@Suppress("DEPRECATION")
5961
public class HttpRequestBuilder : HttpMessageBuilder {
@@ -98,7 +100,7 @@ public class HttpRequestBuilder : HttpMessageBuilder {
98100
internal set
99101

100102
/**
101-
* Call specific attributes.
103+
* Provides access to attributes specific for this request.
102104
*/
103105
public val attributes: Attributes = Attributes(concurrent = true)
104106

@@ -108,7 +110,7 @@ public class HttpRequestBuilder : HttpMessageBuilder {
108110
public fun url(block: URLBuilder.(URLBuilder) -> Unit): Unit = url.block(url)
109111

110112
/**
111-
* Create immutable [HttpRequestData]
113+
* Creates immutable [HttpRequestData].
112114
*/
113115
@OptIn(InternalAPI::class)
114116
public fun build(): HttpRequestData = HttpRequestData(
@@ -121,14 +123,14 @@ public class HttpRequestBuilder : HttpMessageBuilder {
121123
)
122124

123125
/**
124-
* Set request specific attributes specified by [block].
126+
* Sets request-specific attributes specified by [block].
125127
*/
126128
public fun setAttributes(block: Attributes.() -> Unit) {
127129
attributes.apply(block)
128130
}
129131

130132
/**
131-
* Mutates [this] copying all the data from another [builder] using it as base.
133+
* Mutates [this] copying all the data from another [builder] using it as the base.
132134
*/
133135
@InternalAPI
134136
public fun takeFromWithExecutionContext(builder: HttpRequestBuilder): HttpRequestBuilder {
@@ -137,7 +139,7 @@ public class HttpRequestBuilder : HttpMessageBuilder {
137139
}
138140

139141
/**
140-
* Mutates [this] copying all the data but execution context from another [builder] using it as base.
142+
* Mutates [this] by copying all the data but execution context from another [builder] using it as the base.
141143
*/
142144
@OptIn(InternalAPI::class)
143145
public fun takeFrom(builder: HttpRequestBuilder): HttpRequestBuilder {
@@ -153,7 +155,7 @@ public class HttpRequestBuilder : HttpMessageBuilder {
153155
}
154156

155157
/**
156-
* Set capability configuration.
158+
* Sets capability configuration.
157159
*/
158160
@OptIn(InternalAPI::class)
159161
public fun <T : Any> setCapability(key: HttpClientEngineCapability<T>, capability: T) {
@@ -162,7 +164,7 @@ public class HttpRequestBuilder : HttpMessageBuilder {
162164
}
163165

164166
/**
165-
* Retrieve capability by key.
167+
* Retrieves capability by the key.
166168
*/
167169
public fun <T : Any> getCapabilityOrNull(key: HttpClientEngineCapability<T>): T? {
168170
@Suppress("UNCHECKED_CAST")
@@ -257,14 +259,14 @@ public fun HttpRequestBuilder.takeFrom(request: HttpRequestData): HttpRequestBui
257259
}
258260

259261
/**
260-
* Executes a [block] that configures the [URLBuilder] associated to thisrequest.
262+
* Executes a [block] that configures the [URLBuilder] associated to this request.
261263
*/
262264
public operator fun HttpRequestBuilder.Companion.invoke(block: URLBuilder.() -> Unit): HttpRequestBuilder =
263265
HttpRequestBuilder().apply { url(block) }
264266

265267
/**
266268
* Sets the [url] using the specified [scheme], [host], [port] and [path].
267-
* Pass `null` to keep existing value in the [URLBuilder].
269+
* Pass `null` to keep the existing value in the [URLBuilder].
268270
*/
269271
public fun HttpRequestBuilder.url(
270272
scheme: String? = null,
@@ -279,7 +281,7 @@ public fun HttpRequestBuilder.url(
279281
/**
280282
* Constructs a [HttpRequestBuilder] from URL information: [scheme], [host], [port] and [path]
281283
* and optionally further configures it using [block].
282-
* Pass `null` to keep existing value in the [URLBuilder].
284+
* Pass `null` to keep the existing value in the [URLBuilder].
283285
*/
284286
public operator fun HttpRequestBuilder.Companion.invoke(
285287
scheme: String? = null,

0 commit comments

Comments
 (0)