ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
12.82k stars 1.04k forks source link

What to use in place of deprecated takeFrom(HttpRequestBuilder)? #1626

Closed brewin closed 4 years ago

brewin commented 4 years ago

With HttpRequestBuilder.takeFrom(HttpRequestBuilder) deprecated in 1.3.1, what's the recommended way to reuse common request data?

dmitrievanthony commented 4 years ago

Hi @brewin,

We initially assumed HttpRequestBuilder.takeFrom as an internal method. Starting from 1.3.1 started to copy executionContext of the initial request builder and so that became a bit more "dangerous" so we marked it as deprecated.

Could you describe your use case for me to understand how we can bypass the problem?

brewin commented 4 years ago

I may have been using it incorrectly then. My use case is like this:

class SomeDataSource(private val httpClient: HttpClient) {

    private val requestBuilder = HttpRequestBuilder().apply {
        url {
            protocol = URLProtocol.HTTPS
            host = "example.com"
        }
        parameter("param_shared_by_all_requests", 42)
    }

    suspend fun getSomething(): Something = httpClient.get {
        takeFrom(requestBuilder)
        parameter("foo", "bar")
    }

    suspend fun getSomethingElse(): SomethingElse = httpClient.get {
        takeFrom(requestBuilder)
        parameter("baz", true)
    }
}
brewin commented 4 years ago

Added back in 1.3.2. Thanks.