Getting Started: spray-client on sbt console08 Oct 2014
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.
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.
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
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,
It will result with an error thrown.
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,
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.
Inside the console, let's do the following. This is based on this documentation http://spray.io/documentation/1.2.1/spray-client/.
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? :)