Skip to content

Commit 4564784

Browse files
committed
Revert "Remove specialize response-mapping request functions"
This reverts commit 1557c5f.
1 parent bdb8d00 commit 4564784

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ 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-
3231
def mapResponse[T2](f: T => T2): GenericRequest[T2, R]
3332

3433
def toCurl: String = ToCurlConverter(this)
@@ -169,6 +168,19 @@ case class Request[T](
169168
def send(backend: SyncBackend): Response[T] = backend.send(this)
170169
}
171170

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+
172184
//
173185

174186
/** 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ class BackendStubTests extends AnyFlatSpec with Matchers with ScalaFutures {
144144

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

150151
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-
.response(asString.mapRight((_: String).length))
95+
.mapResponseRight(_.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.mapRight((_: String).toInt)).send(backend).toFuture().map { resp =>
575+
r2.response(asString).mapResponseRight(_.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-
.response(asString.mapRight((_: String).length))
64+
.mapResponseRight(_.length)
6565
.send(backend)
6666
response.body should be(Right(expectedPostEchoResponse.length))
6767
}

0 commit comments

Comments
 (0)