Created
June 16, 2014 01:42
-
-
Save krishnabhargav/fc17f4f7c54e938dd5d4 to your computer and use it in GitHub Desktop.
Making a REST Service call in Scala using Dispatch
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
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 away my key") | |
.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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment