Skip to content

Commit

Permalink
Keep .mapResponseRight in Request only
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 11, 2024
1 parent 4564784 commit ae08044
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
28 changes: 15 additions & 13 deletions core/src/main/scala/sttp/client4/request.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import sttp.attributes.AttributeMap
trait GenericRequest[+T, -R] extends RequestBuilder[GenericRequest[T, R]] with RequestMetadata {
def body: GenericRequestBody[R]
def response: ResponseAsDelegate[T, R]

/** Applies the given function `f` to the deserialized value `T`. */
def mapResponse[T2](f: T => T2): GenericRequest[T2, R]

def toCurl: String = ToCurlConverter(this)
Expand Down Expand Up @@ -124,6 +126,19 @@ case class Request[T](

def mapResponse[T2](f: T => T2): Request[T2] = response(response.map(f))

/** If the type to which the response body should be deserialized is an `Either[A, B]`, applies the given function `f`
* to `Right` values.
*
* Because of type inference, the type of `f` must be fully provided, e.g.
*
* ```
* asString.mapRight((s: String) => parse(s))`
* ```
*/
def mapResponseRight[A, B, B2](f: B => B2)(implicit tIsEither: T <:< Either[A, B]): Request[Either[A, B2]] = response(
response.mapRight(f)
)

/** Specifies that this is a WebSocket request. A [[WebSocketBackend]] will be required to send this request. */
def response[F[_], T2](ra: WebSocketResponseAs[F, T2]): WebSocketRequest[F, T2] =
WebSocketRequest(method, uri, body, headers, ra, options, attributes)
Expand Down Expand Up @@ -168,19 +183,6 @@ case class Request[T](
def send(backend: SyncBackend): Response[T] = backend.send(this)
}

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.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.orFailDeserialization)
}
}

//

/** Describes an HTTP request, along with a description of how the response body should be handled. Either the request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class BackendStubTests extends AnyFlatSpec with Matchers with ScalaFutures {

val result = basicRequest
.get(uri"http://example.org")
.mapResponseRight(_.toInt)
.mapResponseRight(_ * 2)
.mapResponseRight((_: String).toInt)
.mapResponseRight((_: Int) * 2)
.send(backend)

result.body should be(Right(20))
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/scala/sttp/client4/testing/HttpTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ trait HttpTest[F[_]]
"as string with mapping using mapResponse" in {
postEcho
.body(testBody)
.mapResponseRight(_.length)
.mapResponseRight((_: String).length)
.send(backend)
.toFuture()
.map(response => response.body should be(Right(expectedPostEchoResponse.length)))
Expand Down Expand Up @@ -572,7 +572,7 @@ trait HttpTest[F[_]]
}

"redirect when redirects should be followed, and the response is parsed" in {
r2.response(asString).mapResponseRight(_.toInt).send(backend).toFuture().map { resp =>
r2.response(asString).mapResponseRight((_: String).toInt).send(backend).toFuture().map { resp =>
resp.code shouldBe StatusCode.Ok
resp.body shouldBe Right(r4response.toInt)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ trait SyncHttpTest
"as string with mapping using mapResponse" in {
val response = postEcho
.body(testBody)
.mapResponseRight(_.length)
.mapResponseRight((_: String).length)
.send(backend)
response.body should be(Right(expectedPostEchoResponse.length))
}
Expand Down

0 comments on commit ae08044

Please sign in to comment.