GrpcClientCallScope

Available in a dev version: 0․11․0-grpc-186

How to configure

The scope of a single outgoing gRPC client call observed by a GrpcClientInterceptor.

An interceptor receives this scope instance for every call and can:

  • Inspect the RPC method being invoked.

  • Read or populate requestHeaders before the request is sent.

  • Read callOptions that affect transport-level behavior.

  • Register callbacks with onHeaders and onClose to observe response metadata and final status.

  • Cancel the call early via cancel.

  • Continue the call by calling proceed with a (possibly transformed) request Flow.

  • Transform the response by modifying the returned Flow.

val interceptor = object : GrpcClientInterceptor {
override fun <Request, Response> GrpcClientCallScope<Request, Response>.intercept(
request: Flow<Request>
): Flow<Response> {
// Example: add a header before proceeding
requestHeaders[MyKeys.Authorization] = token

// Example: modify call options
callOptions.timeout = 5.seconds

// Example: observe response metadata
onHeaders { headers -> /* inspect headers */}
onClose { status, trailers -> /* log status/trailers */}

// IMPORTANT: proceed forwards the call to the next interceptor/transport.
// If you do not call proceed, no request will be sent and the call is short-circuited.
return proceed(request)
}
}

Type Parameters

Request

the request message type of the RPC.

Response

the response message type of the RPC.

Properties

Link copied to clipboard

Transport/engine options used for this call (deadlines, compression, etc.). Modifying this object is only possible before the call is proceeded.

Link copied to clipboard

Descriptor of the RPC method (name, marshalling, type) being invoked.

Link copied to clipboard

Outgoing request headers for this call.

Functions

Link copied to clipboard
abstract fun cancel(message: String, cause: Throwable? = null): Nothing

Cancel the call locally, providing a human-readable message and an optional cause. This method won't return and abort all further processing.

Link copied to clipboard
abstract fun onClose(block: (status: GrpcStatus, responseTrailers: GrpcMetadata) -> Unit)

Register a callback invoked when the call completes, successfully or not. The final status and trailing responseTrailers are provided.

Link copied to clipboard
abstract fun onHeaders(block: (responseHeaders: GrpcMetadata) -> Unit)

Register a callback invoked when the initial response headers are received. Typical gRPC semantics guarantee headers are delivered at most once per call and before the first message is received.

Link copied to clipboard
abstract fun proceed(request: Flow<Request>): Flow<Response>

Continue the invocation by forwarding it to the next interceptor or to the underlying transport.