0

我是 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._使此错误消失。

我还尝试了这篇文章的建议:

import dispatch._
Import dispatch.Default._

但可惜我得到:

object Default is not a member of package dispatch
[error] import dispatch.Default._

还尝试了将 隐式 ExecutionContext 传递给包含的对象/调用方法的建议:

import concurrent._
import concurrent.duration._

但我仍然得到

Cannot find an implicit ExecutionContext, either require one yourself or import ExecutionContext.Implicits.global

回到原点...

Scala 的新手,因此对上面代码的任何建议都值得赞赏。

4

1 回答 1

2

由于推荐的sbt console运行工作正常,看起来您的其他导入之一也有一个Defaultsmodule。这是 Scala 中收集隐式值用作函数参数的标准方法(另一个命名约定是Implicits)。

另一个问题是您从 Google Groups 获得的导入语句中存在拼写错误/过期问题 - 它是Defaults, 复数。

总之 - 最好的解决方案是明确让 Scala 知道你想使用哪个模块:

import dispatch._
import dispatch.Defaults._

在一般情况下,仅当库的文档没有另外说明时:最后一条错误消息对于 Scala 中的并发编程非常常见。引用相关部分:

要么自己需要,要么导入 ExecutionContext.Implicits.global

所以,除非你想推出自己的,只需通过模块ExecutionContext导入一个。scala.concurrent.ExecutionContext.Implicits.globalscala.concurrent.ExecutionContext.Implicits

于 2014-02-13T22:20:28.530 回答