akka/akka-http

Support for Query Parameter Flags

オープン

#2,171 opened on 2018/08/22

 (2 件のコメント) (3 件のリアクション) (0 人の担当者)Scala (598 件のフォーク)batch import
1 - triagedhelp wantednice-to-have (low-prio)t:routingt:server

Repository metrics

Stars
 (1,311 個のスター)
PR merge metrics
 (平均マージ 1d 10h) (30d で 2 merged PRs)

説明

Perhaps I'm missing something obvious, but what I'd like is to have a route that refines some API behavior based on the presence or value of a query parameter.

For example

// These do a dry-run
https://example.com/submit?dryrun
https://example.com/submit?dryrun=true
https://example.com/submit?dryrun=yes
https://example.com/submit?dryrun=1

// These do the real thing
https://example.com/submit
https://example.com/submit?dryrun=false
https://example.com/submit?dryrun=no
https://example.com/submit?dryrun=0

Unfortunately, I haven't found a clean way of using the built-in directives:

// Breaks /submit?dryrun
parameter('dryrun.as[Boolean] ? false)

// Breaks /submit
parameter('dryrun.as[Boolean] ? true)

The best I've come up with is my own directive, which you all can probably improve on:

def parameterFlag(name: Symbol): Directive1[Boolean] =
  (parameter(name.?) & parameter(name.as[Boolean] ? true)) tmap {
    case(flag, isTruthy) => flag.isDefined && isTruthy
  }

parameterFlag('dryrun)

Thoughts?

コントリビューターガイド