Created
February 24, 2016 10:26
-
-
Save megaherz/c66c5dfa97d5f1a35cb0 to your computer and use it in GitHub Desktop.
Get timezone offset with Google timezone API using Play2
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
package services | |
import play.api.libs.ws._ | |
import play.api.libs.concurrent.Execution.Implicits.defaultContext | |
import scala.concurrent.Future | |
class TimezoneProvider { | |
def apply(latitude: Double, longitude: Double)(implicit client: WSClient): Future[Either[Error, Long]] = { | |
val url: String = s"https://maps.googleapis.com/maps/api/timezone/json?location=${latitude},${longitude}×tamp=0" | |
val response = WS.clientUrl(url).get() | |
response.map { | |
response => { | |
val status = (response.json \ "status").as[String] | |
if (status == "OK") { | |
Right((response.json \ "rawOffset").as[Long]) | |
} | |
else | |
Left(new Error(s"Error status ${status}")) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The gist is for Play2 v.2.4 or higher. It requires
libraryDependencies ++= Seq (ws)
in build.sbt