Hey I wonder if anyone is on-line to answer a quic...
# http4k
r
Hey I wonder if anyone is on-line to answer a quick question about the http4k contract library?
d
Shoot! 🙃
r
Ha thanks David!
Just looking at the
bindContract
function vs the
bind
function and it appears the former does not create a
HttpHandler
.
d
(for reference, Londpn office hours are when were generally about )
r
Is this deliberate?
Ah OK, cheers!
d
Yes. That will create a contract route, which sits inside a contract block. It's the contract block which creates the handler
r
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
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
Copy code
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
I have actually modified the
ContractRoute
in a branch. Should I send a PR?
d
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
This is what I came up with:
It's probably a bit duff
d
@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
Cool enjoy your hols
Thanks for the help
🏖️ 1