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
dave
07/04/2019, 5:44 AM
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
dave
07/04/2019, 5:46 AM
Out of interest - are you actually trying to create your own request, and if so what's the use case?
s
s4nchez
07/04/2019, 7:24 AM
@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
dave
07/04/2019, 7:26 AM
@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
Razi Kheir
07/05/2019, 12:26 AM
I just want to have a non final test class that will be easily testable/stubbable is all, otherwise I wouldn’t have minded 🙂
Razi Kheir
07/05/2019, 12:27 AM
As always thanks a lot ! 🙂
d
dave
07/05/2019, 4:58 AM
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 😉)