score:13
EDIT for Play 2.5:
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws._
import play.api.libs.ws.ahc.AhcWSClient
import scala.concurrent.Future
object Main {
import scala.concurrent.ExecutionContext.Implicits._
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val wsClient = AhcWSClient()
call(wsClient)
.andThen { case _ => wsClient.close() }
.andThen { case _ => system.terminate() }
}
def call(wsClient: WSClient): Future[Unit] = {
wsClient.url("http://www.google.com").get().map { response =>
val statusText: String = response.statusText
println(s"Got a response $statusText")
}
}
}
Please see:
- https://www.playframework.com/documentation/2.5.x/ScalaWS#using-wsclient
- https://www.playframework.com/documentation/2.5.x/ScalaTestingWebServiceClients
for more detailed examples of standalone WSClient usage. If you are migrating from earlier versions, see https://www.playframework.com/documentation/2.5.x/Migration25#Play-WS-upgrades-to-AsyncHttpClient-2
For Play 2.4:
Do not use raw AsyncHttpClientConfig.Builder for HTTPS -- it does not configure a secure SSLContext with hostname validation.
You can create a new WSClient instance using the following code:
import play.api.libs.ws.ning._
import play.api.libs.ws._
val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build()
val builder = new AsyncHttpClientConfig.Builder(config)
val wsClient:WSClient = new NingWSClient(builder.build())
Please note that this will start up threads which will not be closed until you close the client:
wsClient.underlying[NingWSClient].close()
and you may run into memory leaks if you don't close it.
score:4
A started PlayApplication contains a client instance, which WS.client simply points to it. Since you won't start a Play application, You have to create your own client, like this:
val client = {
val builder = new com.ning.http.client.AsyncHttpClientConfig.Builder()
new play.api.libs.ws.ning.NingWSClient(builder.build())
}
client.url("http://example.com/").get()
Have a look on my project for a similar usecase, I am using play-ws and play-json, without Play itself.
score:8
Play 2.4 makes it very easy to utilize WS in a standalone app.
The following gist provides a nice working example and the following blog post provides a nice explanation.
Here are the highlights.
Configure build.sbt
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-ws" % "2.4.0-M2"
)
Initialize the WS client
val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build
val builder = new AsyncHttpClientConfig.Builder(config)
val client = new NingWSClient(builder.build)
Use WS
client.url("http://www.example.com").get
Release WS resources
client.close()
Source: stackoverflow.com
Related Query
- How to have a Scala standalone application that uses the playframework libraries
- How do I scale my Scala REST application that uses Akka?
- In the scala spray framework, how can I create multiple http clients that have different configurations (e.g., timeouts, retries)
- How to set up a scala project in IntelliJ IDEA that uses git libraries
- How can I view the code that Scala uses to automatically generate the apply function for case classes?
- Scala - how to make the SortedSet with custom ordering hold multiple different objects that have the same value by which we sort?
- How to make the 'public' schema default in a Scala Play project that uses PostgreSQL?
- How do I write a query for mongodb using the casbah driver for scala that uses a substring and checks if the field is in a list of supplied values?
- How to build a scala application that was created in eclipse scala plugin FROM THE CL
- In a scala macro, how to get the full name that a class will have at runtime?
- How do I change the version of a dependency in a Scala application that I haven't set?
- How to see the list of JARs that are loaded with my Scala Application
- Created scala docker image using sbt native plugin. I have to add LDAP CA cert. How to add that in the image
- How to parse a csv that uses ^A (i.e. \001) as the delimiter with spark-csv?
- How do I write a JSON Format for an object in the Java library that doesn't have an apply method?
- What is the syntax for creating a Map in Scala that uses an enum as a key?
- scala spec2 I am Unable to create a test that uses must be_== and failure at the same time
- How do I create a generic Scala function that returns an object based on the generic type?
- How does the Scala type checker know to prevent calling flatten on a List that is already flat
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- How to convert/wrap a sequence in scala to an Option[Seq] so that if the list is empty, the Option is None
- Scala - How to split the probability column (column of vectors) that we obtain when we fit the GMM model to the data in to two separate columns?
- How to port Scala 2.7.7 code that uses scala.collection.jcl to Scala 2.8?
- How do i change the Scala version that sbt works with?
- How does the Scala type system know that cons + Nil is exhaustive?
- How to make compiler check that 2 method arguments have the same type?
- How can I ensure that the dynamic type of my custom Scala collection is preserved during a map()?
- How do I iterate over the routes in a playframework 2 scala application?
- According to the Scala Language Specification, packages are AnyRef values and have types. How does this make sense?
- How do I run a comand line scala script that uses Play Framework app database?
More Query from same tag
- Is there there any difference or reason to prefer one of these function signatures?
- Is it possible/advisable to index a String of JSON data using elastic4s?
- Error injecting constructor, java.lang.ClassCastException: Play 2.5.4 - Scala
- How do I make a trait that I can mix into mapped fields to that I don not have to override the same properties for many fields in a database?
- Linking the Machine Learning Prediction back to the original data set
- Is there a SBT equivalent for Maven POM properties?
- Iterate through rows in DataFrame and transform one to many
- Apache Flink - can't compile KeyedOneInputStreamOperatorTestHarness with Int or Long generic parameters
- Preventing the creation of anonymous classes from traits, without an enclosing object
- IBM Tone Analyzer (plan Beta )is not working with this example
- Trying to run sbt on Ubuntu. (Error: java.lang.NoClassDefFoundError: scala/ScalaObject)
- How to remove numbers from text in Scala?
- How would I override a function in Scala with another function?
- What is Scalas Product.productIterator supposed to do?
- Transforming an `Option[Tuple]` in a scalaz `\/`
- Understanding `apply` and `unapply` with Singleton Type
- Haskell's mapM equivalent for Scala cats
- Elegant way to reverse a list using foldRight?
- Solving a sudoku in Scala
- Scala: Removing key/value pair from list
- Implicitly Convert Generic and Non-Generic Subtypes in Scala
- How to extract only certain attribute levels from a nested structure in a spark dataframe
- How to select using not operation in Scala XML?
- How to make a parameterized trait which can instantiate classes?
- Signaling failure via the function application parser combinator
- Defining the future implicit context in Play for Scala
- Webbit websocket ws:// connection works but wss:// handshake fails silently without any error?
- `def` vs `val` vs `lazy val` evaluation in Scala
- Scala: how to traverse stream/iterator collecting results into several different collections
- Is there a way to make sure that a class overrides hashCode