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

Add polly SynthesizeSpeech presigner #2189

Merged
merged 2 commits into from
Aug 19, 2023
Merged
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
9 changes: 9 additions & 0 deletions .changelog/419f3a1b33d841779c9656a7d69c63e6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "419f3a1b-33d8-4177-9c96-56a7d69c63e6",
"type": "feature",
"description": "Add support for polly SynthesizeSpeech GET request presigner",
"modules": [
".",
"service/polly"
]
}
13 changes: 13 additions & 0 deletions aws/protocol/query/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func (o *Object) Key(name string) Value {
return o.key(name, false)
}

// KeyWithValues adds the given named key to the Query object.
// Returns a Value encoder that should be used to encode a Query list of values.
func (o *Object) KeyWithValues(name string) Value {
return o.keyWithValues(name, false)
}

// FlatKey adds the given named key to the Query object.
// Returns a Value encoder that should be used to encode a Query value type. The
// value will be flattened if it is a map or array.
Expand All @@ -54,3 +60,10 @@ func (o *Object) key(name string, flatValue bool) Value {
}
return newValue(o.values, name, flatValue)
}

func (o *Object) keyWithValues(name string, flatValue bool) Value {
if o.prefix != "" {
return newAppendValue(o.values, fmt.Sprintf("%s.%s", o.prefix, name), flatValue)
}
return newAppendValue(o.values, name, flatValue)
}
9 changes: 9 additions & 0 deletions aws/protocol/query/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func newValue(values url.Values, key string, flat bool) Value {
}
}

func newAppendValue(values url.Values, key string, flat bool) Value {
return Value{
values: values,
key: key,
flat: flat,
queryValue: httpbinding.NewQueryValue(values, key, true),
}
}

func newBaseValue(values url.Values) Value {
return Value{
values: values,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public class AwsHttpPresignURLClientGenerator implements GoIntegration {
ShapeId.from("com.amazonaws.sts#AWSSecurityTokenServiceV20110615"), SetUtils.of(
ShapeId.from("com.amazonaws.sts#GetCallerIdentity"),
ShapeId.from("com.amazonaws.sts#AssumeRole")
),
ShapeId.from("com.amazonaws.polly#Parrot_v1"), SetUtils.of(
ShapeId.from("com.amazonaws.polly#SynthesizeSpeech")
)
);

Expand Down Expand Up @@ -381,8 +384,8 @@ private void writeConvertToPresignMiddleware(
writer.write("return err");
});

// if protocol used is ec2query or query
if (serviceShape.hasTrait(AwsQueryTrait.ID) || serviceShape.hasTrait(Ec2QueryTrait.ID)) {
// if protocol used is ec2query or query or if service is polly
if (serviceShape.hasTrait(AwsQueryTrait.ID) || serviceShape.hasTrait(Ec2QueryTrait.ID) || isPollyServiceShape(serviceShape)) {
// presigned url should convert to Get request
Symbol queryAsGetMiddleware = SymbolUtils.createValueSymbolBuilder("AddAsGetRequestMiddleware",
AwsGoDependency.AWS_QUERY_PROTOCOL).build();
Expand All @@ -392,6 +395,15 @@ private void writeConvertToPresignMiddleware(
writer.write("if err != nil { return err }");
}

// polly presigner needs to serialize input param into query string
if (isPollyServiceShape(serviceShape)) {
Symbol serializeInputMiddleware = SymbolUtils.createValueSymbolBuilder("AddPresignSynthesizeSpeechMiddleware",
AwsGoDependency.AWS_QUERY_PROTOCOL).build();
writer.writeDocs("use query encoder to encode GET request query string");
writer.write("err = AddPresignSynthesizeSpeechMiddleware(stack)");
writer.write("if err != nil { return err }");
}

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

Expand Down Expand Up @@ -682,5 +694,9 @@ private final boolean isS3ServiceShape(Model model, ServiceShape service) {
String serviceId = service.expectTrait(ServiceTrait.class).getSdkId();
return serviceId.equalsIgnoreCase("S3");
}

private final boolean isPollyServiceShape(ServiceShape service) {
return service.expectTrait(ServiceTrait.class).getSdkId().equalsIgnoreCase("Polly");
}
}

108 changes: 108 additions & 0 deletions service/polly/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions service/polly/api_op_SynthesizeSpeech.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions service/polly/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"github.com/aws/aws-sdk-go-v2": "v1.4.0",
"github.com/aws/aws-sdk-go-v2/internal/configsources": "v0.0.0-00010101000000-000000000000",
"github.com/aws/aws-sdk-go-v2/internal/endpoints/v2": "v2.0.0-00010101000000-000000000000",
"github.com/aws/aws-sdk-go-v2/service/internal/presigned-url": "v1.0.7",
"github.com/aws/smithy-go": "v1.4.0",
"github.com/google/go-cmp": "v0.5.4"
},
Expand Down
3 changes: 3 additions & 0 deletions service/polly/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/aws/aws-sdk-go-v2 v1.20.3
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.40
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.34
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.34
github.com/aws/smithy-go v1.14.2
github.com/google/go-cmp v0.5.8
)
Expand All @@ -15,3 +16,5 @@ replace github.com/aws/aws-sdk-go-v2 => ../../
replace github.com/aws/aws-sdk-go-v2/internal/configsources => ../../internal/configsources/

replace github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 => ../../internal/endpoints/v2/

replace github.com/aws/aws-sdk-go-v2/service/internal/presigned-url => ../../service/internal/presigned-url/
100 changes: 100 additions & 0 deletions service/polly/presign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package polly

import (
"bytes"
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws/protocol/query"
"github.com/aws/smithy-go"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)

// AddPresignSynthesizeSpeechMiddleware adds presignOpSynthesizeSpeechInput into middleware stack to
// parse SynthesizeSpeechInput into request stream
func AddPresignSynthesizeSpeechMiddleware(stack *middleware.Stack) error {
return stack.Serialize.Insert(&presignOpSynthesizeSpeechInput{}, "Query:AsGetRequest", middleware.Before)
}

// presignOpSynthesizeSpeechInput encodes SynthesizeSpeechInput into url format
// query string and put that into request stream for later presign-url build
type presignOpSynthesizeSpeechInput struct {
}

func (*presignOpSynthesizeSpeechInput) ID() string {
return "PresignSerializer"
}

func (m *presignOpSynthesizeSpeechInput) HandleSerialize(ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler) (
out middleware.SerializeOutput, metadata middleware.Metadata, err error,
) {
request, ok := in.Request.(*smithyhttp.Request)
if !ok {
return out, metadata, &smithy.SerializationError{Err: fmt.Errorf("unknown transport type %T", in.Request)}
}

input, ok := in.Parameters.(*SynthesizeSpeechInput)
_ = input
if !ok {
return out, metadata, &smithy.SerializationError{Err: fmt.Errorf("unknown input parameters type %T", in.Parameters)}
}

bodyWriter := bytes.NewBuffer(nil)
bodyEncoder := query.NewEncoder(bodyWriter)

if err := presignSerializeOpDocumentSynthesizeSpeechInput(input, bodyEncoder.Value); err != nil {
return out, metadata, &smithy.SerializationError{Err: err}
}

err = bodyEncoder.Encode()
if err != nil {
return out, metadata, &smithy.SerializationError{Err: err}
}

if request, err = request.SetStream(bytes.NewReader(bodyWriter.Bytes())); err != nil {
return out, metadata, &smithy.SerializationError{Err: err}
}

in.Request = request

return next.HandleSerialize(ctx, in)
}

func presignSerializeOpDocumentSynthesizeSpeechInput(v *SynthesizeSpeechInput, value query.Value) error {
object := value.Object()
_ = object

if v.LexiconNames != nil && len(v.LexiconNames) > 0 {
objectKey := object.KeyWithValues("LexiconNames")
for _, name := range v.LexiconNames {
objectKey.String(name)
}
}

if len(v.OutputFormat) > 0 {
objectKey := object.Key("OutputFormat")
objectKey.String(string(v.OutputFormat))
}

if v.SampleRate != nil {
objectKey := object.Key("SampleRate")
objectKey.String(*v.SampleRate)
}

if v.Text != nil {
objectKey := object.Key("Text")
objectKey.String(*v.Text)
}

if len(v.TextType) > 0 {
objectKey := object.Key("TextType")
objectKey.String(string(v.TextType))
}

if len(v.VoiceId) > 0 {
objectKey := object.Key("VoiceId")
objectKey.String(string(v.VoiceId))
}

return nil
}
Loading
Loading