codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/AwsHttpPresignURLClientGenerator.java
private void writeConvertToPresignMiddleware(
            GoWriter writer,
            Model model,
            SymbolProvider symbolProvider,
            ServiceShape serviceShape
    ) {
        Symbol smithyStack = SymbolUtils.createPointableSymbolBuilder("Stack", SmithyGoDependency.SMITHY_MIDDLEWARE)
                .build();

        writer.write("type $L $T", CONVERT_TO_PRESIGN_TYPE_NAME, presignOptionsSymbol);
        writer.openBlock("func (c $L) $L(stack $P, options Options) (err error) {", "}",
                CONVERT_TO_PRESIGN_TYPE_NAME,
                CONVERT_TO_PRESIGN_MIDDLEWARE_NAME,
                smithyStack,
                () -> {
                    Symbol smithyAfter = SymbolUtils.createValueSymbolBuilder("After",
                            SmithyGoDependency.SMITHY_MIDDLEWARE)
                            .build();

                    // Middleware to remove
                    Symbol requestInvocationID = SymbolUtils.createPointableSymbolBuilder(
                            "ClientRequestID",
                            AwsGoDependency.AWS_MIDDLEWARE)
                            .build();

                    Symbol presignMiddleware = SymbolUtils.createValueSymbolBuilder("NewPresignHTTPRequestMiddleware",
                            AwsGoDependency.AWS_SIGNER_V4)
                            .build();

                    // Middleware to add
                    writer.write("stack.Finalize.Clear()");
                    writer.write("stack.Deserialize.Clear()");
                    writer.write("stack.Build.Remove(($P)(nil).ID())", requestInvocationID);
                    writer.write("stack.Build.Remove($S)", "UserAgent");

                    Symbol middlewareOptionsSymbol = SymbolUtils.createValueSymbolBuilder(
                            "PresignHTTPRequestMiddlewareOptions", AwsGoDependency.AWS_SIGNER_V4).build();
                    writer.openBlock("pmw := $T($T{", "})", presignMiddleware, middlewareOptionsSymbol, () -> {
                        writer.write("CredentialsProvider: options.$L,", AddAwsConfigFields.CREDENTIALS_CONFIG_NAME);
                        writer.write("Presigner: c.Presigner,");
                        writer.write("LogSigning: options.$L.IsSigning(),", AddAwsConfigFields.LOG_MODE_CONFIG_NAME);
                    });
                    writer.write("err = stack.Finalize.Add(pmw, $T)", smithyAfter);
                    writer.write("if err != nil { return err }");

                    // if protocol used is ec2query or query
                    if (serviceShape.hasTrait(AwsQueryTrait.ID) || serviceShape.hasTrait(Ec2QueryTrait.ID)) {
                        // presigned url should convert to Get request
                        Symbol queryAsGetMiddleware = SymbolUtils.createValueSymbolBuilder("AddAsGetRequestMiddleware",
                                AwsGoDependency.AWS_QUERY_PROTOCOL).build();

                        writer.writeDocs("convert request to a GET request");
                        writer.write("err = $T(stack)", queryAsGetMiddleware);
                        writer.write("if err != nil { return err }");
                    }

                    // s3 service needs expires and sets unsignedPayload if input is stream
                    if (isS3ServiceShape(model, serviceShape)) {

                        writer.write("");
                        writer.write("// add multi-region access point presigner");

                        // ==== multi-region access point support
                       Symbol PresignConstructor =  SymbolUtils.createValueSymbolBuilder(
                               "NewPresignHTTPRequestMiddleware", AwsCustomGoDependency.S3_CUSTOMIZATION
                       ).build();

                       Symbol PresignOptions = SymbolUtils.createValueSymbolBuilder(
                         "PresignHTTPRequestMiddlewareOptions", AwsCustomGoDependency.S3_CUSTOMIZATION
                       ).build();

                       Symbol RegisterPresigningMiddleware = SymbolUtils.createValueSymbolBuilder(
                               "RegisterPreSigningMiddleware", AwsCustomGoDependency.S3_CUSTOMIZATION
                       ).build();

                        writer.openBlock("signermv := $T($T{", "})",
                                PresignConstructor,PresignOptions, () -> {
                           writer.write("CredentialsProvider : options.Credentials,");
                           writer.write("V4Presigner : c.Presigner,");
                           writer.write("V4aPresigner : c.presignerV4a,");
                           writer.write("LogSigning : options.ClientLogMode.IsSigning(),");
                        });

                        writer.write("err = $T(stack, signermv)", RegisterPresigningMiddleware);
                        writer.write("if err != nil { return err }");
                        writer.write("");

                        // =======

                        writer.openBlock("if c.Expires < 0 {", "}", () -> {
                            writer.addUseImports(SmithyGoDependency.FMT);
                            writer.write(
                                    "return fmt.Errorf(\"presign URL duration must be 0 or greater, %v\", c.Expires)");
                        });
                        Symbol expiresAsHeaderMiddleware = SymbolUtils.createValueSymbolBuilder(
                                "AddExpiresOnPresignedURL",
                                AwsCustomGoDependency.S3_CUSTOMIZATION).build();
                        writer.writeDocs("add middleware to set expiration for s3 presigned url, "
                                + " if expiration is set to 0, this middleware sets a default expiration of 900 seconds");
                        writer.write("err = stack.Build.Add(&$T{ Expires: c.Expires, }, middleware.After)",
                                expiresAsHeaderMiddleware);
                        writer.write("if err != nil { return err }");
                    }

                    Symbol addAsPresignMiddlewareSymbol = SymbolUtils.createValueSymbolBuilder("AddAsIsPresigingMiddleware",
                            AwsCustomGoDependency.PRESIGNEDURL_CUSTOMIZATION).build();
                    writer.write("err = $T(stack)", addAsPresignMiddlewareSymbol);
                    writer.write("if err != nil { return err }");

                    writer.write("return nil");
                }).insertTrailingNewline();
    }
