Posts DateParamMatcher for Finagle

DateParamMatcher for Finagle

Twitter Finagle is an amazing framework for RPC communication, and my interest is its HTTP features for creating RESTful APIs.

As I bloged at codemountain, I’m using finagle for a personal project. Don’t know how far it will go, but the facts is that I’m learning Finagle a lot.

During this project I needed to extract from the query string, a param to be parsed to a twitter.util.Time. I ended up with a Param Matcher to be used my a pattern match agains the requested path. The use of this Param Patcher is:

Path(request.path) :? request.params match {
       case Root / "myPath" :? RequestTime(requestTimeParam)  => //do something
//Where RequestTime is defined as
 object RequestTime extends DateParamMatcher("requestTimeParam")

The source of DateParamMatcher is the following.

import com.twitter.util.Time._
import com.twitter.util.Time
import java.text.ParseException
import com.twitter.finagle.http.path._
import com.twitter.finagle.http.ParamMap

 * Extractor that takes a request parameter and converts it to a {twitter.util.Time}.
 * The date format should conform with {twitter.util.Time} default format.
abstract class DateParamMatcher(name: String) {

  def unapply(params: ParamMap): Option[Time] = params.get(name) flatMap { value =>
    try {
    } catch {
      case ex: ParseException =>

Finagle also provides an abstract com.twitter.finagle.http.path.ParamMatcher as well as LongParamMatcher and IntParamMatcher in the same package. They are actually Scala Extractors (very well explained here by @codemonkeyism). This is a practical way to extract values from a query string and put it available at your code already parsed.

Note that if the param named requestTimeParam is not present or carries a invalid value for Time in the query string the None value is returned.

Although easy to build and extract params from query string and requested path, it is not a Finagle strength. It is kinda limited. A better abstraction is offered by Unfiltered(another @n8han’s great job).

Hope you enjoy it.

This post is licensed under CC BY 4.0 by the author.