Last active
June 1, 2017 16:16
-
-
Save ScalaWilliam/39d9fc69204a9428b95c to your computer and use it in GitHub Desktop.
Running Play WS standalone, without a Play App. Works with Maven builds nicely. The top way to make REST calls in Scala, in my opinion.
This file contains hidden or 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
Here are the first ten Planet Scala titles: | |
Planet Scala | |
Big Data Engineer / Data Scientist at Recruit IT (Full-time) | |
Functional Jobs: Search 'scala' | |
Mobile Enterprise Integration with Scala, MongoDB and Swagger | |
Janx Spirit | |
Scala: Next Steps | |
Scala 2.11.2 is now available! | |
Scala for Java Developers | |
Relentless Scaling | |
Play 2.3 Applications on OpenShift | |
Process finished with exit code 0 |
This file contains hidden or 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
<repositories> | |
<repository> | |
<id>typesafe</id> | |
<name>typesafe-releases</name> | |
<url>http://repo.typesafe.com/typesafe/releases</url> | |
</repository> | |
</repositories> | |
<dependencies> | |
<dependency> | |
<groupId>com.typesafe.play</groupId> | |
<artifactId>play-ws_2.11</artifactId> | |
<version>2.3.3</version> | |
</dependency> | |
</dependencies> |
This file contains hidden or 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
package com.scalawilliam.example.play | |
import play.api.libs.ws._ | |
/** | |
* Play's Scala WS library is very very cool. Provides you with plenty niceties. | |
* See more: https://www.playframework.com/documentation/2.3.x/ScalaWS | |
* | |
* Unfortunately it by default requires a Play application context. | |
* But you do not necessarily always want that. | |
* | |
* William Narmontas, Scala Consultant, London. UK | |
* https://www.scalawilliam.com/ | |
*/ | |
object WSWithoutPlayApp extends App { | |
class StandaloneWSAPI(wsClientConfig: WSClientConfig = DefaultWSClientConfig()) extends WSAPI with java.io.Closeable { | |
import play.api.libs.ws.ning.{NingWSClient, NingAsyncHttpClientConfigBuilder} | |
lazy val configuration = new NingAsyncHttpClientConfigBuilder(wsClientConfig).build() | |
lazy val ningWsClient = new NingWSClient(configuration) | |
override val client: WSClient = ningWsClient | |
override def url(url: String): WSRequestHolder = client.url(url) | |
def close(): Unit = { | |
ningWsClient.close() | |
} | |
} | |
val standaloneWSAPI = new StandaloneWSAPI() | |
// Standard Play-style WSAPI | |
val wsAPI: WSAPI = standaloneWSAPI | |
def holder = { | |
wsAPI.url("http://www.planetscala.com/atom.xml") | |
.withHeaders("Accept" -> "application/xml") | |
.withRequestTimeout(10000) | |
} | |
import scala.concurrent._ | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Await | |
def getPlanetScalaTitles: Future[Seq[String]] = { | |
holder.get().map(_.xml \\ "title" map (_.text)) | |
} | |
println("Here are the first ten Planet Scala titles:") | |
try { | |
Await.result(getPlanetScalaTitles, 10.seconds) take 10 foreach println | |
} finally { | |
// required, else there'll be threads hanging around | |
// you might not care to do this though. | |
standaloneWSAPI.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment