Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract a header value or return default #639

Closed
zettelmj opened this issue Dec 10, 2016 · 8 comments
Closed

Extract a header value or return default #639

zettelmj opened this issue Dec 10, 2016 · 8 comments
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on t:docs Issues related to the documentation
Milestone

Comments

@zettelmj
Copy link
Contributor

zettelmj commented Dec 10, 2016

I some situation it would be handy to for the headerValue* directives to allow to specify a default value that is used if the header is not present.

for headerValueByName this could look like this:

def headerValueByName(headerName: String, default: String): Directive1[String] = headerValue(optionalValue(headerName.toLowerCase)) | provide(default)

@jrudolph
Copy link
Member

Hi @zettelmj, thanks for the suggestion.

I wonder if the new directive would be general enough to warrant adding it to the library of directives. Using

(headerValue(headerName) | provide(default))

directly, instead, seems to be more explicit to me.

@jrudolph jrudolph added the 0 - new Ticket is unclear on it's purpose or if it is valid or not label Dec 12, 2016
@zettelmj
Copy link
Contributor Author

Hi @jrudolph,

that looks way better than what i tried over the weekend. Passing a function down through the directives makes all of the look very convoluted. Additionally it changes a bunch of signatures.
I'll give the "provide" directive a try.

Cheers
Jens

@ktoso
Copy link
Member

ktoso commented Dec 12, 2016

@ktoso ktoso added t:docs Issues related to the documentation 1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on and removed 0 - new Ticket is unclear on it's purpose or if it is valid or not labels Dec 12, 2016
@ktoso ktoso added this to the 10.0.x milestone Dec 12, 2016
@jlprat
Copy link
Member

jlprat commented Dec 12, 2016

I would say for the Java DSL is not that straight forward, unless #620 is done

@jrudolph
Copy link
Member

@jlprat agreed.

@zettelmj
Copy link
Contributor Author

@ktoso just putting it into headerValue feels a bit strange cause it is effectively just an application of http://doc.akka.io/docs/akka-http/current/scala/http/routing-dsl/directives/index.html#composing-directives.

There are some examples in composing-directives but they aren't covering this scenario.
Maybe a sub page of examples what can be done by composing patterns would be good?!

Thank you
Jens

@ktoso
Copy link
Member

ktoso commented Dec 14, 2016

Page with patterns would also be good, but this one I think is basically a headerValue "or else..." which is why I think it makes sense to add to its page. We have examples of directives which have such additional documentation (simply as section inside their docs) :)

@jrudolph
Copy link
Member

In #666, @zettelmj added the example to the Scala documentation. Would make sense to add something similar to the Java documented, not least for consistency.

jlprat added a commit to jlprat/akka-http that referenced this issue Mar 29, 2017
Issue: akka#639
Java example on how to provide a default value for a header extractor
using directive composition
@jrudolph jrudolph modified the milestones: 10.0.6, 10.0.x Mar 30, 2017
tomrf1 pushed a commit to tomrf1/akka-http that referenced this issue Aug 13, 2017
Java example on how to provide a default value for a header extractor
using directive composition

Fixes akka#639
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted help wanted Identifies issues that the core team will likely not have time to work on t:docs Issues related to the documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants