Skip to content

Commit

Permalink
Rename failLeft -> getRight/ getEither
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Sep 15, 2020
1 parent 7bd554f commit 24265de
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/sttp/client/ResponseAs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ object ResponseAs {
* if `A` is not yet an exception)
* - in case of `B`, returns the value directly
*/
def failLeft: ResponseAs[B, R] =
def getRight: ResponseAs[B, R] =
ra.mapWithMetadata { case (t, meta) =>
t match {
case Left(a: Exception) => throw a
Expand All @@ -127,7 +127,7 @@ object ResponseAs {
* either throws the [[DeserializationException]], returns the deserialized body from the [[HttpError]], or
* the deserialized successful body `B`.
*/
def failLeftDeserialize: ResponseAs[Either[HE, B], R] =
def getEither: ResponseAs[Either[HE, B], R] =
ra.map {
case Left(HttpError(he, _)) => Left(he)
case Left(d: DeserializationException[_]) => throw d
Expand Down
2 changes: 1 addition & 1 deletion docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ libraryDependencies ++= List(
Example code:

```eval_rst
.. literalinclude:: ../../examples/src/main/scala/sttp/client/examples/GetAndParseJsonFailLeftMonixCirce.scala
.. literalinclude:: ../../examples/src/main/scala/sttp/client/examples/GetAndParseJsonGetRightMonixCirce.scala
:language: scala
```

Expand Down
2 changes: 1 addition & 1 deletion docs/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def asJsonAlways[B]: ResponseAs[Either[DeserializationException[Exception], B],
def asJsonEither[E, B]: ResponseAs[Either[ResponseException[E, Exception], B], Any] = ???
```

The response specifications can be further refined using `.failLeft` and `.failLeftDeserialize`, see [response body specifications](responses/body.md).
The response specifications can be further refined using `.getRight` and `.getEither`, see [response body specifications](responses/body.md).

Following data class will be used through the next few examples:

Expand Down
6 changes: 3 additions & 3 deletions docs/responses/body.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ basicRequest.response(asFile(someFile))

## Failing when the response code is not 2xx

Sometimes it's convenient to get a failed effect (or an exception thrown) when the response status code is not successful. In such cases, the response specification can be modified using the `.failLeft` combinator:
Sometimes it's convenient to get a failed effect (or an exception thrown) when the response status code is not successful. In such cases, the response specification can be modified using the `.getRight` combinator:

```scala mdoc:compile-only
import sttp.client._

basicRequest.response(asString.failLeft): PartialRequest[String, Any]
basicRequest.response(asString.getRight): PartialRequest[String, Any]
```

The combinator works in all cases where the response body is specified to be deserialized as an `Either`. If the left is already an exception, it will be thrown unchanged. Otherwise, the left-value will be wrapped in an `HttpError`.

```note:: While both ``asStringAlways`` and ``asString.failLeft`` have the type ``ResponseAs[String, Any]``, they are different. The first will return the response body as a string always, regardless of the responses' status code. The second will return a failed effect / throw a ``HttpError`` exception for non-2xx status codes, and the string as body only for 2xx status codes.```
```note:: While both ``asStringAlways`` and ``asString.getRight`` have the type ``ResponseAs[String, Any]``, they are different. The first will return the response body as a string always, regardless of the responses' status code. The second will return a failed effect / throw a ``HttpError`` exception for non-2xx status codes, and the string as body only for 2xx status codes.```

There's also a variant of the combinator, `.failLeftDeserialize`, which can be used to extract typed errors and fail the effect if there's a deserialization error.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import sttp.client._
import sttp.client.asynchttpclient.monix.AsyncHttpClientMonixBackend
import sttp.client.circe._

object GetAndParseJsonFailLeftMonixCirce extends App {
object GetAndParseJsonGetRightMonixCirce extends App {
import monix.execution.Scheduler.Implicits.global

case class HttpBinResponse(origin: String, headers: Map[String, String])

val request: Request[HttpBinResponse, Any] = basicRequest
.get(uri"https://httpbin.org/get")
.response(asJson[HttpBinResponse].failLeft)
.response(asJson[HttpBinResponse].getRight)

AsyncHttpClientMonixBackend
.resource()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object LogRequestsSlf4j extends App {

val request = basicRequest
.get(uri"https://httpbin.org/get")
.response(asJson[HttpBinResponse].failLeft)
.response(asJson[HttpBinResponse].getRight)

val backend: SttpBackend[Identity, Any] =
Slf4jLoggingBackend(
Expand Down
2 changes: 1 addition & 1 deletion generated-docs/out/backends/wrappers/opentracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Add following dependency:
```
libraryDependencies += "io.opentracing.brave" % "brave-opentracing" % "0.37.2"
// and for integrationw with okHttp:
libraryDependencies += "io.zipkin.reporter2" % "zipkin-sender-okhttp3" % "2.15.1"
libraryDependencies += "io.zipkin.reporter2" % "zipkin-sender-okhttp3" % "2.15.2"
```

Create an instance of tracer:
Expand Down
2 changes: 1 addition & 1 deletion generated-docs/out/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ libraryDependencies ++= List(
Example code:

```eval_rst
.. literalinclude:: ../../examples/src/main/scala/sttp/client/examples/GetAndParseJsonFailLeftMonixCirce.scala
.. literalinclude:: ../../examples/src/main/scala/sttp/client/examples/GetAndParseJsonGetRightMonixCirce.scala
:language: scala
```

Expand Down
2 changes: 1 addition & 1 deletion generated-docs/out/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def asJsonAlways[B]: ResponseAs[Either[DeserializationException[Exception], B],
def asJsonEither[E, B]: ResponseAs[Either[ResponseException[E, Exception], B], Any] = ???
```

The response specifications can be further refined using `.failLeft` and `.failLeftDeserialize`, see [response body specifications](responses/body.md).
The response specifications can be further refined using `.getRight` and `.getEither`, see [response body specifications](responses/body.md).

Following data class will be used through the next few examples:

Expand Down
4 changes: 2 additions & 2 deletions generated-docs/out/resilience.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Resilience covers areas such as retries, circuit breaking and rate limiting.

sttp client doesn't have the above built-in, as these concepts are usually best handled on a higher level. Sending a request (that is, invoking `myRequest.send(backend)` using an implicit backend that is in scope), can be viewed as a:
sttp client doesn't have the above built-in, as these concepts are usually best handled on a higher level. Sending a request (that is, invoking `myRequest.send(backend)`), can be viewed as a:

* `() => Response[T]` function for synchronous backends
* `() => Future[Response[T]]` for `Future`-based asynchronous backends
Expand All @@ -24,7 +24,7 @@ Here's an incomplete list of libraries which can be used to manage retries in va
sttp client contains a default implementation of a predicate, which allows deciding if a request is retriable: if the body can be sent multiple times, and if the HTTP method is idempotent.
This predicate is available as `RetryWhen.Default` and has type `(Request[_, _], Either[Throwable, Response[_]]) => Boolean`.

See also the [retrying using ZIO](examples.md#retry-a-request-using-zio) example, as well as an example of a very simple [retrying backend wrapper](backends/wrappers/custom.md#example-retrying-backend-wrapper).
See also the [retrying using ZIO](examples.html#retry-a-request-using-zio) example, as well as an example of a very simple [retrying backend wrapper](backends/wrappers/custom.html#example-retrying-backend-wrapper).

Note that some backends also have built-in retry mechanisms, e.g. [akka-http](https://doc.akka.io/docs/akka-http/current/scala/http/client-side/host-level.html#retrying-a-request) or [OkHttp](http://square.github.io/okhttp) (see the builder's `retryOnConnectionFailure` method).

Expand Down
6 changes: 3 additions & 3 deletions generated-docs/out/responses/body.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ basicRequest.response(asFile(someFile))

## Failing when the response code is not 2xx

Sometimes it's convenient to get a failed effect (or an exception thrown) when the response status code is not successful. In such cases, the response specification can be modified using the `.failLeft` combinator:
Sometimes it's convenient to get a failed effect (or an exception thrown) when the response status code is not successful. In such cases, the response specification can be modified using the `.getRight` combinator:

```scala
import sttp.client._

basicRequest.response(asString.failLeft): PartialRequest[String, Any]
basicRequest.response(asString.getRight): PartialRequest[String, Any]
```

The combinator works in all cases where the response body is specified to be deserialized as an `Either`. If the left is already an exception, it will be thrown unchanged. Otherwise, the left-value will be wrapped in an `HttpError`.

```note:: While both ``asStringAlways`` and ``asString.failLeft`` have the type ``ResponseAs[String, Any]``, they are different. The first will return the response body as a string always, regardless of the responses' status code. The second will return a failed effect / throw a ``HttpError`` exception for non-2xx status codes, and the string as body only for 2xx status codes.```
```note:: While both ``asStringAlways`` and ``asString.getRight`` have the type ``ResponseAs[String, Any]``, they are different. The first will return the response body as a string always, regardless of the responses' status code. The second will return a failed effect / throw a ``HttpError`` exception for non-2xx status codes, and the string as body only for 2xx status codes.```

There's also a variant of the combinator, `.failLeftDeserialize`, which can be used to extract typed errors and fail the effect if there's a deserialization error.

Expand Down
13 changes: 13 additions & 0 deletions generated-docs/out/websockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ def asWebSocketStreamAlways[S](s: Streams[S])(p: s.Pipe[WebSocketFrame.Data[_],
```

Using streaming websockets requires the backend to support the given streaming capability (see also [streaming](requests/streaming.md)). Streaming capabilities are described as implementations of `Streams[S]`, and are provided by backend implementations, e.g. `AkkaStreams` or `Fs2Streams[F]`.

When working with streams of websocket frames keep in mind that a text payload maybe fragmented into multiple frames.
sttp provides two useful methods (`fromTextPipe`, `fromTextPipeF`) for each backend to aggregate these fragments back into complete messages.
These methods can be found in corresponding WebSockets classes for given effect type:
```eval_rst
================ ==========================================
effect type class name
================ ==========================================
``monix.Task`` ``sttp.client.impl.monix.MonixWebSockets``
``ZIO`` ``sttp.client.impl.zio.ZioWebSockets``
``fs2.Stream`` ``sttp.client.impl.fs2.Fs2WebSockets``
================ ==========================================
```

0 comments on commit 24265de

Please sign in to comment.