Skip to content

Commit

Permalink
Remove specialize response-mapping request functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 11, 2024
1 parent f28dcea commit 1557c5f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 18 deletions.
14 changes: 1 addition & 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,7 @@ import sttp.attributes.AttributeMap
trait GenericRequest[+T, -R] extends RequestBuilder[GenericRequest[T, R]] with RequestMetadata {
def body: GenericRequestBody[R]
def response: ResponseAsDelegate[T, R]

def mapResponse[T2](f: T => T2): GenericRequest[T2, R]

def toCurl: String = ToCurlConverter(this)
Expand Down Expand Up @@ -168,19 +169,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,7 @@ class BackendStubTests extends AnyFlatSpec with Matchers with ScalaFutures {

val result = basicRequest
.get(uri"http://example.org")
.mapResponseRight(_.toInt)
.mapResponseRight(_ * 2)
.response(asString.mapRight((_: String).toInt).mapRight((_: 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)
.response(asString.mapRight((_: 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.mapRight((_: 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)
.response(asString.mapRight((_: String).length))
.send(backend)
response.body should be(Right(expectedPostEchoResponse.length))
}
Expand Down

0 comments on commit 1557c5f

Please sign in to comment.