Skip to content

Commit ae08044

Browse files
committed
Keep .mapResponseRight in Request only
1 parent 4564784 commit ae08044

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

core/src/main/scala/sttp/client4/request.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import sttp.attributes.AttributeMap
2828
trait GenericRequest[+T, -R] extends RequestBuilder[GenericRequest[T, R]] with RequestMetadata {
2929
def body: GenericRequestBody[R]
3030
def response: ResponseAsDelegate[T, R]
31+
32+
/** Applies the given function `f` to the deserialized value `T`. */
3133
def mapResponse[T2](f: T => T2): GenericRequest[T2, R]
3234

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

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

129+
/** If the type to which the response body should be deserialized is an `Either[A, B]`, applies the given function `f`
130+
* to `Right` values.
131+
*
132+
* Because of type inference, the type of `f` must be fully provided, e.g.
133+
*
134+
* ```
135+
* asString.mapRight((s: String) => parse(s))`
136+
* ```
137+
*/
138+
def mapResponseRight[A, B, B2](f: B => B2)(implicit tIsEither: T <:< Either[A, B]): Request[Either[A, B2]] = response(
139+
response.mapRight(f)
140+
)
141+
127142
/** Specifies that this is a WebSocket request. A [[WebSocketBackend]] will be required to send this request. */
128143
def response[F[_], T2](ra: WebSocketResponseAs[F, T2]): WebSocketRequest[F, T2] =
129144
WebSocketRequest(method, uri, body, headers, ra, options, attributes)
@@ -168,19 +183,6 @@ case class Request[T](
168183
def send(backend: SyncBackend): Response[T] = backend.send(this)
169184
}
170185

171-
object Request {
172-
implicit class RichRequestTEither[A, B](r: Request[Either[A, B]]) {
173-
def mapResponseRight[B2](f: B => B2): Request[Either[A, B2]] = r.copy(response = r.response.mapRight(f))
174-
def responseGetRight: Request[B] = r.copy(response = r.response.orFail)
175-
}
176-
177-
implicit class RichRequestTEitherResponseException[HE, DE, B](
178-
r: Request[Either[ResponseException[HE, DE], B]]
179-
) {
180-
def responseGetEither: Request[Either[HE, B]] = r.copy(response = r.response.orFailDeserialization)
181-
}
182-
}
183-
184186
//
185187

186188
/** Describes an HTTP request, along with a description of how the response body should be handled. Either the request

core/src/test/scala/sttp/client4/testing/BackendStubTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ class BackendStubTests extends AnyFlatSpec with Matchers with ScalaFutures {
144144

145145
val result = basicRequest
146146
.get(uri"http://example.org")
147-
.mapResponseRight(_.toInt)
148-
.mapResponseRight(_ * 2)
147+
.mapResponseRight((_: String).toInt)
148+
.mapResponseRight((_: Int) * 2)
149149
.send(backend)
150150

151151
result.body should be(Right(20))

core/src/test/scala/sttp/client4/testing/HttpTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ trait HttpTest[F[_]]
9292
"as string with mapping using mapResponse" in {
9393
postEcho
9494
.body(testBody)
95-
.mapResponseRight(_.length)
95+
.mapResponseRight((_: String).length)
9696
.send(backend)
9797
.toFuture()
9898
.map(response => response.body should be(Right(expectedPostEchoResponse.length)))
@@ -572,7 +572,7 @@ trait HttpTest[F[_]]
572572
}
573573

574574
"redirect when redirects should be followed, and the response is parsed" in {
575-
r2.response(asString).mapResponseRight(_.toInt).send(backend).toFuture().map { resp =>
575+
r2.response(asString).mapResponseRight((_: String).toInt).send(backend).toFuture().map { resp =>
576576
resp.code shouldBe StatusCode.Ok
577577
resp.body shouldBe Right(r4response.toInt)
578578
}

core/src/test/scalanative/sttp/client4/testing/SyncHttpTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ trait SyncHttpTest
6161
"as string with mapping using mapResponse" in {
6262
val response = postEcho
6363
.body(testBody)
64-
.mapResponseRight(_.length)
64+
.mapResponseRight((_: String).length)
6565
.send(backend)
6666
response.body should be(Right(expectedPostEchoResponse.length))
6767
}

0 commit comments

Comments
 (0)