Asynchronous API to to query web services, as an http client.
Usage example:
class MyService @Inject() (ws: WSClient) { ws.url("http://example.com/feed").get() ws.url("http://example.com/item").post("content") }
When greater flexibility is needed, you can also create clients explicitly and pass them into WS:
import play.api.libs.ws._ import play.api.libs.ws.ahc._ import akka.stream.Materializer import play.api.ApplicationLifecycle import javax.inject.Inject import scala.concurrent.Future class MyService @Inject() (lifecycle: ApplicationLifecycle)(implicit mat: Materializer) { private val client = new AhcWSClient(new AhcConfigBuilder().build()) client.url("http://example.com/feed").get() lifecycle.addStopHook(() => // Make sure you close the client after use, otherwise you'll leak threads and connections client.close() Future.successful(()) } }
Or call the client directly:
import com.typesafe.config.ConfigFactory import play.api.libs.ws._ import play.api.libs.ws.ahc._ val configuration = play.api.Configuration(ConfigFactory.parseString( """ |play.ws.ssl.trustManager = ... """.stripMargin)) val parser = new DefaultWSConfigParser(configuration, app.classloader) val builder = new AhcConfigBuilder(parser.parse()) val secureClient: WSClient = new AhcWSClient(builder.build()) val response = secureClient.url("https://secure.com").get() secureClient.close() // must explicitly manage lifecycle
The value returned is a Future[WSResponse]
, and you should use Play's asynchronous mechanisms to use this response.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4