如何创建 Ktor 客户端的层次结构

huangapple go评论64阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月27日 04:23:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定