@@ -123,7 +123,20 @@ public class GrpcClient internal constructor(
123
123
}
124
124
125
125
/* *
126
- * Constructor function for the [GrpcClient] class.
126
+ * Creates and configures a gRPC client instance.
127
+ *
128
+ * This function initializes a new gRPC client with the specified target server
129
+ * and allows optional customization of the client's configuration through a configuration block.
130
+ *
131
+ * @param hostname The gRPC server hostname to connect to.
132
+ * @param port The gRPC server port to connect to.
133
+ * @param configure An optional configuration block to customize the [GrpcClientConfiguration].
134
+ * This can include setting up interceptors, specifying credentials, customizing message codec
135
+ * resolution, and overriding default authority.
136
+ *
137
+ * @return A new instance of [GrpcClient] configured with the specified target and options.
138
+ *
139
+ * @see [GrpcClientConfiguration]
127
140
*/
128
141
public fun GrpcClient (
129
142
hostname : String ,
@@ -134,8 +147,22 @@ public fun GrpcClient(
134
147
return GrpcClient (ManagedChannelBuilder (hostname, port, config.credentials), config)
135
148
}
136
149
150
+
137
151
/* *
138
- * Constructor function for the [GrpcClient] class.
152
+ * Creates and configures a gRPC client instance.
153
+ *
154
+ * This function initializes a new gRPC client with the specified target server
155
+ * and allows optional customization of the client's configuration through a configuration block.
156
+ *
157
+ * @param target The gRPC server endpoint to connect to, typically specified in
158
+ * the format `hostname:port`.
159
+ * @param configure An optional configuration block to customize the [GrpcClientConfiguration].
160
+ * This can include setting up interceptors, specifying credentials, customizing message codec
161
+ * resolution, and overriding default authority.
162
+ *
163
+ * @return A new instance of [GrpcClient] configured with the specified target and options.
164
+ *
165
+ * @see [GrpcClientConfiguration]
139
166
*/
140
167
public fun GrpcClient (
141
168
target : String ,
@@ -155,30 +182,105 @@ private fun GrpcClient(
155
182
return GrpcClient (channel, config.messageCodecResolver, config.interceptors)
156
183
}
157
184
185
+
186
+ /* *
187
+ * Configuration class for a gRPC client, providing customization options
188
+ * for client behavior, including interceptors, credentials, codec resolution,
189
+ * and authority overrides.
190
+ */
158
191
public class GrpcClientConfiguration internal constructor() {
159
- internal var messageCodecResolver: MessageCodecResolver = EmptyMessageCodecResolver
160
- internal var credentials: ClientCredentials ? = null
161
- internal var overrideAuthority: String? = null
162
192
internal val interceptors: MutableList <ClientInterceptor > = mutableListOf ()
163
193
164
- public fun usePlaintext () {
165
- credentials = createInsecureClientCredentials()
166
- }
194
+ /* *
195
+ * Configurable resolver used to determine the appropriate codec for a given Kotlin type
196
+ * during message serialization and deserialization in gRPC calls.
197
+ *
198
+ * Custom implementations of [MessageCodecResolver] can be provided to handle specific serialization
199
+ * for arbitrary types.
200
+ * For custom types prefer using the [kotlinx.rpc.grpc.codec.WithCodec] annotation.
201
+ *
202
+ * @see MessageCodecResolver
203
+ * @see kotlinx.rpc.grpc.codec.SourcedMessageCodec
204
+ * @see kotlinx.rpc.grpc.codec.WithCodec
205
+ */
206
+ public var messageCodecResolver: MessageCodecResolver = EmptyMessageCodecResolver
167
207
168
- public fun useCredentials (credentials : ClientCredentials ) {
169
- this @GrpcClientConfiguration.credentials = credentials
170
- }
171
208
172
- public fun overrideAuthority (authority : String ) {
173
- overrideAuthority = authority
174
- }
209
+ /* *
210
+ * Configures the client credentials used for secure gRPC requests made by the client.
211
+ *
212
+ * By default, the client uses default TLS credentials.
213
+ * To use custom TLS credentials, use the [tls] constructor function which returns a
214
+ * [TlsClientCredentials] instance.
215
+ *
216
+ * To use plaintext communication, use the [plaintext] constructor function.
217
+ * Should only be used for testing or for APIs where the use of such API or
218
+ * the data exchanged is not sensitive.
219
+ *
220
+ * ```
221
+ * GrpcClient("localhost", 50051) {
222
+ * credentials = plaintext() // for testing purposes only!
223
+ * }
224
+ * ```
225
+ */
226
+ public var credentials: ClientCredentials ? = null
227
+
228
+ /* *
229
+ * Overrides the authority used with TLS and HTTP virtual hosting.
230
+ * It does not change what the host is actually connected to.
231
+ * Is commonly in the form `host:port`.
232
+ */
233
+ public var overrideAuthority: String? = null
175
234
176
- public fun useMessageCodecResolver (messageCodecResolver : MessageCodecResolver ) {
177
- this .messageCodecResolver = messageCodecResolver
178
- }
179
235
236
+ /* *
237
+ * Adds one or more client-side interceptors to the current gRPC client configuration.
238
+ * Interceptors enable extended customization of gRPC calls
239
+ * by observing or altering the behaviors of requests and responses.
240
+ *
241
+ * The order of interceptors added via this method is significant.
242
+ * Interceptors are executed in the order they are added,
243
+ * while one interceptor has to invoke the next interceptor to proceed with the call.
244
+ *
245
+ * @param interceptors Interceptors to be added to the current configuration.
246
+ * Each provided instance of [ClientInterceptor] may perform operations such as modifying headers,
247
+ * observing call metadata, logging, or transforming data flows.
248
+ *
249
+ * @see ClientInterceptor
250
+ * @see ClientCallScope
251
+ */
180
252
public fun intercept (vararg interceptors : ClientInterceptor ) {
181
253
this .interceptors.addAll(interceptors)
182
254
}
183
255
256
+ /* *
257
+ * Provides insecure client credentials for the gRPC client configuration.
258
+ *
259
+ * Typically, this would be used for local development, testing, or other
260
+ * environments where security is not a concern.
261
+ *
262
+ * @return An insecure [ClientCredentials] instance that must be passed to [credentials].
263
+ */
264
+ public fun plaintext (): ClientCredentials = createInsecureClientCredentials()
265
+
266
+ /* *
267
+ * Configures and creates secure client credentials for the gRPC client.
268
+ *
269
+ * This method takes a configuration block in which TLS-related parameters,
270
+ * such as trust managers and key managers, can be defined. The resulting
271
+ * credentials are used to establish secure communication between the gRPC client
272
+ * and server, ensuring encrypted transmission of data and mutual authentication
273
+ * if configured.
274
+ *
275
+ * Alternatively, you can use the [TlsClientCredentials] constructor.
276
+ *
277
+ * @param configure A configuration block that allows setting up the TLS parameters
278
+ * using the [TlsClientCredentialsBuilder].
279
+ * @return A secure [ClientCredentials] instance that must be passed to [credentials].
280
+ *
281
+ * @see credentials
282
+ */
283
+ public fun tls (configure : TlsClientCredentialsBuilder .() -> Unit ): ClientCredentials =
284
+ TlsClientCredentials (configure)
285
+
184
286
}
0 commit comments