我是Scala和Dispatch的新手,我似乎无法获得基本的Post请求。
我实际上正在构建一个sbt插件,可以将文件上传到第三方服务。
这是我的build.sbt
文件:
sbtPlugin := true
name := "sbt-sweet-plugin"
organization := "com.mattwalters"
version := "0.0.1-SNAPSHOT"
libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.11.0"
这是插件的SweetPlugin.scala
:
package com.mattwalters
import sbt._
import Keys._
import dispatch._
object SweetPlugin extends Plugin {
import SweetKeys._
object SweetKeys {
lazy val sweetApiToken =
SettingKey[String]("Required. Find yours at https://example.com/account/#api")
lazy val someOtherToken =
SettingKey[String]("Required. Find yours at https://example.com/some/other/token/")
lazy val sweetFile =
SettingKey[String]("Required. File data")
lazy val sweetotes =
SettingKey[String]("Required. Release notes")
lazy val sweetUpload =
TaskKey[Unit]("sweetUpload", "A task to upload the specified sweet file.")
}
override lazy val settings = Seq (
sweetNotes := "some default notes",
// define the upload task
sweetUpload <<= (
sweetApiToken,
someOtherToken,
sweetFile,
sweetNotes
) map { (
sweetApiToken,
someOtherToken,
sweetFile,
sweetNotes
) =>
// define http stuff here
val request = :/("www.example.com") / "some" / "random" / "endpoint"
val post = request.POST
post.addParameter("api_token", sweetApiToken)
post.addParameter("some_other_token", someOtherToken)
post.addParameter("file", io.Source.fromFile(sweetFile).mkString)
post.addParameter("notes", sweetNotes)
val responseFuture = Http(post OK as.String)
val response = responseFuture()
println(response) // see if we can get something at all....
}
)
}
发货文件显示:
import dispatch._, Defaults._
但我得到
reference to Defaults is ambiguous;
[error] it is imported twice in the same scope by
删除, Defaults._
会使此错误消失。
我还尝试了this post的建议:
import dispatch._
Import dispatch.Default._
但是我得到了:
object Default is not a member of package dispatch
[error] import dispatch.Default._
也尝试过的建议 Passing implicit ExecutionContext to contained objects/called methods:
import concurrent._
import concurrent.duration._
但我还是得到了
Cannot find an implicit ExecutionContext, either require one yourself or import ExecutionContext.Implicits.global
回到原点...
scala新手,所以对上述代码的任何建议都表示赞赏。
答案 0 :(得分:2)
由于推荐的sbt console
运行正常,看起来您的其他导入之一也有Defaults
模块。这是Scala中用于收集implicit values to be used as function params的标准方法(另一个命名约定是Implicits
)。
另一个问题是,您从Google网上论坛获取的导入声明中存在拼写错误/过时问题 - 它是Defaults
,复数形式。
总之 - 最好的解决方案是明确让Scala知道您要使用哪个模块:
import dispatch._
import dispatch.Defaults._
在一般情况下,仅当库的文档没有说明时:最后一条错误消息在Scala中并发编程是很常见的。引用相关部分:
要么自己需要,要么导入ExecutionContext.Implicits.global
因此,除非您想推送自己的ExecutionContext
,否则只需通过scala.concurrent.ExecutionContext.Implicits.global
模块导入scala.concurrent.ExecutionContext.Implicits
。