Getting Started: spray-client on sbt console

I've been playing around with spray for some time now. I was introduced to this framework while learning another framework that was sockoweb.

What got me hooked with spray is that it is 100% scala. One comparison would be the other framework uses an embedded server written in java (ie HTTP server) and unlike spray it implements its own known as spray-can (though you can still deploy your spray based REST services on any servlet container like tomcat and jetty).

I don't have to get deeper into the introduction of spray here as you can get more information by visiting their site at http://spray.io.

One of the many other modules that spray provides is an http client. In this post, we will try to prepare a minimal configuration to be able to use it on the sbt console.

Assumptions

This blog assumes that you have sbt installed preferably the latest one. I'm using 0.13.5 which is the latest stable release of sbt as of this writing.

You should also be comfortable with the terminal. This post is written using a unix system but can easily be followed (as it is very basic) on other operating system.

Let's begin

The fastest way to do this is to clone spray-template found on this link: https://github.com/spray/spray-template.git. Then you'll just update some dependencies to build.sbt and you're good to go but this is not what this post is about.

I'll do this manually and will attempt to make sense of each line as it is used.

We will begin by making an arbitrary directory. I will name it spray-client.

$ mkdir spray-client
$ cd spray-client

We will now define the build.sbt for this project.

$ touch build.sbt

Supply the entries below to the build.sbt

version := "0.1"
scalaVersion := "2.11.2"
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")
libraryDependencies ++= {
val akkaV = "2.3.5"
val sprayV = "1.3.1"
Seq(
"io.spray" %% "spray-client" % sprayV,
"io.spray" %% "spray-can" % sprayV,
"io.spray" %% "spray-http" % sprayV,
"io.spray" %% "spray-httpx" % sprayV,
"io.spray" %% "spray-util" % sprayV,
"io.spray" %% "spray-testkit" % sprayV % "test",
"com.typesafe.akka" %% "akka-actor" % akkaV % "provided",
"com.typesafe.akka" %% "akka-testkit" % akkaV % "test",
"org.specs2" %% "specs2-core" % "2.3.11" % "test"
)
}
Revolver.settings

If you tried cloning sbt-template, the entries here are pretty much what is in there. There are some difference though like spray-http, httpx and util. These are necessary because we are going to use spray-client and these artifacts are its dependencies.

Another interesting line here is Revolvers.settings. Try running this,

$ sbt

It will result with an error thrown.

[error] Type error in expression

That is because we need to add the revolver plugin. If you look into the root directory, you'll find a project directory. This is created when we attempted to run sbt.

Let's do the following,

$ cd project
$ echo 'addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.2")' > plugins.sbt
$ cd ..

The last line above will bring us back to the root directory. This time we'll try to run sbt again and expect to get into the shell.In the shell, let's get into scala console. These are the following line we did.

$ sbt
$ > console

Inside the console, let's do the following. This is based on this documentation http://spray.io/documentation/1.2.1/spray-client/.

scala > import spray.http._
scala > import spray.client.pipelining._
scala > import akka.actor.ActorSystem
scala > import system.dispatcher
scala > import scala.concurrent._
scala > val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
scala > val response: Future[HttpResponse] = pipeline(Get("http://spray.io/"))

Now we have a working base project that uses spray-client to make http request. What's next? Web scraper? Web service client with REST endpoints? Who knows? :)