-
Notifications
You must be signed in to change notification settings - Fork 69
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
UrlForm and AWS query support #1101
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a81faa
UrlForm support
dhpiggott e5e1a2d
Wire up codec, client and compliance tests
dhpiggott 9238550
Fix tests... except the one that seems like it requires a remodel
dhpiggott 5e0e253
Disgusting hacks
dhpiggott 60a50c2
Less stinky solve for collection/map encoding difference
dhpiggott e8784b0
Smoke test the AWS query protocol by listing CloudWatch metrics
dhpiggott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
modules/aws-http4s/src/smithy4s/aws/internals/AwsQueryCodecs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
/* | ||
* Copyright 2023 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 | ||
package aws | ||
package internals | ||
|
||
import cats.effect.Concurrent | ||
import cats.syntax.all._ | ||
import fs2.compression.Compression | ||
import smithy4s.Endpoint | ||
import smithy4s.schema.CachedSchemaCompiler | ||
import org.http4s.EntityDecoder | ||
import smithy4s.xml.XmlDocument | ||
import org.http4s.MediaRange | ||
import fs2.data.xml._ | ||
import fs2.data.xml.dom._ | ||
// import smithy4s.codecs._ | ||
import smithy4s.http._ | ||
// import smithy4s.http.internals._ | ||
import smithy4s.http4s.kernel._ | ||
import cats.data.EitherT | ||
import smithy4s.kinds.PolyFunction | ||
import org.http4s.EntityEncoder | ||
import smithy4s.capability.Covariant | ||
import _root_.aws.protocols.AwsQueryError | ||
|
||
import smithy4s.http.Metadata | ||
|
||
private[aws] object AwsQueryCodecs { | ||
|
||
def make[F[_]: Concurrent: Compression]( | ||
version: String | ||
): UnaryClientCodecs.Make[F] = | ||
new UnaryClientCodecs.Make[F] { | ||
def apply[I, E, O, SI, SO]( | ||
endpoint: Endpoint.Base[I, E, O, SI, SO] | ||
): UnaryClientCodecs[F, I, E, O] = { | ||
|
||
val stringAndBlobsEntityDecoders = | ||
smithy4s.http.StringAndBlobCodecs.ReaderCompiler | ||
.mapK( | ||
Covariant.liftPolyFunction[Option]( | ||
EntityDecoders.fromHttpMediaReaderK[F] | ||
) | ||
) | ||
|
||
val urlFormEntityEncoders: CachedSchemaCompiler[EntityEncoder[F, *]] = | ||
UrlForm.Encoder | ||
.mapK( | ||
new PolyFunction[UrlForm.Encoder, EntityEncoder[F, *]] { | ||
def apply[A](fa: UrlForm.Encoder[A]): EntityEncoder[F, A] = | ||
urlFormEntityEncoder[F].contramap(fa.encode) | ||
} | ||
) | ||
.contramapSchema( | ||
smithy4s.schema.Schema.transformHintsLocallyK( | ||
_ ++ smithy4s.Hints( | ||
smithy4s.http.UrlForm.Action(endpoint.id.name), | ||
smithy4s.http.UrlForm.Version(version) | ||
) | ||
) | ||
) | ||
|
||
// TODO: Not needed here, but will be needed for an OAuth server | ||
// val urlFormEntityDecoders: CachedSchemaCompiler[EntityDecoder[F, *]] = | ||
// UrlForm.Decoder.mapK( | ||
// new PolyFunction[UrlForm.Decoder, EntityDecoder[F, *]] { | ||
// def apply[A]( | ||
// fa: UrlForm.Decoder[A] | ||
// ): EntityDecoder[F, A] = | ||
// urlFormEntityDecoder[F].flatMapR(urlForm => | ||
// EitherT.liftF { | ||
// fa.decode(urlForm) | ||
// .leftMap(fromUrlFormToHttpError) | ||
// .liftTo[F] | ||
// } | ||
// ) | ||
// } | ||
// ) | ||
|
||
val xmlEntityDecoders: CachedSchemaCompiler[EntityDecoder[F, *]] = | ||
XmlDocument.Decoder.mapK( | ||
new PolyFunction[XmlDocument.Decoder, EntityDecoder[F, *]] { | ||
def apply[A]( | ||
fa: XmlDocument.Decoder[A] | ||
): EntityDecoder[F, A] = | ||
xmlEntityDecoder[F].flatMapR(xmlDocument => | ||
EitherT.liftF { | ||
fa.decode(xmlDocument) | ||
.leftMap(fromXmlToHttpError) | ||
.liftTo[F] | ||
} | ||
) | ||
} | ||
) | ||
|
||
val decoders = CachedSchemaCompiler.getOrElse( | ||
stringAndBlobsEntityDecoders, | ||
xmlEntityDecoders | ||
) | ||
|
||
val restEncoders = | ||
RequestEncoder | ||
.restSchemaCompiler[F]( | ||
Metadata.AwsEncoder, | ||
urlFormEntityEncoders, | ||
writeEmptyStructs = true | ||
) | ||
|
||
val restDecoders = | ||
ResponseDecoder.restSchemaCompiler(Metadata.AwsDecoder, decoders) | ||
|
||
val responseTag = endpoint.name + "Response" | ||
val resultTag = endpoint.name + "Result" | ||
val successDecoders = restDecoders.contramapSchema( | ||
smithy4s.schema.Schema.transformHintsLocallyK( | ||
_ ++ smithy4s.Hints( | ||
smithy4s.xml.internals.XmlStartingPath( | ||
List(responseTag, resultTag) | ||
) | ||
) | ||
) | ||
) | ||
|
||
val errorDecoders = restDecoders.contramapSchema( | ||
smithy4s.schema.Schema.transformHintsLocallyK( | ||
_ ++ smithy4s.Hints( | ||
smithy4s.xml.internals.XmlStartingPath( | ||
List("ErrorResponse", "Error") | ||
) | ||
) | ||
) | ||
) | ||
|
||
// Takes the `@awsQueryError` trait into consideration to decide | ||
// how to discriminate error responses. | ||
val errorNameMapping: String => String = { | ||
endpoint.errorable match { | ||
case None => identity[String] | ||
case Some(err) => | ||
val mapping = err.error.alternatives.flatMap { alt => | ||
val shapeName = alt.schema.shapeId.name | ||
alt.hints.get(AwsQueryError).map(_.code).map(_ -> shapeName) | ||
}.toMap | ||
(errorCode: String) => mapping.getOrElse(errorCode, errorCode) | ||
} | ||
} | ||
|
||
val discriminator = AwsErrorTypeDecoder | ||
.fromResponse(errorDecoders) | ||
.andThen(_.map(_.map { | ||
case HttpDiscriminator.NameOnly(name) => | ||
HttpDiscriminator.NameOnly(errorNameMapping(name)) | ||
case other => other | ||
})) | ||
|
||
val transformEncoders = | ||
applyCompression[F](endpoint.hints, retainUserEncoding = false) | ||
val finalEncoders = transformEncoders(restEncoders) | ||
|
||
val make = | ||
UnaryClientCodecs | ||
.Make[F]( | ||
finalEncoders, | ||
successDecoders, | ||
errorDecoders, | ||
discriminator | ||
) | ||
make.apply(endpoint) | ||
} | ||
} | ||
|
||
private def xmlEntityDecoder[F[_]: Concurrent] | ||
: EntityDecoder[F, XmlDocument] = | ||
EntityDecoder.decodeBy( | ||
MediaRange.parse("application/xml").getOrElse(sys.error("TODO")) | ||
) { media => | ||
EitherT.liftF( | ||
media.body | ||
.through(fs2.text.utf8.decode[F]) | ||
.through(events[F, String]()) | ||
.through(referenceResolver()) | ||
.through(normalize) | ||
.through(documents[F, XmlDocument]) | ||
.head | ||
.compile | ||
.last | ||
.map( | ||
_.getOrElse( | ||
// TODO: This isn't right | ||
XmlDocument( | ||
XmlDocument.XmlElem( | ||
XmlDocument.XmlQName(None, "Unit"), | ||
List.empty, | ||
List.empty | ||
) | ||
) | ||
) | ||
) | ||
) | ||
} | ||
|
||
// TODO: Not needed here, but will be needed for an OAuth server | ||
// private def urlFormEntityDecoder[F[_]: Concurrent]: EntityDecoder[F, UrlForm] = EntityDecoders.fromHttpMediaReader( | ||
// HttpMediaTyped( | ||
// HttpMediaType("application/x-www-form-urlencoded"), | ||
// // TODO: Avoid going to string and back | ||
// // TODO: Make error mapping consistent with elsewhere | ||
// blob => UrlFormParser.parseUrlForm(blob.toUTF8String).left.map(parseFailure => | ||
// HttpPayloadError( | ||
// PayloadPath.root, | ||
// "", | ||
// parseFailure.message | ||
// ) | ||
// ) | ||
// ) | ||
// ) | ||
|
||
private def urlFormEntityEncoder[F[_]: Concurrent] | ||
: EntityEncoder[F, UrlForm] = EntityEncoders.fromHttpMediaWriter( | ||
HttpMediaTyped( | ||
HttpMediaType("application/x-www-form-urlencoded"), | ||
(_: Any, urlForm: UrlForm) => Blob(urlForm.values.render) | ||
) | ||
) | ||
|
||
private def fromXmlToHttpError( | ||
xmlDecodeError: smithy4s.xml.XmlDecodeError | ||
): smithy4s.http.HttpContractError = { | ||
smithy4s.http.HttpPayloadError( | ||
xmlDecodeError.path.toPayloadPath, | ||
"", | ||
xmlDecodeError.message | ||
) | ||
} | ||
|
||
// TODO: Not needed here, but will be needed for an OAuth server | ||
// private def fromUrlFormToHttpError( | ||
// urlFormDecodeError: smithy4s.http.UrlFormDecodeError | ||
// ): smithy4s.http.HttpContractError = { | ||
// smithy4s.http.HttpPayloadError( | ||
// urlFormDecodeError.path, | ||
// "", | ||
// urlFormDecodeError.message | ||
// ) | ||
// } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
related: #1075