|
| 1 | +# Using as a Play client |
| 2 | + |
| 3 | +Add the dependency: |
| 4 | + |
| 5 | +```scala |
| 6 | +"com.softwaremill.sttp.tapir" %% "tapir-play-client" % "0.17.0-M6" |
| 7 | +``` |
| 8 | + |
| 9 | +To make requests using an endpoint definition using the [play client](https://github.com/playframework/play-ws), import: |
| 10 | + |
| 11 | +```scala |
| 12 | +import sttp.tapir.client.play._ |
| 13 | +``` |
| 14 | + |
| 15 | +This adds the two extension methods to any `Endpoint`: |
| 16 | + - `toPlayRequestUnsafe(String)`: given the base URI returns a function, |
| 17 | + which will generate a request and a response parser which might throw |
| 18 | + an exception when decoding of the result fails |
| 19 | + ```scala |
| 20 | + I => (StandaloneWSRequest, StandaloneWSResponse => Either[E, O]) |
| 21 | + ``` |
| 22 | + - `toPlayRequest(String)`: given the base URI returns a function, |
| 23 | + which will generate a request and a response parser which represents |
| 24 | + decoding errors as the `DecodeResult` class |
| 25 | + ```scala |
| 26 | + I => (StandaloneWSRequest, StandaloneWSResponse => DecodeResult[Either[E, O]]) |
| 27 | + ``` |
| 28 | + |
| 29 | +Note that these are a one-argument functions, where the single argument is the input of end endpoint. This might be a |
| 30 | +single type, a tuple, or a case class, depending on the endpoint description. |
| 31 | + |
| 32 | +After providing the input parameters, the two following are returned: |
| 33 | +- a description of the request to be made, with the input value |
| 34 | + encoded as appropriate request parameters: path, query, headers and body. |
| 35 | + This can be further customised and sent using regular Play methods. |
| 36 | +- a response parser to be applied to the response got after executing the request. |
| 37 | + The result will then contain the decoded error or success values |
| 38 | + (note that this can be the body enriched with data from headers/status code). |
| 39 | + |
| 40 | +Example: |
| 41 | + |
| 42 | +```scala |
| 43 | +import sttp.tapir._ |
| 44 | +import sttp.tapir.client.play._ |
| 45 | +import sttp.capabilities.akka.AkkaStreams |
| 46 | + |
| 47 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 48 | +import scala.concurrent.Future |
| 49 | + |
| 50 | +import play.api.libs.ws.StandaloneWSClient |
| 51 | + |
| 52 | +def example[I, E, O, R >: AkkaStreams](implicit wsClient: StandaloneWSClient) { |
| 53 | + val e: Endpoint[I, E, O, R] = ??? |
| 54 | + val inputArgs: I = ??? |
| 55 | + |
| 56 | + val (req, responseParser) = e |
| 57 | + .toPlayRequestUnsafe(s"http://localhost:9000") |
| 58 | + .apply(inputArgs) |
| 59 | + |
| 60 | + val result: Future[Either[E, O]] = req |
| 61 | + .execute() |
| 62 | + .map(responseParser) |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Limitations |
| 67 | + |
| 68 | +Multipart requests are not supported. |
| 69 | + |
| 70 | +Streaming capabilities: |
| 71 | +- only `AkkaStreams` is supported |
0 commit comments