Skip to content

Commit

Permalink
Update HttpContentCodec.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 authored Aug 31, 2024
1 parent b9a7c07 commit 89f4dc0
Showing 1 changed file with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import zio._
import zio.stream.ZPipeline

import zio.schema.codec._
import zio.schema.{ DeriveSchema, Schema }
import zio.schema.{DeriveSchema, Schema}

import zio.http.Header.Accept.MediaTypeWithQFactor
import zio.http._
Expand All @@ -34,7 +34,7 @@ sealed trait HttpContentCodec[A] { self =>
request.body.asChunk.flatMap { bytes =>
ZIO.fromEither(codec.codec(config).decode(bytes))
}
case None =>
case None =>
ZIO.fail(throw new IllegalArgumentException(s"No codec found for content type $contentType"))
}
}
Expand All @@ -49,7 +49,7 @@ sealed trait HttpContentCodec[A] { self =>
response.body.asChunk.flatMap { bytes =>
ZIO.fromEither(codec.codec(config).decode(bytes))
}
case None =>
case None =>
ZIO.fail(throw new IllegalArgumentException(s"No codec found for content type $contentType"))
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ sealed trait HttpContentCodec[A] { self =>
}

private[http] def chooseFirstOrDefault(
mediaTypes: Chunk[MediaTypeWithQFactor]
mediaTypes: Chunk[MediaTypeWithQFactor],
): (MediaType, BinaryCodecWithSchema[A]) =
if (mediaTypes.isEmpty) {
(defaultMediaType, defaultBinaryCodecWithSchema)
Expand Down Expand Up @@ -157,17 +157,17 @@ object HttpContentCodec {

def from[A](
codec: (MediaType, BinaryCodecWithSchema[A]),
codecs: (MediaType, BinaryCodecWithSchema[A])*
codecs: (MediaType, BinaryCodecWithSchema[A])*,
): HttpContentCodec[A] =
Default(ListMap((codec +: codecs): _*))

implicit def fromSchema[A](implicit schema: Schema[A]): HttpContentCodec[A] =
schemaCache
.getOrElseUpdate(schema, _: Schema[A] => json.only[A] ++ protobuf.only[A] ++ text.only[A])
.getOrElseUpdate(schema, json.only[A] ++ protobuf.only[A] ++ text.only[A])
.asInstanceOf[HttpContentCodec[A]]

final private case class Default[A](
choices: ListMap[MediaType, BinaryCodecWithSchema[A]]
choices: ListMap[MediaType, BinaryCodecWithSchema[A]],
) extends HttpContentCodec[A]

final case class Filtered[A](codec: HttpContentCodec[A], mediaType: MediaType) extends HttpContentCodec[A] {
Expand All @@ -176,8 +176,8 @@ object HttpContentCodec {
mediaType -> codec
.lookup(mediaType)
.getOrElse(
throw new IllegalArgumentException(s"MediaType $mediaType is not supported by $codec")
)
throw new IllegalArgumentException(s"MediaType $mediaType is not supported by $codec"),
),
)
}

Expand Down Expand Up @@ -215,47 +215,49 @@ object HttpContentCodec {
case (Some(name), Some(message)) => Right(HttpCodecError.CustomError(name, message))
case _ => Left("Could not extract name and message from the DOM")
}
}, {
},
{
case HttpCodecError.CustomError(name, message) =>
Right(
html(
body(
h1("Codec Error"),
p("There was an error en-/decoding the request/response"),
p(name, idAttr := "name"),
p(message, idAttr := "message")
)
)
p(name, idAttr := "name"),
p(message, idAttr := "message"),
),
),
)
case e: HttpCodecError =>
case e: HttpCodecError =>
Right(
html(
body(
h1("Codec Error"),
p("There was an error en-/decoding the request/response"),
p(e.productPrefix, idAttr := "name"),
p(e.getMessage(), idAttr := "message")
)
)
p(e.getMessage(), idAttr := "message"),
),
),
)
}
},
)