/**
     * generates a helper to mutate request middleware stack in favor of generating a presign URL request
     *
     * @param writer         the writer to write to
     * @param model          the service model
     * @param symbolProvider the symbol provider
     * @param serviceShape   the service for which helper is generated
     */
generates a helper to mutate request middleware stack in favor of generating a presign URL request
@param writer         the writer to write to
@param model          the service model
@param symbolProvider the symbol provider
@param serviceShape   the service for which helper is generated
generates a helper to mutate request middleware stack in favor of generating a presign URL request @param writer the writer to write to @param model the service model @param symbolProvider the symbol provider @param serviceShape the service for which helper is generated
private void writeConvertToPresignMiddleware(
            GoWriter writer,
            Model model,
            SymbolProvider symbolProvider,
            ServiceShape serviceShape
    ) {
        Symbol smithyStack = SymbolUtils.createPointableSymbolBuilder("Stack", SmithyGoDependency.SMITHY_MIDDLEWARE)
                .build();
        writer.write("type $L $T", CONVERT_TO_PRESIGN_TYPE_NAME, presignOptionsSymbol);
        writer.openBlock("func (c $L) $L(stack $P, options Options) (err error) {", "}",
                CONVERT_TO_PRESIGN_TYPE_NAME,
                CONVERT_TO_PRESIGN_MIDDLEWARE_NAME,
                smithyStack,
                () -> {
                    Symbol smithyAfter = SymbolUtils.createValueSymbolBuilder("After",
                            SmithyGoDependency.SMITHY_MIDDLEWARE)
                            .build();
                    Symbol requestInvocationID = SymbolUtils.createPointableSymbolBuilder(
                            "ClientRequestID",
                            AwsGoDependency.AWS_MIDDLEWARE)
                            .build();
                    Symbol presignMiddleware = SymbolUtils.createValueSymbolBuilder("NewPresignHTTPRequestMiddleware",
                            AwsGoDependency.AWS_SIGNER_V4)
                            .build();
                    writer.write("stack.Finalize.Clear()");
                    writer.write("stack.Deserialize.Clear()");
                    writer.write("stack.Build.Remove(($P)(nil).ID())", requestInvocationID);
                    writer.write("stack.Build.Remove($S)", "UserAgent");
                    Symbol middlewareOptionsSymbol = SymbolUtils.createValueSymbolBuilder(
                            "PresignHTTPRequestMiddlewareOptions", AwsGoDependency.AWS_SIGNER_V4).build();
                    writer.openBlock("pmw := $T($T{", "})", presignMiddleware, middlewareOptionsSymbol, () -> {
                        writer.write("CredentialsProvider: options.$L,", AddAwsConfigFields.CREDENTIALS_CONFIG_NAME);
                        writer.write("Presigner: c.Presigner,");
                        writer.write("LogSigning: options.$L.IsSigning(),", AddAwsConfigFields.LOG_MODE_CONFIG_NAME);
                    });
                    writer.write("err = stack.Finalize.Add(pmw, $T)", smithyAfter);
                    writer.write("if err != nil { return err }");
                    if (serviceShape.hasTrait(AwsQueryTrait.ID) || serviceShape.hasTrait(Ec2QueryTrait.ID)) {
                        Symbol queryAsGetMiddleware = SymbolUtils.createValueSymbolBuilder("AddAsGetRequestMiddleware",
                                AwsGoDependency.AWS_QUERY_PROTOCOL).build();
                        writer.writeDocs("convert request to a GET request");
                        writer.write("err = $T(stack)", queryAsGetMiddleware);
                        writer.write("if err != nil { return err }");
                    }
                    if (isS3ServiceShape(model, serviceShape)) {
                        writer.write("");
                        writer.write("// add multi-region access point presigner");
                       Symbol PresignConstructor =  SymbolUtils.createValueSymbolBuilder(
                               "NewPresignHTTPRequestMiddleware", AwsCustomGoDependency.S3_CUSTOMIZATION
                       ).build();
                       Symbol PresignOptions = SymbolUtils.createValueSymbolBuilder(
                         "PresignHTTPRequestMiddlewareOptions", AwsCustomGoDependency.S3_CUSTOMIZATION
                       ).build();
                       Symbol RegisterPresigningMiddleware = SymbolUtils.createValueSymbolBuilder(
                               "RegisterPreSigningMiddleware", AwsCustomGoDependency.S3_CUSTOMIZATION
                       ).build();
                        writer.openBlock("signermv := $T($T{", "})",
                                PresignConstructor,PresignOptions, () -> {
                           writer.write("CredentialsProvider : options.Credentials,");
                           writer.write("V4Presigner : c.Presigner,");
                           writer.write("V4aPresigner : c.presignerV4a,");
                           writer.write("LogSigning : options.ClientLogMode.IsSigning(),");
                        });
                        writer.write("err = $T(stack, signermv)", RegisterPresigningMiddleware);
                        writer.write("if err != nil { return err }");
                        writer.write("");
                        writer.openBlock("if c.Expires < 0 {", "}", () -> {
                            writer.addUseImports(SmithyGoDependency.FMT);
                            writer.write(
                                    "return fmt.Errorf(\"presign URL duration must be 0 or greater, %v\", c.Expires)");
                        });
                        Symbol expiresAsHeaderMiddleware = SymbolUtils.createValueSymbolBuilder(
                                "AddExpiresOnPresignedURL",
                                AwsCustomGoDependency.S3_CUSTOMIZATION).build();
                        writer.writeDocs("add middleware to set expiration for s3 presigned url, "
                                + " if expiration is set to 0, this middleware sets a default expiration of 900 seconds");
                        writer.write("err = stack.Build.Add(&$T{ Expires: c.Expires, }, middleware.After)",
                                expiresAsHeaderMiddleware);
                        writer.write("if err != nil { return err }");
                    }
                    Symbol addAsPresignMiddlewareSymbol = SymbolUtils.createValueSymbolBuilder("AddAsIsPresigingMiddleware",
                            AwsCustomGoDependency.PRESIGNEDURL_CUSTOMIZATION).build();
                    writer.write("err = $T(stack)", addAsPresignMiddlewareSymbol);
                    writer.write("if err != nil { return err }");
                    writer.write("return nil");
                }).insertTrailingNewline();
    }
