@@ -28,6 +28,8 @@ import sttp.attributes.AttributeMap
2828trait 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
0 commit comments