finch icon indicating copy to clipboard operation
finch copied to clipboard

Header-Endpoint that only matches specific values, akin to path

Open d-s-d opened this issue 6 years ago • 6 comments

It would be convenient to have a header-endpoint that matches iff a header is present with a particular value. This is useful in situations where one wants to provide different business logic depending on that value. An example of this is API-Versioning: Let's assume that such an endpoint-factory would exist and header(x, y) matched iff header x was present with value y. Then one could solve this problem as follows:

val v1Endpoint: Endpoint[ResponseV1] =
  get("myEndpoint" :: header("X-API-VERSION", "1")) { /* ... */ }
val v2Endpoint: Endpoint[ResponseV2] =
  get("myEndpoint" :: header("X-API-VERSION", "2")) { /* ... */ }
val allVersions = v1Endpoint :+: v2Endpoint

d-s-d avatar Sep 16 '18 13:09 d-s-d

Perhaps headerExact("foo", "bar") could be a reasonable API to expose (that's what warp uses)?

vkostyukov avatar Sep 18 '18 06:09 vkostyukov

@d-s-d Let me know if you're willing to contributed this new endpoint instance and I will move the ticket to appropriate milestone.

vkostyukov avatar Oct 02 '18 21:10 vkostyukov

@vkostyukov Ok. I'm new to this. Till when would you like to see a PR?

d-s-d avatar Oct 03 '18 19:10 d-s-d

There is no deadline whatsoever. Whenever you have time.

vkostyukov avatar Oct 03 '18 19:10 vkostyukov

@vkostyukov @d-s-d I just started some work on this one, but it looks like a bit of inconsistency to me. At the moment we always match the header endpoint. If header(name) is missing in the headers error is raised. While headerExact is supposed to return EndpointResult.NotMatched instead

sergeykolbasov avatar Dec 06 '18 21:12 sergeykolbasov

@sergeykolbasov That is the point, I think. Similar to the path, this endpoint can be used to dispatch a request. Maybe a different name would do: headerMatch, or headerDispatch. Or did I misunderstand your remark?

d-s-d avatar Dec 08 '18 20:12 d-s-d