Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #1

Open
wants to merge 1 commit into
base: series/0.19
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions modules/core/src/smithy4s/http/HttpDiscriminator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,45 @@ object HttpDiscriminator {
discriminatingHeaderNames: List[String],
statusCode: Int,
headers: Map[CaseInsensitive, Seq[String]]
): HttpDiscriminator = {
discriminatingHeaderNames.iterator
): HttpDiscriminator = fromStatusOrHeaderWithResults(
discriminatingHeaderNames,
statusCode,
headers
).discriminator

def fromStatusOrHeaderWithResults(
discriminatingHeaderNames: List[String],
statusCode: Int,
headers: Map[CaseInsensitive, Seq[String]]
): HttpDiscriminatorMatchResults = {
val (reasons, discriminator) = discriminatingHeaderNames
.map(CaseInsensitive(_))
.map(headers.get)
.collectFirst { case Some(h) => h }
.flatMap(_.headOption)
.map(errorType =>
ShapeId
.parse(errorType)
.map(FullId(_))
.getOrElse(NameOnly(errorType))
)
.getOrElse(
StatusCode(statusCode)
)
.foldLeft((List.empty[String], Option.empty[HttpDiscriminator])) {
(acc, item) =>
acc match {
case (info, None) =>
val header = headers
.get(item)
.flatMap(_.headOption)
header match {
case Some(errorType) =>
(
info,
Some(
ShapeId
.parse(errorType)
.map(FullId(_))
.getOrElse(NameOnly(errorType))
)
)
case None =>
(info :+ s"Cannot discriminate error by $item header due to missing response header", None)
}
case (reasons, header) => (reasons, header)
}
}

HttpDiscriminatorMatchResults(reasons, discriminator.getOrElse(StatusCode(statusCode)))
}

}
22 changes: 22 additions & 0 deletions modules/core/src/smithy4s/http/HttpDiscriminatorMatchResults.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2021-2024 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package smithy4s.http

