Created
June 16, 2014 01:57
-
-
Save krishnabhargav/762b7f6456bd4c91414b to your computer and use it in GitHub Desktop.
Scala + REST + JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Imports for Dispatch | |
import dispatch._ | |
import Defaults._ | |
object Places { | |
def main(args: Array[String]) { | |
//Step 1 : Prepare the request object | |
//even though its a https . doing a .secure is not required | |
val request = url("https://maps.googleapis.com/maps/api/place/nearbysearch/json") | |
val requestAsGet = request.GET //not required but lets be explicit | |
//Step 2 : Set the required parameters | |
val builtRequest = requestAsGet.addQueryParameter("key", "Almost gave this away") | |
.addQueryParameter("location", "40.5556204,-74.4162536") | |
.addQueryParameter("radius", "10000") | |
.addQueryParameter("sensor", "false") | |
.addQueryParameter("keyword", "starbucks") | |
//Step 3: Make the request (method is already set above) | |
val content = Http(builtRequest) | |
//Step 4: Once the response is available | |
//response completed successfully | |
content onSuccess { | |
//Step 5 : Request was successful & response was OK | |
case x if x.getStatusCode() == 200 => | |
//Step 6 : Response was OK, read the contents | |
handleJsonOutput(x.getResponseBody) | |
case y => //Step 7 : Response is not OK, read the error | |
println("Failed with status code" + y.getStatusCode()) | |
} | |
//Step 7 : Request did not complete successfully, read the error | |
content onFailure { | |
case x => | |
println("Failed but"); println(x.getMessage) | |
} | |
println("ENTER TO EXIT") | |
readLine() | |
} | |
//CASE CLASSES | |
case class Location(lat: Double, lng: Double) | |
case class Geometry(location: Location) | |
case class RunningStatus(open_now: Boolean) | |
case class Results(geometry: Geometry, icon: Option[String], id: Option[String], name: Option[String], | |
opening_hours: Option[RunningStatus], price_level: Option[Double], rating: Option[Double], | |
reference: Option[String], types: Option[List[String]], vicinity: Option[String]) | |
case class RootJsonObject(results: List[Results]) | |
//JSON imports | |
import org.json4s.jackson.Serialization | |
import org.json4s.jackson.Serialization.{ read } | |
import org.json4s.NoTypeHints | |
private def handleJsonOutput(body: String): Unit = { | |
//required to set implicit here for JSON serialization to work properly | |
implicit val formats = Serialization.formats(NoTypeHints) | |
//read the output as a RootJsonObject instance | |
//the read call does the trick of mapping JSon to the case classes | |
val output = read[RootJsonObject](body) | |
println(s"Total Results: ${output.results.size}") | |
for (each <- output.results) | |
println(s"${each.name.get} at ${each.vicinity.get}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment