Skip to content

Instantly share code, notes, and snippets.

@bandrzejczak
Created February 9, 2018 22:56
Show Gist options
  • Save bandrzejczak/159544fe4e9f39255c8936f9bd3e16b5 to your computer and use it in GitHub Desktop.
Save bandrzejczak/159544fe4e9f39255c8936f9bd3e16b5 to your computer and use it in GitHub Desktop.
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