private val defaultCodecErrorSchema: Schema[HttpCodecError] =
Schema[DefaultCodecError].transformOrFail[HttpCodecError](
codecError => Right(HttpCodecError.CustomError(codecError.name, codecError.message)), {
codecError => Right(HttpCodecError.CustomError(codecError.name, codecError.message)),
{
case HttpCodecError.CustomError(name, message) => Right(DefaultCodecError(name, message))
case e: HttpCodecError => Right(DefaultCodecError(e.productPrefix, e.getMessage()))
}
},
)

private val defaultHttpContentCodec: HttpContentCodec[HttpCodecError] =
HttpContentCodec.from(
MediaType.text.`html` -> BinaryCodecWithSchema(TextBinaryCodec.fromSchema(domBasedSchema), domBasedSchema),
MediaType.text.`html` -> BinaryCodecWithSchema(TextBinaryCodec.fromSchema(domBasedSchema), domBasedSchema),
MediaType.application.json -> BinaryCodecWithSchema(
JsonCodec.schemaBasedBinaryCodec(defaultCodecErrorSchema),
defaultCodecErrorSchema
)
defaultCodecErrorSchema,
),
)

val responseErrorCodec: HttpCodec[HttpCodecType.ResponseType, HttpCodecError] =
Expand All @@ -270,11 +272,11 @@ object HttpContentCodec {
BinaryCodecWithSchema(
config =>
JsonCodec.schemaBasedBinaryCodec[A](
JsonCodec.Config(ignoreEmptyCollections = config.ignoreEmptyCollections)
JsonCodec.Config(ignoreEmptyCollections = config.ignoreEmptyCollections),
)(schema),
schema
)
)
schema,
),
),
)
}

Expand All @@ -284,8 +286,8 @@ object HttpContentCodec {
Default(
ListMap(
MediaType.parseCustomMediaType("application/protobuf").get ->
BinaryCodecWithSchema(ProtobufCodec.protobufCodec[A], schema)
)
BinaryCodecWithSchema(ProtobufCodec.protobufCodec[A], schema),
),
)
}

Expand All @@ -294,11 +296,11 @@ object HttpContentCodec {
def only[A](implicit schema: Schema[A]): HttpContentCodec[A] =
Default(
ListMap(
MediaType.text.`plain` ->
MediaType.text.`plain` ->
BinaryCodecWithSchema(zio.http.codec.internal.TextBinaryCodec.fromSchema[A](schema), schema),
MediaType.application.`octet-stream` ->
BinaryCodecWithSchema(zio.http.codec.internal.TextBinaryCodec.fromSchema[A](schema), schema)
)
BinaryCodecWithSchema(zio.http.codec.internal.TextBinaryCodec.fromSchema[A](schema), schema),
),
)
}

Expand All @@ -317,12 +319,12 @@ object HttpContentCodec {
}

implicit val byteChunkCodec: HttpContentCodec[Chunk[Byte]] = {
HttpContentCodec(
Default(
ListMap(
MediaType.allMediaTypes
.filter(_.binary)
.map(mt => mt -> BinaryCodecWithSchema(ByteChunkBinaryCodec, Schema.chunk[Byte])): _*
)
.map(mt => mt -> BinaryCodecWithSchema(ByteChunkBinaryCodec, Schema.chunk[Byte])): _*,
),
)
}

Expand All @@ -341,12 +343,12 @@ object HttpContentCodec {
}

implicit val byteCodec: HttpContentCodec[Byte] = {
HttpContentCodec(
Default(
ListMap(
MediaType.allMediaTypes
.filter(_.binary)
.map(mt => mt -> BinaryCodecWithSchema(ByteBinaryCodec, Schema[Byte])): _*
)
.map(mt => mt -> BinaryCodecWithSchema(ByteBinaryCodec, Schema[Byte])): _*,
),
)
}
}

0 comments on commit 89f4dc0

Please sign in to comment.