Created
February 9, 2018 22:56
-
-
Save bandrzejczak/159544fe4e9f39255c8936f9bd3e16b5 to your computer and use it in GitHub Desktop.
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
private def extractTokenAndExpiration(response: Response[String]): Option[(String, Duration)] = { | |
for { | |
location <- response.headers.find(_._1 == "Location").map(_._2) | |
queryParams <- extractQueryParams(location) | |
token <- queryParams.get("access_token") | |
expiration <- queryParams.get("expires_in").map(parseExpirationTime) | |
} yield (token, expiration) | |
} | |
private def extractQueryParams(location: String): Option[Map[String, String]] = { | |
val uri: Uri = uri"$location" | |
uri.fragment.map { fragment => | |
fragment | |
.split("&") | |
.toSeq | |
.map(_.split("=").toSeq) | |
.map { case Seq(x, y) => (x, y) } | |
.toMap | |
} | |
} | |
private def parseExpirationTime(timeInSeconds: String): Duration = { | |
timeInSeconds.toLong.seconds | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment