https://kotlinlang.org logo
Title
r

Ricardo

07/17/2019, 2:40 PM
Hey I wonder if anyone is on-line to answer a quick question about the http4k contract library?
d

dave

07/17/2019, 2:51 PM
Shoot! 🙃
r

Ricardo

07/17/2019, 2:53 PM
Ha thanks David!
Just looking at the
bindContract
function vs the
bind
function and it appears the former does not create a
HttpHandler
.
d

dave

07/17/2019, 2:54 PM
(for reference, Londpn office hours are when were generally about )
r

Ricardo

07/17/2019, 2:54 PM
Is this deliberate?
Ah OK, cheers!
d

dave

07/17/2019, 2:55 PM
Yes. That will create a contract route, which sits inside a contract block. It's the contract block which creates the handler
r

Ricardo

07/17/2019, 2:56 PM
It just would be nice if
ContractRoute
was also an
HttpHandler
.
It means we can test an endpoint without running it through an application or a contract block.
From a design perspective, is there a reason why this isn't the case?
d

dave

07/17/2019, 2:58 PM
You can use a very simple contract block (literally just
contract { routes+= <cr> }
) to test it
But I'm not sure that the machinery of the contract routing would allow it to work because it lives outside.
From a design perspective - not really TBH. Just the way it worked out. I haven't got the code in front of me at the mo so I can't check easily 🙃
But it is a valid question - maybe we can look at if it would be possible (I am sceptical though)
I'm afk, but try adding this and see if it would work
operator fun ContractRoute.invoke(request:Request)  = toRouter(Root)?.match(request) ?: Response(NOT_FOUND)
@Ricardo one thing with that though - security will not be applied as it's done at the routing layer - you do have access in the extension function to get the security filter though so it could also be applied inside the invoke method
(with a tweak)
r

Ricardo

07/17/2019, 3:26 PM
I have actually modified the
ContractRoute
in a branch. Should I send a PR?
d

dave

07/17/2019, 3:41 PM
Yep - thinking about it I think we have all the correct machinery to do it in a more consistent fashion (so we might not end up using it exactly as your PR although I will likely merge it), but let's see what you've got already. 🙃 (We'll deffo give you a credit in the changelog anyway) Thanks 🙃
r

Ricardo

07/17/2019, 3:45 PM
This is what I came up with:
It's probably a bit duff
d

dave

07/17/2019, 3:49 PM
@Ricardo doesn't matter - we can refactor if it needs bashing into shape 😉! The important thing is that it's a great idea and we should definitely do it if we can. 🙃
(although am on hols at the moment, so it'll probably be next week before merging it in)
r

Ricardo

07/17/2019, 3:52 PM
Cool enjoy your hols
Thanks for the help
🏖️ 1