A sample Scala/Spray API application which demonstrates how Spray can be used to create and consume RESTful APIs. This application exposes two features; timezone and elevation. The application uses Google APIs for the real work.
-
Clone the repository
-
In the base directory, start sbt
-
Compile the application by typing 'compile'
-
Run the application by typing 'run' or run the tests by typing 'test'
-
Use the api...Examples:
a) http://localhost:8080/api/ElevationService/39/80
b) http://localhost:8080/api/TimezoneService/39/-119/1331161200
- Spray 1.2-M8
- Akka 2.2.0-RC1
- Scala 2.10.2
- ScalaTest 2.0.M6
- Sbt 0.13.0
Here is a step-by-step explaination of what I did, so that you can use this to build your own APIs using Scala/Spray. Assumption: Use sbt, it's definitly the way to go.
- Add spray to your ./project/plugins.sbt file.
- Add the spray resolver and bring in the required dependencis to your build.sbt file.
name := "SprayApiDemo"
version := "0.1"
scalaVersion := "2.10.2"
scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")
resolvers ++= Seq(
"spray repo" at "http://repo.spray.io/"
)
libraryDependencies ++= {
val sprayVersion = "1.2-M8"
val akkaVersion = "2.2.0-RC1"
Seq(
"io.spray" % "spray-can" % sprayVersion,
"io.spray" % "spray-routing" % sprayVersion,
"io.spray" % "spray-testkit" % sprayVersion,
"io.spray" % "spray-client" % sprayVersion,
"io.spray" %% "spray-json" % "1.2.5",
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"com.typesafe.akka" %% "akka-testkit" % akkaVersion % "test",
"ch.qos.logback" % "logback-classic" % "1.0.12",
"org.scalatest" %% "scalatest" % "2.0.M7" % "test"
)
}
seq(Revolver.settings: _*)
- Create the Service App. Here we called it ./src/main/scala/Boot.scala. This will create your Actor system, and initialize the SprayApiDemoService class which is where our routings are maintained. This is also the entry point for your application.
- Write our tests. See ./src/test/scala/ElevationServiceSpec.scala as an example. Here were are using the FreeSpec trait from the ScalaTest library to help test our services.
- Create the Service Actor, which the App we created in step 3 will hand work off to. See ./project/src/main/scala/ElevationService.scala as an example. This will process the business logic. Here we also call other services to fulfill the request.
This demo application is a work in progress. The TimezoneService is not fully functional because I have not been able to figure out how to get HTTPS working with Google's APIs which require you to use HTTPS instead of HTTP.
- Add additional validation to provide more helpful messages when the user submit bad parameter values (e.g. Longitude great than 180)