-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use http client from play, which is called WS
- Loading branch information
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.ensime_cache | ||
target | ||
*.class | ||
*.log |
27 changes: 27 additions & 0 deletions
27
src/main/scala/net/hiogawa/playground/ws/IndependentClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/scala/net/hiogawa/playground/ws/IndependentClientTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/test/scala/net/hiogawa/playground/ws/WSStuffTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |