Akka HTTP和Spray提供了一个authenticateOAuth2
指令,但他们的文档说明了这一点
该指令不实现完整的OAuth2协议,而是通过从HTTP头中提取所需的令牌来实现它.
我也找不到任何为Akka HTTP或Spray实现OAuth2的库.有什么我想念的,或者这只是现在这些库的状态?
在Scala中,我可以Await
用来等待未来完成.但是,如果我已经注册了一个回调,以便在完成该未来的情况下运行,那么我怎样才能等待未来完成以及回调完成?
这是一个最小但完整的程序来说明问题:
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{ Await, Future }
object Main {
def main(args: Array[String]): Unit = {
val f: Future[Int] = Future(0)
f.onSuccess { case _ =>
Thread.sleep(10000)
println("The program waited patiently for this callback to finish.")
}
// This waits for `f` to complete but doesn't wait for the callback
// to finish running.
Await.ready(f, Duration.Inf)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
The program waited patiently for this callback to finish.
Run Code Online (Sandbox Code Playgroud)
相反,没有输出; 程序在回调结束前退出.
请注意,这与等待未来完成的问题不同,这已在此问题中得到解答.
在线搜索答案给出了两个突出的帖子(Codacy和Daniel Westheide),两者都给出了与Scala官方文档Try相同的答案:
上面示例中显示的Try的一个重要属性是它能够管理或链接操作,沿途捕获异常.
上面引用的示例是:
import scala.io.StdIn
import scala.util.{Try, Success, Failure}
def divide: Try[Int] = {
val dividend = Try(StdIn.readLine("Enter an Int that you'd like to divide:\n").toInt)
val divisor = Try(StdIn.readLine("Enter an Int that you'd like to divide by:\n").toInt)
val problem = dividend.flatMap(x => divisor.map(y => x/y))
problem match {
case Success(v) =>
println("Result of " + dividend.get + "/"+ divisor.get +" is: " + v)
Success(v)
case Failure(e) =>
println("You must've divided by zero …
Run Code Online (Sandbox Code Playgroud) 我有一个表面上很简单的宏观问题,我已经用头撞了几个小时,但没有运气。也许有更多经验的人可以提供帮助。
我有以下宏:
import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
object MacroObject {
def run(s: String): Unit =
macro runImpl
def runImpl(c: Context)(s: c.Tree): c.Tree = {
import c.universe._
println(s) // <-- I need the macro to know the value of s at compile time
q"()"
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:我希望宏知道s
传递给它的值——不是 AST s
,而是s
它本身的值。具体来说,我希望它具有这种行为:
def runTheMacro(str: String): Unit = MacroObject.run(str)
final val HardCodedString1 = "Hello, world!"
runTheMacro(HardCodedString1) // the macro should print "Hello, world!"
// to the console during macro expansion …
Run Code Online (Sandbox Code Playgroud) 我有以下一段代码我想制作DRYer:
def createAdmin(/* ... */): Future[Int] =
db.run {
{
(users returning users.map(_.id)) += Account(0, /* ... */)
} flatMap { id =>
admins += Admin(userId = id, /* ... */)
}
}
def createStandardUser(/* ... */): Future[Int] =
db.run {
{
(users returning users.map(_.id)) += Account(0, /* ... */)
} flatMap { id =>
standardUsers += StandardUser(userId = id, /* ... */)
}
}
Run Code Online (Sandbox Code Playgroud)
编译好.但如果我将两者合并到以下内容中:
def createUser(role: String)(/* ... */): Future[Int] =
db.run {
{
(users returning users.map(_.id)) += …
Run Code Online (Sandbox Code Playgroud)