Skip to content

Latest commit

 

History

History
76 lines (57 loc) · 2.42 KB

README.md

File metadata and controls

76 lines (57 loc) · 2.42 KB

Lich Thrift

Maven Central

A library for using Apache Thrift in combination with OkHttp.

// build.gradle
dependencies {
    implementation 'com.linecorp.lich:thrift:x.x.x'
}

The basic usage is just to implement a class that inherits AbstractThriftServiceClient.

For example, the code that calls FooService can be implemented as follows:

class FooServiceClient(
    override val okHttpClient: OkHttpClient,
    override val endpointUrl: HttpUrl
) : AbstractThriftServiceClient<FooService.Client>() {

    override val thriftClientFactory: ThriftClientFactory<FooService.Client> =
        ThriftClientFactory(FooService.Client.Factory())

    suspend fun ping() =
        call({ send_ping() }, { recv_ping() })

    suspend fun callFoo(id: Long, name: String, param: FooParam): FooResponse =
        call({ send_callFoo(id, name, param) }, { recv_callFoo() })
}

val fooServiceClient = FooServiceClient(OkHttpClient(), "https://api.example.com/foo".toHttpUrl())

try {
    val response = fooServiceClient.callFoo(123, "foobar", FooParam(4))
    println("response = $response")
} catch (e: TException) {
    println("fooService.callFoo() failed: $e")
}

Transparent logging

thrift-logging provides transparent logging with dynamic code generation.

// build.gradle
dependencies {
    debugImplementation 'com.linecorp.lich:thrift-logging:x.x.x'
}
val thriftLogger = object : ThriftLogger {
    override fun logSend(namespace: String, service: String, function: String, args: TBase<*, *>) {
        Log.d("ThriftLogger", "$service.send_$function: $args")
    }

    override fun logReceive(namespace: String, service: String, function: String, result: TBase<*, *>) {
        Log.d("ThriftLogger", "$service.recv_$function: $result")
    }
}
val thriftLogEnabler = ThriftLogEnabler(context, thriftLogger)

class FooServiceClient : AbstractThriftServiceClient<FooService.Client>() {

    override val thriftClientFactory: ThriftClientFactory<FooService.Client> =
        thriftLogEnabler.enableLogging(ThriftClientFactory(FooService.Client.Factory()))

    // snip...
}