|
| 1 | +package software.amazon.smithy.aws.swift.codegen.customization.s3 |
| 2 | + |
| 3 | +import software.amazon.smithy.aws.swift.codegen.AWSClientRuntimeTypes |
| 4 | +import software.amazon.smithy.aws.swift.codegen.restxml.AWSRestXMLHttpResponseBindingErrorGenerator |
| 5 | +import software.amazon.smithy.codegen.core.Symbol |
| 6 | +import software.amazon.smithy.model.Model |
| 7 | +import software.amazon.smithy.model.shapes.ServiceShape |
| 8 | +import software.amazon.smithy.model.shapes.StructureShape |
| 9 | +import software.amazon.smithy.swift.codegen.ClientRuntimeTypes |
| 10 | +import software.amazon.smithy.swift.codegen.StructureGenerator |
| 11 | +import software.amazon.smithy.swift.codegen.SwiftSettings |
| 12 | +import software.amazon.smithy.swift.codegen.SwiftTypes |
| 13 | +import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator |
| 14 | +import software.amazon.smithy.swift.codegen.integration.SectionWriter |
| 15 | +import software.amazon.smithy.swift.codegen.integration.SectionWriterBinding |
| 16 | +import software.amazon.smithy.swift.codegen.integration.SwiftIntegration |
| 17 | +import software.amazon.smithy.swift.codegen.integration.httpResponse.HttpResponseBindingErrorInitGenerator |
| 18 | +import software.amazon.smithy.swift.codegen.integration.httpResponse.HttpResponseBindingErrorNarrowGenerator |
| 19 | +import software.amazon.smithy.swift.codegen.model.expectShape |
| 20 | + |
| 21 | +class S3ErrorIntegration : SwiftIntegration { |
| 22 | + override val order: Byte |
| 23 | + get() = 127 |
| 24 | + |
| 25 | + override fun enabledForService(model: Model, settings: SwiftSettings): Boolean { |
| 26 | + return model.expectShape<ServiceShape>(settings.service).isS3 |
| 27 | + } |
| 28 | + override val sectionWriters: List<SectionWriterBinding> |
| 29 | + get() = listOf( |
| 30 | + SectionWriterBinding(HttpResponseBindingErrorInitGenerator.HttpResponseBindingErrorInit, s3MembersParams), |
| 31 | + SectionWriterBinding(HttpResponseBindingErrorInitGenerator.HttpResponseBindingErrorInitMemberAssignment, s3MembersAssignment), |
| 32 | + SectionWriterBinding(StructureGenerator.AdditionalErrorMembers, s3Members), |
| 33 | + SectionWriterBinding(HttpResponseBindingErrorNarrowGenerator.HttpResponseBindingErrorNarrowGeneratorSectionId, httpResponseBindingErrorNarrow), |
| 34 | + SectionWriterBinding(AWSRestXMLHttpResponseBindingErrorGenerator.RestXMLResponseBindingSectionId, httpResponseBinding) |
| 35 | + |
| 36 | + ) |
| 37 | + |
| 38 | + private val s3MembersParams = SectionWriter { writer, _ -> |
| 39 | + writer.write( |
| 40 | + "public init (httpResponse: \$N, decoder: \$D, message: \$D, requestID: \$D, requestID2: \$D) throws {", |
| 41 | + ClientRuntimeTypes.Http.HttpResponse, |
| 42 | + ClientRuntimeTypes.Serde.ResponseDecoder, |
| 43 | + SwiftTypes.String, |
| 44 | + SwiftTypes.String, |
| 45 | + SwiftTypes.String |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + private val s3MembersAssignment = SectionWriter { writer, _ -> |
| 50 | + writer.write("self._requestID2 = requestID2") |
| 51 | + } |
| 52 | + |
| 53 | + private val s3Members = SectionWriter { writer, _ -> |
| 54 | + writer.write("public var _requestID2: \$T", SwiftTypes.String) |
| 55 | + } |
| 56 | + |
| 57 | + private val httpResponseBinding = SectionWriter { writer, _ -> |
| 58 | + val operationErrorName = writer.getContext("operationErrorName") as String |
| 59 | + writer.write("let errorDetails = $operationErrorName.isNotFoundAndEmptyBody(httpResponse: httpResponse)") |
| 60 | + writer.write(" ? $operationErrorName.constructRestXMLError(httpResponse: httpResponse)") |
| 61 | + writer.write(" : try \$N(httpResponse: httpResponse)", AWSClientRuntimeTypes.RestXML.RestXMLError) |
| 62 | + writer.write("let requestID2 = $operationErrorName.getRequestId2(httpResponse: httpResponse)") |
| 63 | + writer.write("try self.init(errorType: errorDetails.errorCode, httpResponse: httpResponse, decoder: decoder, message: errorDetails.message, requestID: errorDetails.requestId, requestID2: requestID2)") |
| 64 | + } |
| 65 | + |
| 66 | + private val httpResponseBindingErrorNarrow = SectionWriter { writer, _ -> |
| 67 | + val ctx = writer.getContext("ctx") as ProtocolGenerator.GenerationContext |
| 68 | + val unknownServiceErrorType = writer.getContext("unknownServiceErrorType") as String |
| 69 | + val operationErrorName = writer.getContext("operationErrorName") as String |
| 70 | + val errorShapes = writer.getContext("errorShapes") as List<StructureShape> |
| 71 | + |
| 72 | + writer.openBlock("extension \$L {", "}", operationErrorName) { |
| 73 | + writer.write( |
| 74 | + "public init(errorType: \$T, httpResponse: \$N, decoder: \$D, message: \$D, requestID: \$D, requestID2: \$D) throws {", |
| 75 | + SwiftTypes.String, |
| 76 | + ClientRuntimeTypes.Http.HttpResponse, |
| 77 | + ClientRuntimeTypes.Serde.ResponseDecoder, |
| 78 | + SwiftTypes.String, |
| 79 | + SwiftTypes.String, |
| 80 | + SwiftTypes.String |
| 81 | + ) |
| 82 | + writer.indent() |
| 83 | + writer.write("switch errorType {") |
| 84 | + for (errorShape in errorShapes) { |
| 85 | + var errorShapeName = ctx.symbolProvider.toSymbol(errorShape).name |
| 86 | + var errorShapeType = ctx.symbolProvider.toSymbol(errorShape).name |
| 87 | + var errorShapeEnumCase = errorShapeType.decapitalize() |
| 88 | + writer.write("case \$S : self = .\$L(try \$L(httpResponse: httpResponse, decoder: decoder, message: message, requestID: requestID, requestID2: requestID2))", errorShapeName, errorShapeEnumCase, errorShapeType) |
| 89 | + } |
| 90 | + writer.write("default : self = .unknown($unknownServiceErrorType(httpResponse: httpResponse, message: message, requestID: requestID))") |
| 91 | + writer.write("}") |
| 92 | + writer.dedent() |
| 93 | + writer.write("}") |
| 94 | + } |
| 95 | + |
| 96 | + writer.openBlock("extension $operationErrorName {", "}") { |
| 97 | + writer.openBlock("static func isNotFoundAndEmptyBody(httpResponse: HttpResponse) -> Bool {", "}") { |
| 98 | + writer.write("if case .none = httpResponse.body {") |
| 99 | + writer.indent() |
| 100 | + writer.write("return httpResponse.statusCode == .notFound") |
| 101 | + writer.dedent() |
| 102 | + writer.write("} else if case .empty = httpResponse.body {") |
| 103 | + writer.indent() |
| 104 | + writer.write("return httpResponse.statusCode == .notFound") |
| 105 | + writer.dedent() |
| 106 | + writer.write("}") |
| 107 | + writer.write("return false") |
| 108 | + } |
| 109 | + |
| 110 | + writer.openBlock("static func constructRestXMLError(httpResponse: HttpResponse) -> AWSClientRuntime.RestXMLError {", "}") { |
| 111 | + writer.write("return RestXMLError(errorCode: \"NotFound\", requestId: httpResponse.headers.value(for: \"x-amz-request-id\"))") |
| 112 | + } |
| 113 | + writer.openBlock("static func getRequestId2(httpResponse: HttpResponse) -> String? {", "}") { |
| 114 | + writer.write("return httpResponse.headers.value(for: \"x-amz-id-2\")") |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + override fun serviceErrorProtocolSymbol(): Symbol? { |
| 120 | + return AWSClientRuntimeTypes.RestXML.S3.S3HttpServiceError |
| 121 | + } |
| 122 | +} |
0 commit comments