Skip to content

Commit

Permalink
Add OkHttpSupplier classes and implement for all clients (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidliu authored Jun 18, 2024
1 parent 44fde8d commit 1c0c4e5
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 36 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 44 additions & 10 deletions src/main/kotlin/io/livekit/server/EgressServiceClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package io.livekit.server

import io.livekit.server.okhttp.OkHttpFactory
import io.livekit.server.okhttp.OkHttpHolder
import io.livekit.server.retrofit.TransformCall
import livekit.LivekitEgress
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.protobuf.ProtoConverterFactory
import java.util.function.Supplier

data class EncodedOutputs(
val fileOutput: LivekitEgress.EncodedFileOutput?,
Expand Down Expand Up @@ -681,17 +683,49 @@ class EgressServiceClient(

companion object {

@Deprecated(
"Use EgressServiceClient.createClient()",
ReplaceWith(
"EgressServiceClient.createClient(host, apiKey, secret, OkHttpFactory(logging))",
"import io.livekit.server.okhttp.OkHttpFactory",
"io.livekit.server.IngressServiceClient.createClient"
)
)
@JvmStatic
@JvmOverloads
fun create(host: String, apiKey: String, secret: String, logging: Boolean = false): EgressServiceClient {
val okhttp = with(OkHttpClient.Builder()) {
if (logging) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
addInterceptor(loggingInterceptor)
}
build()
}
fun create(
host: String,
apiKey: String,
secret: String,
logging: Boolean = false
): EgressServiceClient {
return createClient(
host = host,
apiKey = apiKey,
secret = secret,
okHttpSupplier = OkHttpFactory(logging = logging)
)
}

/**
* Create an EgressServiceClient.
*
* @param okHttpSupplier provide an [OkHttpFactory] if you wish to customize the http client
* (e.g. proxy, timeout, certificate/auth settings), or supply your own OkHttpClient
* altogether to pool resources with [OkHttpHolder].
*
* @see OkHttpHolder
* @see OkHttpFactory
*/
@JvmStatic
@JvmOverloads
fun createClient(
host: String,
apiKey: String,
secret: String,
okHttpSupplier: Supplier<OkHttpClient> = OkHttpFactory()
): EgressServiceClient {
val okhttp = okHttpSupplier.get()

val service = Retrofit.Builder()
.baseUrl(host)
Expand Down
50 changes: 40 additions & 10 deletions src/main/kotlin/io/livekit/server/IngressServiceClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package io.livekit.server

import io.livekit.server.okhttp.OkHttpFactory
import io.livekit.server.okhttp.OkHttpHolder
import io.livekit.server.retrofit.TransformCall
import livekit.LivekitIngress
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.protobuf.ProtoConverterFactory
import java.util.function.Supplier

class IngressServiceClient(
private val service: IngressService,
Expand Down Expand Up @@ -185,18 +187,46 @@ class IngressServiceClient(
}

companion object {

/**
* Create an IngressServiceClient.
*/
@Deprecated(
"Use IngressServiceClient.createClient()",
ReplaceWith(
"IngressServiceClient.createClient(host, apiKey, secret, OkHttpFactory(logging))",
"import io.livekit.server.okhttp.OkHttpFactory",
)
)
@JvmStatic
@JvmOverloads
fun create(host: String, apiKey: String, secret: String, logging: Boolean = false): IngressServiceClient {
val okhttp = with(OkHttpClient.Builder()) {
if (logging) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
addInterceptor(loggingInterceptor)
}
build()
}
return createClient(
host = host,
apiKey = apiKey,
secret = secret,
okHttpSupplier = OkHttpFactory(logging = logging)
)
}

/**
* Create an IngressServiceClient.
*
* @param okHttpSupplier provide an [OkHttpFactory] if you wish to customize the http client
* (e.g. proxy, timeout, certificate/auth settings), or supply your own OkHttpClient
* altogether to pool resources with [OkHttpHolder].
*
* @see OkHttpHolder
* @see OkHttpFactory
*/
@JvmStatic
@JvmOverloads
fun createClient(
host: String,
apiKey: String,
secret: String,
okHttpSupplier: Supplier<OkHttpClient> = OkHttpFactory()
): IngressServiceClient {
val okhttp = okHttpSupplier.get()

val service = Retrofit.Builder()
.baseUrl(host)
Expand Down
50 changes: 40 additions & 10 deletions src/main/kotlin/io/livekit/server/RoomServiceClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
package io.livekit.server

import com.google.protobuf.ByteString
import io.livekit.server.okhttp.OkHttpFactory
import io.livekit.server.okhttp.OkHttpHolder
import io.livekit.server.retrofit.TransformCall
import livekit.LivekitModels
import livekit.LivekitRoom
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.protobuf.ProtoConverterFactory
import java.util.function.Consumer
import java.util.function.Supplier

class RoomServiceClient(
private val service: RoomService,
Expand Down Expand Up @@ -322,6 +324,13 @@ class RoomServiceClient(
* @param okHttpConfigurator provide this if you wish to customize the http client
* (e.g. proxy, timeout, certificate/auth settings).
*/
@Deprecated(
"Use RoomServiceClient.createClient()",
ReplaceWith(
"RoomServiceClient.createClient(host, apiKey, secret, OkHttpFactory(logging, okHttpConfigurator))",
"import io.livekit.server.okhttp.OkHttpFactory",
)
)
@JvmStatic
@JvmOverloads
fun create(
Expand All @@ -331,15 +340,36 @@ class RoomServiceClient(
logging: Boolean = false,
okHttpConfigurator: Consumer<OkHttpClient.Builder>? = null
): RoomServiceClient {
val okhttp = with(OkHttpClient.Builder()) {
if (logging) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
addInterceptor(loggingInterceptor)
}
okHttpConfigurator?.accept(this)
build()
}
return createClient(
host = host,
apiKey = apiKey,
secret = secret,
okHttpSupplier = OkHttpFactory(
logging = logging,
okHttpConfigurator = okHttpConfigurator,
)
)
}

/**
* Create a RoomServiceClient.
*
* @param okHttpSupplier provide an [OkHttpFactory] if you wish to customize the http client
* (e.g. proxy, timeout, certificate/auth settings), or supply your own OkHttpClient
* altogether to pool resources with [OkHttpHolder].
*
* @see OkHttpHolder
* @see OkHttpFactory
*/
@JvmStatic
@JvmOverloads
fun createClient(
host: String,
apiKey: String,
secret: String,
okHttpSupplier: Supplier<OkHttpClient> = OkHttpFactory()
): RoomServiceClient {
val okhttp = okHttpSupplier.get()
val service = Retrofit.Builder()
.baseUrl(host)
.addConverterFactory(ProtoConverterFactory.create())
Expand Down
43 changes: 43 additions & 0 deletions src/main/kotlin/io/livekit/server/okhttp/OkHttpSupplier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.livekit.server.okhttp

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.util.function.Consumer
import java.util.function.Supplier

/**
* Holds an [OkHttpClient] object that is used as-is when supplying one.
*/
class OkHttpHolder(val okHttp: OkHttpClient) : Supplier<OkHttpClient> {
override fun get() = okHttp
}

/**
* Lazily creates and caches an [OkHttpClient] object.
*/
class OkHttpFactory(
/**
* When set to true, turns on body level logging.
*/
val logging: Boolean = false,
/**
* Provide this if you wish to customize the http client
* (e.g. proxy, timeout, certificate/auth settings)
*/
val okHttpConfigurator: Consumer<OkHttpClient.Builder>? = null
) : Supplier<OkHttpClient> {

val okHttp by lazy {
with(OkHttpClient.Builder()) {
if (logging) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
addInterceptor(loggingInterceptor)
}
okHttpConfigurator?.accept(this)
build()
}
}

override fun get() = okHttp
}
3 changes: 2 additions & 1 deletion src/test/kotlin/io/livekit/server/EgressServiceClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.livekit.server

import io.livekit.server.okhttp.OkHttpFactory
import livekit.LivekitEgress
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand All @@ -41,7 +42,7 @@ class EgressServiceClientTest {

@BeforeTest
fun setup() {
client = EgressServiceClient.create(HOST, KEY, SECRET, true)
client = EgressServiceClient.createClient(HOST, KEY, SECRET, OkHttpFactory(true))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.livekit.server

import io.livekit.server.okhttp.OkHttpFactory
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertNotNull
Expand All @@ -33,7 +34,7 @@ class IngressServiceClientTest {

@BeforeTest
fun setup() {
client = IngressServiceClient.create(HOST, KEY, SECRET, true)
client = IngressServiceClient.createClient(HOST, KEY, SECRET, OkHttpFactory(true))
}

@Test
Expand Down
9 changes: 7 additions & 2 deletions src/test/kotlin/io/livekit/server/RoomServiceClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

package io.livekit.server

import io.livekit.server.okhttp.OkHttpFactory
import livekit.LivekitModels
import kotlin.test.*
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class RoomServiceClientTest {

Expand All @@ -34,7 +39,7 @@ class RoomServiceClientTest {

@BeforeTest
fun setup() {
client = RoomServiceClient.create(HOST, KEY, SECRET, true)
client = RoomServiceClient.createClient(HOST, KEY, SECRET, OkHttpFactory(true, null))
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/io/livekit/server/WebhookReceiverTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class WebhookReceiverTest {
val testSecret = "ababababababababababababababababababababababababababababababa"

val token = AccessToken(testApiKey, testSecret)
token.sha256 = "CoEQz1chqJ9bnZRcORddjplkvpjmPujmLTR42DbefYI="
token.sha256 = "1renMMRYeCXsy6M9bjJ90XA3M1q1byhUGNoD91aPuhM="
val jwt = token.toJwt()

val receiver = WebhookReceiver(testApiKey, testSecret)
Expand Down

0 comments on commit 1c0c4e5

Please sign in to comment.