Giter Site home page Giter Site logo

sprayapidemo's Introduction

SprayApiDemo

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.

Getting Started

  1. Clone the repository

  2. In the base directory, start sbt

  3. Compile the application by typing 'compile'

  4. Run the application by typing 'run' or run the tests by typing 'test'

  5. Use the api...Examples:

    a) http://localhost:8080/api/ElevationService/39/80

    b) http://localhost:8080/api/TimezoneService/39/-119/1331161200

Technologies Used

  1. Spray 1.2-M8
  2. Akka 2.2.0-RC1
  3. Scala 2.10.2
  4. ScalaTest 2.0.M6
  5. Sbt 0.13.0

What's Going On

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.

  1. Add spray to your ./project/plugins.sbt file.
  2. 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: _*)
  1. 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.
  2. 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.
  3. 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.

Known Issues

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.

What's on my ToDo List for this Project

  1. Add additional validation to provide more helpful messages when the user submit bad parameter values (e.g. Longitude great than 180)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.