Skip to content

Commit

Permalink
feat(headers): Added an X-Proxy-Cache header
Browse files Browse the repository at this point in the history
  • Loading branch information
dbousamra committed Jul 27, 2017
1 parent ed73190 commit 6566913
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.imageintelligence.http4c.headers

import scalaz._, Scalaz._
import org.http4s.util.Writer
import org.http4s.dsl._
import org.http4s._

final case class `X-Proxy-Cache`(cacheHit: Boolean) extends Header.Parsed {
override def key = `X-Proxy-Cache`
override def renderValue(writer: Writer): writer.type = {
if (cacheHit) {
writer.append("HIT")
} else {
writer.append("MISS")
}
}
}

object `X-Proxy-Cache` extends HeaderKey.Singleton {
type HeaderT = `X-Proxy-Cache`
def name = "X-Proxy-Cache".ci

def parse(s: String): ParseResult[`X-Proxy-Cache`] = {
s match {
case "HIT" => `X-Proxy-Cache`(true).right
case "MISS" => `X-Proxy-Cache`(false).right
case other => {
val e = s"$other is not a valid X-Proxy-Cache header"
ParseFailure.apply(e, e).left
}
}
}

def matchHeader(header: Header): Option[`X-Proxy-Cache`] = {
if (header.name == name)
parse(header.value).toOption
else None
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.imageintelligence.http4c.examples

import com.imageintelligence.http4c.DslHelpers
import com.imageintelligence.http4c.headers.`X-Proxy-Cache`
import org.http4s.ParseFailure
import org.http4s.HttpService
import org.http4s.dsl._
Expand Down Expand Up @@ -37,7 +38,7 @@ object ExampleUserService {
val service = HttpService {
case req @ GET -> Root / UserIdMatcher(userId) :? GenderQueryParam(gender) => {
userId match {
case \/-(id) => Ok(id.value)
case \/-(id) => Ok(id.value).putHeaders(`X-Proxy-Cache`(false))
case -\/(error) => BadRequest(error)
}
}
Expand Down

0 comments on commit 6566913

Please sign in to comment.