Skip to content

Commit

Permalink
Rename .getRight to .orFail
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 10, 2024
1 parent 9ab3a29 commit 7d4ff31
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions core/src/main/scala/sttp/client4/ResponseAs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ case class ResponseAs[+T](delegate: GenericResponseAs[T, Any]) extends ResponseA
* yet an exception)
* - in case of `B`, returns the value directly
*/
def getRight[A, B](implicit tIsEither: T <:< Either[A, B]): ResponseAs[B] =
def orFail[A, B](implicit tIsEither: T <:< Either[A, B]): ResponseAs[B] =
mapWithMetadata { case (t, meta) =>
(t: Either[A, B]) match {
case Left(a: Exception) => throw a
Expand All @@ -89,10 +89,10 @@ case class ResponseAs[+T](delegate: GenericResponseAs[T, Any]) extends ResponseA
}

/** If the type to which the response body should be deserialized is an `Either[ResponseException[HE, DE], B]`, either
* throws the [[DeserializationException]], returns the deserialized body from the [[HttpError]], or the deserialized
* successful body `B`.
* throws /returns a failed effect with the [[DeserializationException]], returns the deserialized body from the
* [[HttpError]], or the deserialized successful body `B`.
*/
def getEither[HE, DE, B](implicit
def orFailDeserialization[HE, DE, B](implicit
tIsEither: T <:< Either[ResponseException[HE, DE], B]
): ResponseAs[Either[HE, B]] = map { t =>
(t: Either[ResponseException[HE, DE], B]) match {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/sttp/client4/request.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ case class Request[T](
object Request {
implicit class RichRequestTEither[A, B](r: Request[Either[A, B]]) {
def mapResponseRight[B2](f: B => B2): Request[Either[A, B2]] = r.copy(response = r.response.mapRight(f))
def responseGetRight: Request[B] = r.copy(response = r.response.getRight)
def responseGetRight: Request[B] = r.copy(response = r.response.orFail)
}

implicit class RichRequestTEitherResponseException[HE, DE, B](
r: Request[Either[ResponseException[HE, DE], B]]
) {
def responseGetEither: Request[Either[HE, B]] = r.copy(response = r.response.getEither)
def responseGetEither: Request[Either[HE, B]] = r.copy(response = r.response.orFailDeserialization)
}
}

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

The response specifications can be further refined using `.getRight` and `.getEither`, see [response body specifications](responses/body.md).
The response specifications can be further refined using `.orFail` and `.orFailDeserialization`, 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 @@ -79,20 +79,20 @@ 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 `.getRight` 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 `.orFail` combinator:

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

basicRequest.response(asString.getRight): PartialRequest[String]
basicRequest.response(asString.orFail): PartialRequest[String]
```

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`.

```eval_rst
.. 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.
While both ``asStringAlways`` and ``asString.orFail`` 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, `.getEither`, which can be used to extract typed errors and fail the effect if there's a deserialization error.
Expand Down
2 changes: 1 addition & 1 deletion docs/responses/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import sttp.client4._
def asJson[T]: ResponseAs[Either[ResponseException[String, Exception], T]] = ???
```

There are also the `.getRight` and `.getEither` methods on eligible response specifications, which convert http errors or deserialization exceptions as failed effects.
There are also the `.orFail` and `.orFailDeserialization` methods on eligible response specifications, which convert http errors or deserialization exceptions as failed effects.

## Possible outcomes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import sttp.client4._
import sttp.client4.httpclient.monix.HttpClientMonixBackend
import sttp.client4.circe._

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

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

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

HttpClientMonixBackend
.resource()
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 @@ -85,7 +85,7 @@ libraryDependencies ++= List(
Example code:

```eval_rst
.. literalinclude:: ../../examples-ce2/src/main/scala/sttp/client4/examples/GetAndParseJsonGetRightMonixCirce.scala
.. literalinclude:: ../../examples-ce2/src/main/scala/sttp/client4/examples/GetAndParseJsonOrFailMonixCirce.scala
:language: scala
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class OpenTelemetryMetricsBackendTest extends AnyFlatSpec with Matchers with Opt
assertThrows[SttpClientException] {
basicRequest
.get(uri"http://127.0.0.1/foo")
.response(asString.getRight)
.response(asString.orFail)
.send(backend)
}

Expand Down Expand Up @@ -231,7 +231,7 @@ class OpenTelemetryMetricsBackendTest extends AnyFlatSpec with Matchers with Opt
// when
basicRequest
.get(uri"http://127.0.0.1/foo")
.response(asString.getRight)
.response(asString.orFail)
.send(backend)

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class PrometheusBackendTest
assertThrows[SttpClientException] {
basicRequest
.get(uri"http://127.0.0.1/foo")
.response(asString.getRight)
.response(asString.orFail)
.send(backend)
}

Expand Down Expand Up @@ -401,7 +401,7 @@ class PrometheusBackendTest
// when
basicRequest
.get(uri"http://127.0.0.1/foo")
.response(asString.getRight)
.response(asString.orFail)
.send(backend)

// then
Expand Down

0 comments on commit 7d4ff31

Please sign in to comment.