Skip to content

Commit

Permalink
use http client from play, which is called WS
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 23, 2015
1 parent f26a1ce commit 44afc0d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.ensime_cache
target
*.class
*.log
27 changes: 27 additions & 0 deletions src/main/scala/net/hiogawa/playground/ws/IndependentClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.hiogawa.playground.ws

// java
import com.ning.http.client.AsyncHttpClientConfig

// scala
import scala.concurrent.Future
import play.api.libs.ws.{WS, WSClient, WSResponse}
import play.api.libs.ws.ning.NingWSClient

// implicits
import scala.concurrent.ExecutionContext.Implicits.global // ExecutionContext


object IndependentClient {

def getNonBlockWithClient(url : String)(implicit c : WSClient) : Future[String] = {
WS.clientUrl(url).get() map (resp => resp.body)
}

// NOTE: implicit val implicitClient = IndependentClient.makeDummyClient
def makeDummyClient : NingWSClient = {
val builder = new AsyncHttpClientConfig.Builder()
val client = new NingWSClient(builder.build())
client
}
}
31 changes: 31 additions & 0 deletions src/main/scala/net/hiogawa/playground/ws/WSStuff.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.hiogawa.playground.ws

import scala.concurrent.Future
import play.api.libs.ws.{WS, WSResponse}

// implicits
import scala.concurrent.ExecutionContext.Implicits.global // ExecutionContext
import play.api.Play.current // Application


object WSStuff {

def getNonBlock(url : String) : Future[String] = {
WS.url(url).get() map (resp => resp.body)
}

def getParallelAndProcess[T](urls : List[String])(f : WSResponse => T) : Future[List[T]] = {
sequence(urls.map((url) => WS.url(url).get() map f))
}

// TODO: use applicative from scalaz
// cf. https://www.haskell.org/hoogle/?hoogle=sequence
def sequence[T](ms : List[Future[T]]) : Future[List[T]] = {
ms.foldLeft[Future[List[T]]](Future(List.empty))((accFutures, future) =>
for {
accResps <- accFutures
resp <- future
} yield (resp :: accResps)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.hiogawa.playground.ws

import org.specs2.mutable._

import scala.concurrent.Await
import scala.concurrent.duration.Duration

class IndependentClientTest extends Specification {

"IndependentClient" >> {


// TODO: how to inject implicit declaration as an before all context or anything
// https://etorreborre.github.io/specs2/guide/SPECS2-3.6.5/org.specs2.guide.ContextObjects.html
"getNonBlock" >> {
implicit val implicitClient = IndependentClient.makeDummyClient
val f = IndependentClient.getNonBlockWithClient("http://stackoverflow.com/questions/24881145/how-do-i-use-play-ws-library-in-normal-sbt-project-instead-of-play")
Await.result(f.map((body) => {body.substring(0, 100).length}), Duration.Inf) must be_==(100)
implicitClient.close()
true
}
}
}
29 changes: 29 additions & 0 deletions src/test/scala/net/hiogawa/playground/ws/WSStuffTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.hiogawa.playground.ws

import org.specs2.mutable._
import play.api.test.{FakeApplication, WithApplication}

import scala.concurrent.Await
import scala.concurrent.duration.Duration

// implicits
import scala.concurrent.ExecutionContext.Implicits.global

class WSStuffTest extends Specification {

"WSStuff" >> {

".getParallelAndProcess" in new WithApplication(new FakeApplication) {
val urls = List(
"http://stackoverflow.com/questions/33718775/changing-bootstrap-btn-css-and-hover-action-issue",
"http://stackoverflow.com/questions/33718719/github-hot-to-avoid-exceeding-search-limit-while-searching-for-accounts-detail",
"http://stackoverflow.com/questions/33718754/my-javascript-script-for-changing-css-dont-seem-to-be-workin"
)
// NOTE: you would see output order is defferent between each test run
Await.result(WSStuff.getParallelAndProcess(urls){(resp) =>
println("-----------")
println(resp.body.substring(0, 200))
}, Duration.Inf).length must be_==(3)
}
}
}

0 comments on commit 44afc0d

Please sign in to comment.