我需要做些什么才能为friends_count提取值.我注意到screen_name已经在Status对象和case类中定义.仍然需要扩展Js或JsObject的不同
object TweetDetails extends Js { val friends_count = 'friends_count ? num }
Run Code Online (Sandbox Code Playgroud)
然后将它与JsObjects列表中的每个json对象进行模式匹配,如下所示.符号令人困惑:
scala> val friends_count = 'friends_count ! num // I wish SO understood Scala's symbols
val twtJsonList = http(Status("username").timeline)
twtJsonList foreach {
js =>
val Status.user.screen_name(screen_name) = js
val Status.text(text) = js
val friends_counts(friends_count) = js //i cannot figure out how to extract this
println(friends_count)
println(screen_name)
println(text)
Run Code Online (Sandbox Code Playgroud)
}
我正在尝试使用scala和Dispatch库进行HTTPS发布.我找不到将我的连接标记为https而不是http的位置.这是我到目前为止的代码
println("Running Test")
val http = new Http
val req = :/("www.example.com" , 443) / "full/path.asp"
var response: NodeSeq = Text("")
http(req << "username=x&password=y" <> {response = _ } )
response
println("Done Running Test")
Run Code Online (Sandbox Code Playgroud)
编辑
因此,在尝试解决这个问题之后,我追溯了http线需要看起来像什么
http(req.secure << "username=x&password=y" <> {response = _ } )
Run Code Online (Sandbox Code Playgroud)
另外在这个特定的例子中,我需要POST作为application/x-www-form-urlencoded,要求行看起来像这样
http(req.secure << ("username=x&password=y","application/x-www-form-urlencoded") <> {response = _ } )
Run Code Online (Sandbox Code Playgroud)
这将取代40行C++ + Boost + Asio代码.
我有一些处理HTTP请求的代码,我想对它进行单元测试.
因此,我正在尝试模拟dispatch.Http甚至更好的dispatch.HttpExecutor(0.8.5)与Scala(2.9.1.final),Mockito(1.9.0-rc1)和ScalaTest(1.6.1),但即使可以'让我的测试代码可编辑.
在MyHttpTest中,我希望收到任何HTTP请求的某些HTTP响应:
import org.scalatest.FunSuite
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito.when
import org.mockito.Matchers.any
import dispatch._
class MyHttpTest extends FunSuite with MockitoSugar {
test("example") {
val httpMock = mock[HttpExecutor]
when(httpMock.apply(any(classOf[Handler[String]]))).thenReturn("Some_HTTP_response")
}
}
Run Code Online (Sandbox Code Playgroud)
但它会产生编译错误:
error: overloaded method value thenReturn with alternatives:
(httpMock.HttpPackage[String],<repeated...>[httpMock.HttpPackage[String]])org.mockito.stubbing.OngoingStubbing[httpMock.HttpPackage[String]] <and>
(httpMock.HttpPackage[String])org.mockito.stubbing.OngoingStubbing[httpMock.HttpPackage[String]]
cannot be applied to (java.lang.String)
when(httpMock.apply(any(classOf[Handler[String]]))).thenReturn("Some_response")
Run Code Online (Sandbox Code Playgroud)
那么如何模拟调度客户端呢?
我需要能够发送证书文件(.pem,我认为),使用scala和dispatch发送get请求.
你是怎样做的?
我正在使用Scala的Dispatch如下:
val body = """{"count":5,"requeue":true,"encoding":"auto","truncate":50000}"""
val req = url("http://localhost:4567/api/queues/%2f/myQueue/get").as_!("guest", "guest") << (body, "application/json")
val http = new Http
val resp = http(req as_str)
Run Code Online (Sandbox Code Playgroud)
在%2f被变成了/,所以它会试图张贴到/api/queues///myQueue/get而不是/api/queues/%2f/myQueue/get.
我该如何妥善逃脱?
哪里来的电话到dispatch.Http.shutdown()是否有n独立的HTTP调用等,例如:
import com.typesafe.scalalogging.slf4j.Logging
import org.json4s._
import org.json4s.native.JsonMethods._
import scala.util.{ Failure, Success }
object Main extends App with Logging {
logger.debug("github.cli")
// GET /users/defunkt: `curl https://api.github.com/users/defunkt`
val host: dispatch.Req = dispatch.host("api.github.com").secure
val request: dispatch.Req = host / "users" / "defunkt"
logger.debug(s"Request URL: ${request.url}")
import dispatch.Defaults.executor
dispatch.Http(request > dispatch.as.Response(_.getHeaders())) onComplete {
case Success(h) => logger.debug(h.toString())
case Failure(e) => logger.debug(s"Error: $e")
}
dispatch.Http(request OK dispatch.as.json4s.Json) onComplete {
case Success(j) => logger.debug(j.toString())
case Failure(e) => logger.debug(s"Error: $e")
}
//dispatch.Http.shutdown() // …Run Code Online (Sandbox Code Playgroud) 以下是浏览器中的有效查询(例如Firefox):
http://www.freesound.org/api/sounds/search/?q=barking&api_key=074c0b328aea46adb3ee76f6918f8fae
Run Code Online (Sandbox Code Playgroud)
产生一个JSON文档:
{
"num_results": 610,
"sounds": [
{
"analysis_stats": "http://www.freesound.org/api/sounds/115536/analysis/",
"analysis_frames": "http://www.freesound.org/data/analysis/115/115536_1956076_frames.json",
"preview-hq-mp3": "http://www.freesound.org/data/previews/115/115536_1956076-hq.mp3",
"original_filename": "Two Barks.wav",
"tags": [
"animal",
"bark",
"barking",
"dog",
"effects",
...
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用Dispatch 0.9.4执行此查询.这是一个build.sbt:
scalaVersion := "2.10.0"
libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.9.4"
Run Code Online (Sandbox Code Playgroud)
从sbt console,我做以下事情:
import dispatch._
val q = url("http://www.freesound.org/api/sounds/search")
.addQueryParameter("q", "barking")
.addQueryParameter("api_key", "074c0b328aea46adb3ee76f6918f8fae")
val res = Http(q OK as.String)
Run Code Online (Sandbox Code Playgroud)
但承诺总是以下列错误完成:
res0: dispatch.Promise[String] = Promise(!Unexpected response status: 301!)
Run Code Online (Sandbox Code Playgroud)
那么我做错了什么?这是API文档,如果有帮助的话.
有一些关于在发送http://dispatch.databinder.net/Combined+Pages.html中发送帖子请求的文档, 但目前尚不清楚.那里有myRequest和myPost是什么?
我想发送一个https发布请求+通过标题手动添加一些cookie +添加一些海关标题,如表单数据等,然后通过阅读标题和cookie读取响应.
我只知道如何准备发送帖子请求的网址:
val url = host(myUrl + "?check=1&val1=123").secure
Run Code Online (Sandbox Code Playgroud)
接下来我该怎么办?
当我使用Dispatch库和Scala时,出于调试目的,如何在写完这样的语句之后用文本中的标题等打印出整个HTTP请求?
val svc = url("http://api.hostip.info/country.php")
Run Code Online (Sandbox Code Playgroud) 使用以下代码,我可以从模块生成LLVM 位代码文件:
llvm::Module * module;
// fill module with code
module = ...;
std::error_code ec;
llvm::raw_fd_ostream out("anonymous.bc", ec, llvm::sys::fs::F_None);
llvm::WriteBitcodeToFile(module, out);
Run Code Online (Sandbox Code Playgroud)
然后我可以使用该位码文件生成可执行机器代码文件,例如:
clang -o anonymous anonymous.bc
Run Code Online (Sandbox Code Playgroud)
或者:
llc anonymous.bc
gcc -o anonymous anonymous.s
Run Code Online (Sandbox Code Playgroud)
我现在的问题是:我可以使用 LLVM API 直接在 C++ 中生成机器代码,而不需要首先编写位码文件吗?
我正在寻找代码示例或至少 LLVM API 中的一些起点,例如要使用哪些类,将我推向正确的方向可能就足够了。