https://kotlinlang.org logo
#ktor
Title
# ktor
p

Paul Griffith

02/17/2021, 6:51 PM
Is there a more idiomatic way to have a wrapper around an HttpClient that sets up some default features, but also allows some use-site customization? More in a thread, but basically I want to be able to create an
XClient
which might be backed by a
CIOEngine
...but also be able to use the same class but with a
MockEngine
, which requires being able to add handlers
I've got an abstract class like this
Then impl classes look like this (adding additional features)
Which then allows customization at the use site like this:
But the stuff at the abstract class feels very weird and unergonomic
Guess I answered my own question, making the second parameter on the abstract class
Copy code
engineCustomization: EngineType.() -> Unit = {},
which then makes the client initialization
Copy code
protected val client: HttpClient by lazy {    HttpClient(clientSupplier.create(engineCustomization), clientConfiguration)
}
makes things a lot nicer to look at
Just kidding, that can even be reduced further, because HttpClientEngineFactory has a
create
extension function, so you don't need to provide a configuration lambda at all
2 Views