private void writeConvertToPresignMiddleware ( GoWriter writer , Model model , SymbolProvider symbolProvider , ServiceShape serviceShape ) { Symbol smithyStack = SymbolUtils . createPointableSymbolBuilder ( "Stack" , SmithyGoDependency . SMITHY_MIDDLEWARE ) . build ( ) ; writer . write ( "type $L $T" , CONVERT_TO_PRESIGN_TYPE_NAME , presignOptionsSymbol ) ; writer . openBlock ( "func (c $L) $L(stack $P, options Options) (err error) {" , "}" , CONVERT_TO_PRESIGN_TYPE_NAME , CONVERT_TO_PRESIGN_MIDDLEWARE_NAME , smithyStack , ( ) -> { Symbol smithyAfter = SymbolUtils . createValueSymbolBuilder ( "After" , SmithyGoDependency . SMITHY_MIDDLEWARE ) . build ( ) ; Symbol requestInvocationID = SymbolUtils . createPointableSymbolBuilder ( "ClientRequestID" , AwsGoDependency . AWS_MIDDLEWARE ) . build ( ) ; Symbol presignMiddleware = SymbolUtils . createValueSymbolBuilder ( "NewPresignHTTPRequestMiddleware" , AwsGoDependency . AWS_SIGNER_V4 ) . build ( ) ; writer . write ( "stack.Finalize.Clear()" ) ; writer . write ( "stack.Deserialize.Clear()" ) ; writer . write ( "stack.Build.Remove(($P)(nil).ID())" , requestInvocationID ) ; writer . write ( "stack.Build.Remove($S)" , "UserAgent" ) ; Symbol middlewareOptionsSymbol = SymbolUtils . createValueSymbolBuilder ( "PresignHTTPRequestMiddlewareOptions" , AwsGoDependency . AWS_SIGNER_V4 ) . build ( ) ; writer . openBlock ( "pmw := $T($T{" , "})" , presignMiddleware , middlewareOptionsSymbol , ( ) -> { writer . write ( "CredentialsProvider: options.$L," , AddAwsConfigFields . CREDENTIALS_CONFIG_NAME ) ; writer . write ( "Presigner: c.Presigner," ) ; writer . write ( "LogSigning: options.$L.IsSigning()," , AddAwsConfigFields . LOG_MODE_CONFIG_NAME ) ; } ) ; writer . write ( "err = stack.Finalize.Add(pmw, $T)" , smithyAfter ) ; writer . write ( "if err != nil { return err }" ) ; if ( serviceShape . hasTrait ( AwsQueryTrait . ID ) || serviceShape . hasTrait ( Ec2QueryTrait . ID ) ) { Symbol queryAsGetMiddleware = SymbolUtils . createValueSymbolBuilder ( "AddAsGetRequestMiddleware" , AwsGoDependency . AWS_QUERY_PROTOCOL ) . build ( ) ; writer . writeDocs ( "convert request to a GET request" ) ; writer . write ( "err = $T(stack)" , queryAsGetMiddleware ) ; writer . write ( "if err != nil { return err }" ) ; } if ( isS3ServiceShape ( model , serviceShape ) ) { writer . write ( "" ) ; writer . write ( "// add multi-region access point presigner" ) ; Symbol PresignConstructor = SymbolUtils . createValueSymbolBuilder ( "NewPresignHTTPRequestMiddleware" , AwsCustomGoDependency . S3_CUSTOMIZATION ) . build ( ) ; Symbol PresignOptions = SymbolUtils . createValueSymbolBuilder ( "PresignHTTPRequestMiddlewareOptions" , AwsCustomGoDependency . S3_CUSTOMIZATION ) . build ( ) ; Symbol RegisterPresigningMiddleware = SymbolUtils . createValueSymbolBuilder ( "RegisterPreSigningMiddleware" , AwsCustomGoDependency . S3_CUSTOMIZATION ) . build ( ) ; writer . openBlock ( "signermv := $T($T{" , "})" , PresignConstructor , PresignOptions , ( ) -> { writer . write ( "CredentialsProvider : options.Credentials," ) ; writer . write ( "V4Presigner : c.Presigner," ) ; writer . write ( "V4aPresigner : c.presignerV4a," ) ; writer . write ( "LogSigning : options.ClientLogMode.IsSigning()," ) ; } ) ; writer . write ( "err = $T(stack, signermv)" , RegisterPresigningMiddleware ) ; writer . write ( "if err != nil { return err }" ) ; writer . write ( "" ) ; writer . openBlock ( "if c.Expires < 0 {" , "}" , ( ) -> { writer . addUseImports ( SmithyGoDependency . FMT ) ; writer . write ( "return fmt.Errorf(\"presign URL duration must be 0 or greater, %v\", c.Expires)" ) ; } ) ; Symbol expiresAsHeaderMiddleware = SymbolUtils . createValueSymbolBuilder ( "AddExpiresOnPresignedURL" , AwsCustomGoDependency . S3_CUSTOMIZATION ) . build ( ) ; writer . writeDocs ( "add middleware to set expiration for s3 presigned url, " + " if expiration is set to 0, this middleware sets a default expiration of 900 seconds" ) ; writer . write ( "err = stack.Build.Add(&$T{ Expires: c.Expires, }, middleware.After)" , expiresAsHeaderMiddleware ) ; writer . write ( "if err != nil { return err }" ) ; } Symbol addAsPresignMiddlewareSymbol = SymbolUtils . createValueSymbolBuilder ( "AddAsIsPresigingMiddleware" , AwsCustomGoDependency . PRESIGNEDURL_CUSTOMIZATION ) . build ( ) ; writer . write ( "err = $T(stack)" , addAsPresignMiddlewareSymbol ) ; writer . write ( "if err != nil { return err }" ) ; writer . write ( "return nil" ) ; } ) . insertTrailingNewline ( ) ; }
generates a helper to mutate request middleware stack in favor of generating a presign URL request
 @param writer         the writer to write to
 @param model          the service model
 @param symbolProvider the symbol provider
 @param serviceShape   the service for which helper is generated
generates a helper to mutate request middleware stack in favor of generating a presign URL request @param writer the writer to write to @param model the service model @param symbolProvider the symbol provider @param serviceShape the service for which helper is generated
// Middleware to remove // Middleware to add // if protocol used is ec2query or query // presigned url should convert to Get request // s3 service needs expires and sets unsignedPayload if input is stream // ==== multi-region access point support // =======