私はGETおよびPOST要求を行うためにAkka HTTPクライアントを作成しています。ドキュメントからFuture
ベースの例を使用しています。 「ローカル変数インスタンスを見つけることができません」というエラーが表示されます。簡単なコードとテストケースがあります。コードは何GET要求のためのakka-httpクライアントは、ローカル変数インスタンスを見つけることができません。
package com.epl.akka
import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, HttpResponse, StatusCodes}
import akka.stream.{ActorMaterializer, ActorMaterializerSettings}
import akka.util.ByteString
import com.epl.akka.AkkaHTTPClient.GET
class AkkaHTTPClient() extends Actor
with ActorLogging {
import akka.pattern.pipe
import context.dispatcher
final implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))
val http = Http(context.system)
override def receive: Receive = {
case GET(uri: String) =>
log.info("getting the url")
http
.singleRequest(HttpRequest(HttpMethods.GET,uri = uri))
.pipeTo(self)
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body =>
log.info("Got response, body: " + body.utf8String)
}
case resp @ HttpResponse(code, _, _, _) =>
log.info("Request failed, response code: " + code)
resp.discardEntityBytes()
}
}
object AkkaHTTPClient {
def props() =
Props[AkkaHTTPClient]
final case class GET(uri: String){}
}
はURLをクロールして、JSONの結果を作成するために、JSoupによって解析されるHTML応答を返しています。
テストケース:
class AkkaHTTPClientSpec(_system: ActorSystem)
extends TestKit(_system)
with Matchers
with FlatSpecLike
with BeforeAndAfterAll {
def this() = this(ActorSystem("AkkaHTTPClientSpec"))
override def afterAll: Unit = {
shutdown(system)
}
"A AkkaHttpClient Actor" should "give HTML response when instructed to" in {
val testProbe = TestProbe()
val url = "http://www.premierleague.com"
val akkaHTTPClientActor = system.actorOf(AkkaHTTPClient.props(),"AkkaHttpClient")
akkaHTTPClientActor ! GET(url)
}
}
コードがval http = Http(context.system)
を呼び出した後に失敗しました。どんな助けもありがとうございます。
あなたは完全を投稿することができますが、エラーメッセージ「ローカル変数のインスタンスを見つけることができませんか」? –