Spraying Rest Api’s With Spray

Spraying Rest Application With Spray

Spray is a suite of lightweight Scala libraries providing client- and server-side REST/HTTP support on top of Akka. Akka makes the application fully asynchronous. There are other frameworks build on top of Akka such as Play that gives the ability to build a full web application, but with one down side and that is application becomes highly coupled with the framework as the application is built on top of the play. As oppose to salt spray gives the ability to create Application layer independent of anything without even thinking of the framework. Spray will just work as an  integration layer to shake hands between the outer world and the application. Outer world could be any web,mobile or third party applications.

In this blog post we will see how to setup spray environment for the scala application and also learn about some of the building blockes.In the upcoming post we will see a simple example of writing rest Api’s, authentication support, cross origin support and actor handling also most important part of the development unit test spray routes.

Table of content


Basic setup

To start with we need to firstly create the sbt project.

build.sbt consists of following lines
name := “SprayingRestWithSpray”

version := “1.0”

libraryDependencies ++= {
val akkaV = “2.3.6”
val sprayV = “1.3.2”
“io.spray” %% “spray-can” % sprayV,
“io.spray” %% “spray-routing” % sprayV,
“com.typesafe.akka” %% “akka-actor” % akkaV

We are using akka version 2.3.6 and spray version 1.3.2
we have defined 3  libraries  spray-can ,spray-routing and akka-actor. These are sufficient to start building spray application. We will keep adding other spray libraries as and when needed. This is the beauty of spray that you use only what you needed.

Let's understand why these libraries are for
<a id="spraycan"></a>
### Spray-can :
A low-level, low-overhead, high-performance HTTP server and client built on top of spray-io. When spray-can server get started,It starts a new listener actor which can handle thousands of  incoming requests asynchronously.

<a id="sprayrouting"></a>
### Spray-routing:
 A high-level routing DSL for elegantly defining RESTful web services. It provides abstraction layer over the spray-http . It saves developers efforts of writing boiler plate code and provide them the simple interface to build  the application.

 Now we will start doing the real action by creating the Main object which starts the server.

object Main extends App {

 implicit val system = ActorSystem("SprayingRestWithSpray")
 val apiRouteHandlerActor = system.actorOf(Props[RequestHandlerActor], "application-actor")
 (IO(Http) ! Http.Bind(apiRouteHandlerActor, interface = "localhost", port = 8080))


 What we just did is bind the apiRouteHandlerActor  to the http port 8080 on localhost. Any subsequent request  will be passed on to the apiRouterHandler in the form of RequestContext message.

Now we will create RequestHandlerActor

class RequestHandlerActor extends HttpServiceActor{
override def receive: Receive = runRoute(route)
val route:Route = path(“firstRoute”){
complete(“Request completed”)

RequestHandlerActor extends HttpServiceActor which extends HttpService.

HttpService provides execution context to the request which it receives.
In the example above we have override receive method with partial function runRoute of HttpService which takes Route type as parameter. In spray-routing Route has following form:

type Route = RequestContext => Unit

All the structure you define will be subtype of Route.

<a id="directives"></a>
## Directives
We can combine multiple such structure to form a complex Route. These building blocks are termed as Directive in spray. Spray comes up with predefined Directives for handling almost every type of request one can encounter. But we can create our own Directives if required by modifying or combining these Directives.

One such Directive is path Directive, which matches the part of uri.
In the example above our path directive matches the path "firstRoute". On encountering request localhost:8080/firstRoute request will be forwarded to this Directive and the request is completed by  complete method of RequestContext.

The complete method creates an HttpResponse an sends it to responder of the RequestContext.

You can find out sample code [here][Code_link]

<a id="conclusion"></a>
## Conclusion

In this post we explored how to set up spray application,learned about basic ingredients of the spray.In my upcoming  blog we will deep dive in detail and we will be using these ingredients to create more stuff about Rest Api's through spray.

Leave a Reply

Your email address will not be published. Required fields are marked *