kv_storage.KVStorageClient.kt Maven / Gradle / Ivy
package kv_storage
import com.google.api.kgax.Retry
import com.google.api.kgax.grpc.ClientCallOptions
import com.google.api.kgax.grpc.ClientCallOptions.Builder
import com.google.api.kgax.grpc.ClientStreamingCall
import com.google.api.kgax.grpc.GrpcBasicRetry
import com.google.api.kgax.grpc.GrpcClientStub
import com.google.api.kgax.grpc.ServerStreamingCall
import com.google.api.kgax.grpc.pager
import com.google.api.kgax.grpc.prepare
import com.google.bytestream.ByteStreamProto
import com.google.longrunning.OperationsClientStub
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import java.util.concurrent.TimeUnit
import javax.annotation.Generated
import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.Unit
import kotlin.collections.List
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
/**
*
*
*
*
* [Product Documentation](http://www.google.com)
*/
@Generated("com.google.api.kotlin.generator.GRPCGenerator")
class KVStorageClient private constructor(
val channel: ManagedChannel,
val options: ClientCallOptions,
factory: Stubs.Factory? = null
) {
private val stubs: Stubs = factory?.create(channel, options) ?: Stubs(
KVStorageClientStub(channel).prepare(options),
OperationsClientStub(channel).prepare(options))
/**
* Prepare for an API call by setting any desired options. For example:
*
* ```
* val client = KVStorageClient.create()
* val response = client.prepare {
* withMetadata("my-custom-header", listOf("some", "thing"))
* }.get(request)
* ```
*
* You may save the client returned by this call and reuse it if you
* plan to make multiple requests with the same settings.
*/
fun prepare(init: ClientCallOptions.Builder.() -> Unit): KVStorageClient {
val optionsBuilder = ClientCallOptions.Builder(options)
optionsBuilder.init()
return KVStorageClient(channel, optionsBuilder.build())
}
/**
* Shutdown the [channel] associated with this client.
*/
fun shutdownChannel(waitForSeconds: Long = 5) {
channel.shutdown().awaitTermination(waitForSeconds, TimeUnit.SECONDS)
}
/**
*
*
* For example:
* ```
* val client = KVStorageClient.create()
* val result = client.get(
* com.google.bytestream.ByteStreamProto.readRequest {
* }
* )
* ```
*/
fun get(request: ByteStreamProto.ReadRequest): ServerStreamingCall
= stubs.api.executeServerStreaming(context = "get") { stub, observer ->
stub.get(request, observer)
}
/**
*
*
* For example:
* ```
* val client = KVStorageClient.create()
* val result = client.put(
* com.google.bytestream.ByteStreamProto.writeRequest {
* }
* )
* ```
*/
fun put(): ClientStreamingCall =
stubs.api.executeClientStreaming(context = "put") { it::put }
/**
* Utilities for creating a fully configured KVStorageClient.
*/
companion object {
/**
* Default scopes to use. Use [prepare] to override as needed.
*/
@JvmStatic
val ALL_SCOPES: List = listOf()
/**
* Default operations to retry on failure. Use [prepare] to override as needed.
*
* Note: This setting controls client side retries. If you enable
* server managed retries on the channel do not use this.
*/
@JvmStatic
val RETRY: Retry = GrpcBasicRetry(mapOf())
/**
* Create a KVStorageClient with the provided [channel], [options], or stub [factory].
*/
@JvmStatic
@JvmOverloads
fun create(
channel: ManagedChannel? = null,
options: ClientCallOptions? = null,
factory: Stubs.Factory? = null
): KVStorageClient = KVStorageClient(
channel ?: createChannel(),
options ?: ClientCallOptions(),
factory
)
/**
* Create a [ManagedChannel] to use with a KVStorageClient.
*
* [enableRetry] can be used to enable server managed retries, which is currently
* experimental. You should not use any client retry settings if you enable it.
*/
@JvmStatic
@JvmOverloads
fun createChannel(
host: String = "",
port: Int = 443,
enableRetry: Boolean = false
): ManagedChannel {
val builder = ManagedChannelBuilder.forAddress(host, port)
if (enableRetry) {
builder.enableRetry()
}
return builder.build()
}
}
class Stubs(val api: GrpcClientStub, val operation:
GrpcClientStub) {
interface Factory {
fun create(channel: ManagedChannel, options: ClientCallOptions): Stubs
}
}
}