final case class HttpDiscriminatorMatchResults(
failedToMatch: List[String],
discriminator: HttpDiscriminator
)
13 changes: 7 additions & 6 deletions modules/core/src/smithy4s/http/HttpResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ object HttpResponse {
toStrict: Body => F[(Body, Blob)]
): Decoder[F, Body, E] =
discriminating(
discriminate,
discriminate.andThen(df => MonadThrowLike[F].map(df)(d => (List.empty[String], d))),
HttpErrorSelector(maybeErrorSchema, decoderCompiler),
toStrict
)
Expand All @@ -215,11 +215,11 @@ object HttpResponse {
private[http] def forErrorAsThrowable[F[_]: MonadThrowLike, Body, E](
maybeErrorSchema: Option[ErrorSchema[E]],
decoderCompiler: CachedSchemaCompiler[Decoder[F, Body, *]],
discriminate: HttpResponse[Body] => F[HttpDiscriminator],
discriminate: HttpResponse[Body] => F[HttpDiscriminatorMatchResults],
toStrict: Body => F[(Body, Blob)]
): Decoder[F, Body, Throwable] =
discriminating(
discriminate,
discriminate.andThen(df => MonadThrowLike[F].map(df)(d => (d.failedToMatch, d.discriminator))),
HttpErrorSelector.asThrowable(maybeErrorSchema, decoderCompiler),
toStrict
)
Expand All @@ -229,23 +229,24 @@ object HttpResponse {
* to a given decoder, based on some discriminator.
*/
private def discriminating[F[_], Body, Discriminator, E](
discriminate: HttpResponse[Body] => F[Discriminator],
discriminate: HttpResponse[Body] => F[(List[String], Discriminator)],
select: Discriminator => Option[Decoder[F, Body, E]],
toStrict: Body => F[(Body, Blob)]
)(implicit F: MonadThrowLike[F]): Decoder[F, Body, E] =
new Decoder[F, Body, E] {
def decode(response: HttpResponse[Body]): F[E] = {
F.flatMap(toStrict(response.body)) { case (strictBody, bodyBlob) =>
val strictResponse = response.copy(body = strictBody)
F.flatMap(discriminate(strictResponse)) { discriminator =>
F.flatMap(discriminate(strictResponse)) { case (failedToMatch, discriminator) =>
select(discriminator) match {
case Some(decoder) => decoder.decode(strictResponse)
case None =>
F.raiseError(
smithy4s.http.UnknownErrorResponse(
response.statusCode,
response.headers,
bodyBlob.toUTF8String
bodyBlob.toUTF8String,
failedToMatch
)
)
}
Expand Down
13 changes: 10 additions & 3 deletions modules/core/src/smithy4s/http/HttpUnaryClientCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object HttpUnaryClientCodecs {
requestBodyEncoders = BlobEncoder.noop,
successResponseBodyDecoders = BlobDecoder.noop,
errorResponseBodyDecoders = BlobDecoder.noop,
errorDiscriminator = _ => F.pure(HttpDiscriminator.Undetermined),
errorDiscriminator = _ => F.pure(HttpDiscriminatorMatchResults(List.empty, HttpDiscriminator.Undetermined)),
metadataEncoders = None,
metadataDecoders = None,
rawStringsAndBlobPayloads = false,
Expand All @@ -56,6 +56,7 @@ object HttpUnaryClientCodecs {
def withSuccessBodyDecoders(decoders: BlobDecoder.Compiler): Builder[F, Request, Response]
def withErrorBodyDecoders(decoders: BlobDecoder.Compiler): Builder[F, Request, Response]
def withErrorDiscriminator(f: HttpResponse[Blob] => F[HttpDiscriminator]): Builder[F, Request, Response]
def withErrorDiscriminator(f: HttpResponse[Blob] => F[HttpDiscriminatorMatchResults]): Builder[F, Request, Response]
def withMetadataEncoders(encoders: Metadata.Encoder.Compiler): Builder[F, Request, Response]
def withMetadataDecoders(decoders: Metadata.Decoder.Compiler): Builder[F, Request, Response]
def withRawStringsAndBlobsPayloads: Builder[F, Request, Response]
Expand All @@ -73,7 +74,7 @@ object HttpUnaryClientCodecs {
requestBodyEncoders: BlobEncoder.Compiler,
successResponseBodyDecoders: BlobDecoder.Compiler,
errorResponseBodyDecoders: BlobDecoder.Compiler,
errorDiscriminator: HttpResponse[Blob] => F[HttpDiscriminator],
errorDiscriminator: HttpResponse[Blob] => F[HttpDiscriminatorMatchResults],
metadataEncoders: Option[Metadata.Encoder.Compiler],
metadataDecoders: Option[Metadata.Decoder.Compiler],
rawStringsAndBlobPayloads: Boolean,
Expand All @@ -95,6 +96,12 @@ object HttpUnaryClientCodecs {
def withErrorBodyDecoders(decoders: BlobDecoder.Compiler): Builder[F, Request, Response] =
copy(errorResponseBodyDecoders = decoders)
def withErrorDiscriminator(f: HttpResponse[Blob] => F[HttpDiscriminator]): Builder[F, Request, Response] =
copy(errorDiscriminator =
f.andThen(df => MonadThrowLike[F].map(df)(d => HttpDiscriminatorMatchResults(List.empty, d)))
)
def withErrorDiscriminator(
f: HttpResponse[Blob] => F[HttpDiscriminatorMatchResults]
): Builder[F, Request, Response] =
copy(errorDiscriminator = f)
def withMetadataEncoders(encoders: Metadata.Encoder.Compiler): Builder[F, Request, Response] =
copy(metadataEncoders = Some(encoders))
Expand Down Expand Up @@ -212,7 +219,7 @@ object HttpUnaryClientCodecs {
HttpResponse.Decoder.forErrorAsThrowable(
endpoint.error,
errorDecoders,
errorDiscriminator,
errorDiscriminator.andThen(df => MonadThrowLike[F].map(df)(_.discriminator)),
toStrict
)
new UnaryClientCodecs(inputEncoder, errorDecoder.decode, outputDecoder.decode)
Expand Down
5 changes: 3 additions & 2 deletions modules/core/src/smithy4s/http/UnknownErrorResponse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package smithy4s.http
case class UnknownErrorResponse(
code: Int,
headers: Map[CaseInsensitive, Seq[String]],
body: String
body: String,
discriminatorMatchingInfo: List[String]
) extends Throwable {
override def getMessage(): String =
s"status $code, headers: $headers, body:\n$body"
s"status $code, headers: $headers, body:\n$body, discriminators: ${discriminatorMatchingInfo.mkString(", ")}"
}
Loading