Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 52631723 authored by Ricki Hirner's avatar Ricki Hirner Committed by GitHub
Browse files

[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.
parent babd52cf
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -55,11 +55,12 @@ class HttpClientBuilderTest {
            .setResponseCode(200)
            .setBody("Some Content"))

        val client = httpClientBuilder.get().buildKtor()
        httpClientBuilder.get().buildKtor().use { client ->
            val response = client.get(server.url("/").toString())
            assertEquals(200, response.status.value)
            assertEquals("Some Content", response.bodyAsText())
        }
    }

    @Test
    fun testCookies() {
+4 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import at.bitfire.davdroid.settings.Settings
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.ui.ForegroundTracker
import com.google.common.net.HttpHeaders
import com.google.errorprone.annotations.MustBeClosed
import dagger.hilt.android.qualifiers.ApplicationContext
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
@@ -202,7 +203,6 @@ class HttpClientBuilder @Inject constructor(
     *
     * @throws IllegalStateException    on second and later calls
     */
    @Deprecated("Use buildKtor instead", replaceWith = ReplaceWith("buildKtor()"))
    fun build(): OkHttpClient {
        if (alreadyBuilt)
            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`,
     * which is usually not desired.
     *
     * @return the new HttpClient (with [OkHttp] engine) which **must be closed by the caller**
     */
    @MustBeClosed
    fun buildKtor(): HttpClient {
        if (alreadyBuilt)
            throw IllegalStateException("build() must only be called once; use Provider<HttpClientBuilder>")