Hey I was wondering why http4k Request is just an ...
# http4k
r
Hey I was wondering why http4k Request is just an inteface that uses invokes and creates data classes instead if having an implementation of an interface too? I mean if I want to create a custom request with very few changes I still have to override everything on the interface and create them myself :X
d
As far as I remember, we started with a data class but moved to an interface when we introduced RoutedRequest, which also contains the template that the request was matched upon - this allows us to extract the named path parameters later
👍 1
Out of interest - are you actually trying to create your own request, and if so what's the use case?
s
@Razi Kheir we do have implementations for the interface (InMemory/Routed). The
invoke
in the interface is to make the interface act as a factory. Regarding creating a custom request, yes, you need to override the whole interface because Request is expected to be immutable, so on every change you need to create a new instance of your specific implementation.
👍 1
d
@Razi Kheir but also, you can use Kotlin's delegating feature to do something like this (as we do here to reuse pretty much all of the code that exists already):
Copy code
data class RoutedRequest(private val delegate: Request, val xUriTemplate: UriTemplate) : Request by delegate
👍 1
r
I just want to have a non final test class that will be easily testable/stubbable is all, otherwise I wouldn’t have minded 🙂
As always thanks a lot ! 🙂
d
Because the implementations are data classes (which provide equals methods automatically) , you can easily use the http message objects in assertions. We've never needed to mock or stub them - if you post an example of were you might want to do that then we can probably suggest an alternate approach (and we've written a LOT of tests using these objects over the years 😉)
👍 1