I have this 3 class: the first is Pages:
import akka.actor.ActorSystem
object Pages extends App {
implicit val system: ActorSystem = ActorSystem("simple-spray-client")
def function(n: Int, system: ActorSystem): Unit = {
if (n <= 245) {
new Id(n, system)
val t = n + 1
function(t, system)
}
else {
println("finish")
}
}
function(0, system)
}
the second is: Idimport spray.http.HttpResponse
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future}
import scala.util.{Success, Failure}
import akka.actor.ActorSystem
import akka.event.{LoggingAdapter, Logging}
import spray.client.pipelining._
import argonaut._, Argonaut._
class Id (i :Int, systemId: ActorSystem){
case class NameId(name: String, id: Int)
implicit def NameCodecJson: CodecJson[NameId] = casecodec2(NameId.apply, NameId.unapply)("name", "id")
implicit val system: ActorSystem = systemId
val log: LoggingAdapter = Logging(system, getClass)
val pipeline: SendReceive = sendReceive
val responseFuture: Future[HttpResponse] = pipeline {
Get("https://api.guildwars2.com/v2/items?page="+ i+"&page_size=200")
}
responseFuture.onComplete {
case Success(result: HttpResponse) => {
val res: String = result.entity.asString
if (res.contains("Bolt of Damask")) {
val list = res.decodeOption[List[NameId]].get.filter(x => x.name =="Bolt of Damask").head.id
new BuysSells(list, system )
}
}
case Failure(error) =>
log.error(error, "!")
}
}
the third is: BUysSells:import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Success, Failure}
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
import akka.io.IO
import spray.can.Http
import spray.client.pipelining._
import spray.util._
import argonaut._, Argonaut._
class BuysSells(id: Int, systemid: ActorSystem ){
case class Buys (quantity: Int, unit_price: Int)
case class Sells(quantity: Int, unit_price: Int)
case class CommercePrice(id: Int, whiteList: Boolean, buys: Buys, sells: Sells)
implicit def BuysCodecJson: CodecJson[Buys] =casecodec2(Buys.apply, Buys.unapply)("quantity", "unit_price")
implicit def SellsCodecJson: CodecJson[Sells]= casecodec2(Sells.apply, Sells.unapply)("quantity", "unit_price")
implicit def CommercePriceCodecJson: CodecJson[CommercePrice]= casecodec4(CommercePrice.apply, CommercePrice.unapply)("id", "whitelisted", "buys", "sells")
implicit val system: ActorSystem =systemid
val log = Logging(system, getClass)
val pipeline= sendReceive
val responseFuture= pipeline{
Get("https://api.guildwars2.com/v2/commerce/prices/"+id)
}
responseFuture.onComplete{
case Success(result)=> {
val content = result.entity.asString
val res: CommercePrice = content.decodeOption[CommercePrice].get
log.info(" Sell and buy : {} {}", res.buys, res.sells)
shutdown()
}
case Failure(error) =>
log.error(error, "Couldn't get elevation")
shutdown()
}
def shutdown(): Unit = {
IO(Http).ask(Http.CloseAll)(1.second).await
system.shutdown()
}
}
thanks.