Skip to content

Instantly share code, notes, and snippets.

@ScalaWilliam
Last active June 1, 2017 16:16
Show Gist options
  • Save ScalaWilliam/39d9fc69204a9428b95c to your computer and use it in GitHub Desktop.
Save ScalaWilliam/39d9fc69204a9428b95c to your computer and use it in GitHub Desktop.

Revisions

  1. ScalaWilliam revised this gist Aug 20, 2014. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion pom-dependency.xml
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,14 @@
    <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>
    </dependency>
    </dependencies>
  2. ScalaWilliam revised this gist Aug 20, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion pom-dependency.xml
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    <dependency>
    <groupId>com.typesafe.play</groupId>
    <artifactId>play-ws_2.11</artifactId>
  3. ScalaWilliam revised this gist Aug 20, 2014. 2 changed files with 6 additions and 1 deletion.
    1 change: 0 additions & 1 deletion output.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    Here are the first ten Planet Scala titles:
    Planet Scala
    Big Data Engineer / Data Scientist at Recruit IT (Full-time)
    6 changes: 6 additions & 0 deletions pom-dependency.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@

    <dependency>
    <groupId>com.typesafe.play</groupId>
    <artifactId>play-ws_2.11</artifactId>
    <version>2.3.3</version>
    </dependency>
  4. ScalaWilliam created this gist Aug 20, 2014.
    62 changes: 62 additions & 0 deletions WSWithoutPlayApp.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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()
    }

    }
    14 changes: 14 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@

    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