从webapp的客户端,我点击了服务器端路由,它只是第三方API的包装器。使用dispatch,我试图让服务器端请求返回完全标头和第三方API对客户端AJAX调用的响应。
当我这样做时:
val req = host("third-pary.api.com, 80)
val post = req.as("user", "pass") / "route" << Map("key" -> "akey", "val" -> "aval")
Http(post > as.String)
我总是看到一个200
响应返回到AJAX调用(有点预期)。我看过使用了Either
语法,但我真的更像是Any
,因为它只是确切的响应和标题。怎么写呢?
我应该提到我在服务器端使用Scalatra,因此本地路由是:
post("/route") {
}
修改
以下是我正在使用的建议的任一匹配示例,但match
语法没有意义 - 我不在乎是否有错误,我只想返回它。此外,我似乎无法通过此方法返回BODY。
val asHeaders = as.Response { response =>
println("BODY: " + response.getResponseBody())
scala.collection.JavaConverters.mapAsScalaMapConverter(
response.getHeaders).asScala.toMap.mapValues(_.asScala.toList)
}
val response: Either[Throwable, Map[String, List[String]]] =
Http(post > asHeaders).either()
response match {
case Left(wrong) =>
println("Left: " + wrong.getMessage())
// return Action with header + body
case Right(good) =>
println("Right: " + good)
// return Action with header + body
}
理想情况下,解决方案会返回Scalatra ActionResult(responseStatus(status, reason), body, headers)
。
答案 0 :(得分:5)
使用Dispatch时,实际上很容易获得响应头。例如,使用0.9.4:
import dispatch._
import scala.collection.JavaConverters._
val headers: java.util.Map[String, java.util.List[String]] = Http(
url("http://www.google.com")
)().getHeaders
现在,例如:
scala> headers.asScala.mapValues(_.asScala).foreach {
| case (k, v) => println(k + ": " + v)
| }
X-Frame-Options: Buffer(SAMEORIGIN)
Transfer-Encoding: Buffer(chunked)
Date: Buffer(Fri, 30 Nov 2012 20:42:45 GMT)
...
如果您经常这样做,最好将其封装起来,例如:
val asHeaders = as.Response { response =>
scala.collection.JavaConverters.mapAsScalaMapConverter(
response.getHeaders
).asScala.toMap.mapValues(_.asScala.toList)
}
现在您可以写下以下内容:
val response: Either[Throwable, Map[String, List[String]]] =
Http(url("http://www.google.com") OK asHeaders).either()
你有错误检查,不可变的集合等等。
答案 1 :(得分:0)
我们需要API的失败请求的响应主体,因此我们提出了这个解决方案:
使用ApiHttpError
和code
(正文正文)定义您自己的body
课程:
case class ApiHttpError(code: Int, body: String)
extends Exception("Unexpected response status: %d".format(code))
定义OkWithBodyHandler
,类似于displatch
来源中使用的内容:
class OkWithBodyHandler[T](f: Response => T) extends AsyncCompletionHandler[T] {
def onCompleted(response: Response) = {
if (response.getStatusCode / 100 == 2) {
f(response)
} else {
throw ApiHttpError(response.getStatusCode, response.getResponseBody)
}
}
}
现在,在您调用可能抛出和异常的代码(调用API
)附近时,将implicit
覆盖添加到ToupleBuilder
(再次类似于源代码)并调用{ {1}}上的{1}}:
OkWithBody
从现在开始,抓取request
将为您提供class MyApiService {
implicit class MyRequestHandlerTupleBuilder(req: Req) {
def OKWithBody[T](f: Response => T) =
(req.toRequest, new OkWithBodyHandler(f))
}
def callApi(request: Req) = {
Http(request OKWithBody as.String).either
}
}
(使用either
,[Throwable, String]
为as.String)
Throwable
和ApiHttpError
。
希望它有所帮助。