I am using Scala STTP
I have a call to an api that is constructed as this:
val request = basicRequest
.get(uri"$fullUri")
.headers(headers)
.response(asJson[MyClass])
Is it possible to cast errors returned to a different case class with similar approach (ex: asJson
method)
So basically I would want to add that potential cast in response body result here:
case Success(result) =>
if (result.code != StatusCode.Ok) {
# try to cast here
}
result.body match {
case Left(error) =>
# Try to cast here
case Right(r) => Right(r)
}
I believe that a conditional response
ConditionalResponseAs
is what you are looking for. Example of usage:My example using
asJsonAlwaysUnsafe
, but it only because I have such an example at hand, you are free to useasJson
.