[Ktor] Add MustBeClosed annotation to buildKtor method (#1829)

Add @MustBeClosed annotation to buildKtor method

This commit adds the `@MustBeClosed` annotation to the `buildKtor` method in `HttpClientBuilder.kt` to indicate that the returned `HttpClient` instance must be closed by the caller. It also updates the test in `HttpClientBuilderTest.kt` to use the `use` function to ensure proper resource management.
This commit is contained in:
Ricki Hirner
2025-11-21 12:48:04 +01:00
committed by GitHub
parent babd52cfb1
commit 5263172376
2 changed files with 9 additions and 5 deletions

View File

@@ -55,10 +55,11 @@ class HttpClientBuilderTest {
.setResponseCode(200) .setResponseCode(200)
.setBody("Some Content")) .setBody("Some Content"))
val client = httpClientBuilder.get().buildKtor() httpClientBuilder.get().buildKtor().use { client ->
val response = client.get(server.url("/").toString()) val response = client.get(server.url("/").toString())
assertEquals(200, response.status.value) assertEquals(200, response.status.value)
assertEquals("Some Content", response.bodyAsText()) assertEquals("Some Content", response.bodyAsText())
}
} }
@Test @Test

View File

@@ -19,6 +19,7 @@ import at.bitfire.davdroid.settings.Settings
import at.bitfire.davdroid.settings.SettingsManager import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.ui.ForegroundTracker import at.bitfire.davdroid.ui.ForegroundTracker
import com.google.common.net.HttpHeaders import com.google.common.net.HttpHeaders
import com.google.errorprone.annotations.MustBeClosed
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp import io.ktor.client.engine.okhttp.OkHttp
@@ -202,7 +203,6 @@ class HttpClientBuilder @Inject constructor(
* *
* @throws IllegalStateException on second and later calls * @throws IllegalStateException on second and later calls
*/ */
@Deprecated("Use buildKtor instead", replaceWith = ReplaceWith("buildKtor()"))
fun build(): OkHttpClient { fun build(): OkHttpClient {
if (alreadyBuilt) if (alreadyBuilt)
throw IllegalStateException("build() must only be called once; use Provider<HttpClientBuilder>") throw IllegalStateException("build() must only be called once; use Provider<HttpClientBuilder>")
@@ -378,7 +378,10 @@ class HttpClientBuilder @Inject constructor(
* *
* However in this case the configuration of `client1` is still in `builder` and would be reused for `client2`, * However in this case the configuration of `client1` is still in `builder` and would be reused for `client2`,
* which is usually not desired. * which is usually not desired.
*
* @return the new HttpClient (with [OkHttp] engine) which **must be closed by the caller**
*/ */
@MustBeClosed
fun buildKtor(): HttpClient { fun buildKtor(): HttpClient {
if (alreadyBuilt) if (alreadyBuilt)
throw IllegalStateException("build() must only be called once; use Provider<HttpClientBuilder>") throw IllegalStateException("build() must only be called once; use Provider<HttpClientBuilder>")