Skip to content

Instantly share code, notes, and snippets.

@bandrzejczak
Created February 9, 2018 22:55
Show Gist options
  • Save bandrzejczak/2f25a0fa38df6fec53b8e3f04afbe56e to your computer and use it in GitHub Desktop.
Save bandrzejczak/2f25a0fa38df6fec53b8e3f04afbe56e to your computer and use it in GitHub Desktop.
private def getUserId(username: String, token: String): Future[String] = {
import io.circe.generic.auto._
sttp
.get(uri"${config.authServerUrl}/admin/realms/${config.realm}/users?username=$username")
.auth.bearer(token)
.response(asJson[List[UserResponse]])
.send()
.flatMap {
_.body match {
case Left(error) => Future.failed(new RuntimeException(error))
case Right(Left(circeError)) => Future.failed(circeError)
case Right(Right(user :: Nil)) => Future.successful(user.id)
case _ => Future.failed(new RuntimeException(s"Couldn't find a single user with username $username"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment