英文:
How to create hierarchy of ktor clients
问题
来自Okhttp,我习惯于创建多个HTTP客户端实例,其中每个实例“继承”来自父实例的行为(并添加自己的行为),通过调用 okHttpClient.newBuilder()
(这比仅仅共享设置更好,因为它共享了线程池等,如果可能的话)
如何在Ktor客户端中实现相同的功能?
我注意到有 install(httpClient: HttpClient)
这个方法似乎是我想要的,但事实证明是相反的(它将“this”行为应用于客户端参数)
我希望在配置子客户端时继承父客户端的行为
或者在Ktor中不习惯这样做吗?
英文:
Coming from Okhttp, I'm used to creating multiple instances of the http client, where each "inherits" behavior from the parent one (and adding own behavior), by calling okHttpClient.newBuilder()
(which is better than just sharing setup, as it shared the threadpools etc if possible)
How to do the same with ktor client?
I notice there is install(httpClient: HttpClient)
which seems like what I want, but turns out the the otherway around (it applies "this" behavior to the client argument)
I want to inherit parent client behavior when configuring the child one
Or is this not idiomatic in ktor?
答案1
得分: 1
HttpClient.config 方法创建一个基于现有客户端配置的新HttpClient
实例,同时提供了自定义的配置。
此外,您可以使用HttpClientEngineFactory.config 方法基于现有配置创建一个新的引擎配置。
英文:
The HttpClient.config method creates a new HttpClient
instance with a provided configuration based on an existing client configuration.
Also, you can create a new engine configuration based on an existing one using the HttpClientEngineFactory.config method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论