Copyright 2015 PagerDuty, Inc.
fun-http-client is a small, asynchronous, and functional interface to the Akka HTTP client.
The primary differences between this interface and the default one provided by Akka HTTP are:
- Functional-style error handling where the error type is restricted to (sub-classes of) a particular type
- Request logging as pure data
- Simplification: we support the simple case of a single request resulting in a single response, which we consume strictly (but not synchronously).
It makes sense to use fun-http-client if you need an HTTP client in a project already employing Akka.
- Akka HTTP - For the HTTP engine and data models
- Scalaz - For monad transformer definitions and the disjunction (
- ScalaCheck - For property based testing
- ScalaTest - For unit tests
The current version of fun-http-client is 2.1.0.
fun-http-client is available for Scala 2.10 and 2.11.
libraryDependencies += "com.pagerduty" %% "fun-http-client" % "2.1.0"
fun-http-client is written by Jesse Haber-Kucharsky (firstname.lastname@example.org).
Please report issues in the issue tracker on GitHub.
Tests are executed via SBT.
In a terminal:
$ sbt test
Running the example
Switch to the
example project and run the
$ sbt > project example > run
fun-http-client introduces the
HttpTask[E, A] type, which is an asynchronous computation that involves zero or more HTTP requests. The task has one of two outcomes: either an error of type
E, or a result of type
HttpTask[E, A] computations also keep a log of HTTP requests that are made (and how long they take).
HttpTask[E, A] computations can be combined and manipulated with familiar functions like
Error handling is explicit, by-value, and recoverable with
When it's time to do something with the result of a computation, it can be converted to a standard Scala
asFutureWithLog: Future[(RequestLog, E \/ A)] or
asFuture: Future[E \/ A].
Every HTTP request made through fun-http-client via
Http[E].request logs a
LogEntry with this signature:
case class LogEntry private ( request: HttpRequest, initiationTime: Time, completed: Boolean, duration: Option[Duration])
The request log is just
type RequestLog = Vector[LogEntry].
Building HTTP computations
Http[E] trait provides combinators for building
Http[String].unit(3) results in
HttpTask[String, Int]. We sneakily accessed a new instance of
Http[String] via the function
Using the library
There is a complete worked example in
example/JsonTest.scala which is described in the tutorial.