Skip to content

Commit

Permalink
adds prototyped s3 PutObject Request that uses rest encoder utility
Browse files Browse the repository at this point in the history
  • Loading branch information
skotambkar committed Dec 6, 2019
1 parent fca5472 commit 77999c9
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 29 deletions.
22 changes: 14 additions & 8 deletions service/apigateway/internal/awsrestjson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@ import (
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
)

// ProtoGetApiKeyMarshaler defines marshaler for ProtoGetApiKey operation
type ProtoGetApiKeyMarshaler struct {
// ProtoGetAPIKeyMarshaler defines marshaler for ProtoGetApiKey operation
type ProtoGetAPIKeyMarshaler struct {
Input *types.GetApiKeyInput
}

func (m ProtoGetApiKeyMarshaler) MarshalOperation(r *aws.Request) {
// MarshalOperation is the top level method used within a handler stack to marshal an operation
// This method calls appropriate marshal shape functions as per the input shape and protocol used by the service.
func (m ProtoGetAPIKeyMarshaler) MarshalOperation(r *aws.Request) {
var err error
encoder := v2.NewEncoder(r.HTTPRequest)
// adds content-type header
encoder.AddHeader("Content-Type").String("application/json")

err = MarshalGetApiKeyInputShapeAWSREST(m.Input, encoder)
err = MarshalGetAPIKeyInputShapeAWSREST(m.Input, encoder)
if err != nil {
r.Error = err
}

err = MarshalGetApiKeyInputShapeAWSJSON(m.Input, r)
err = MarshalGetAPIKeyInputShapeAWSJSON(m.Input, r)
if err != nil {
r.Error = err
}
}

func MarshalGetApiKeyInputShapeAWSREST(input *types.GetApiKeyInput, encoder *v2.Encoder) error {
// MarshalGetAPIKeyInputShapeAWSREST is a stand alone function used to marshal the HTTP bindings a input shape.
// This method uses the rest encoder utility
func MarshalGetAPIKeyInputShapeAWSREST(input *types.GetApiKeyInput, encoder *v2.Encoder) error {
// encode using rest encoder utility
if err := encoder.SetURI("api_Key").String(*input.ApiKey); err != nil {
log.Fatalf("Failed to marshal API KEY to URI using REST encoder:\n \t %v", err.Error())
Expand All @@ -41,7 +45,9 @@ func MarshalGetApiKeyInputShapeAWSREST(input *types.GetApiKeyInput, encoder *v2.
encoder.Encode()
return nil
}
func MarshalGetApiKeyInputShapeAWSJSON(v *types.GetApiKeyInput, r *aws.Request) error {

// MarshalGetAPIKeyInputShapeAWSJSON is a stand alone function used to marshal the json body
func MarshalGetAPIKeyInputShapeAWSJSON(v *types.GetApiKeyInput, r *aws.Request) error {
// delegate to reflection based marshaling
if t := rest.PayloadType(r.Params); t == "structure" || t == "" {
jsonrpc.Build(r)
Expand All @@ -50,7 +56,7 @@ func MarshalGetApiKeyInputShapeAWSJSON(v *types.GetApiKeyInput, r *aws.Request)
}

// GetNamedBuildHandler returns a Named Build Handler for an operation marshal function
func (m ProtoGetApiKeyMarshaler) GetNamedBuildHandler() aws.NamedHandler {
func (m ProtoGetAPIKeyMarshaler) GetNamedBuildHandler() aws.NamedHandler {
const BuildHandler = "ProtoGetApiKey.BuildHandler"
return aws.NamedHandler{
Name: BuildHandler,
Expand Down
8 changes: 3 additions & 5 deletions service/apigateway/protoGetAPIKeyRequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package apigateway_test

import (
"context"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -23,14 +22,13 @@ func TestProtoGetApiKeyRequest_Diff(t *testing.T) {
request := svc.GetApiKeyRequest(&input)
_, err := request.Send(context.TODO())
if err != nil {
fmt.Println(err)
t.Error(err)
}

prototypeRequest := svc.ProtoGetApiKeyRequest(&input)
prototypeRequest := svc.ProtoGetAPIKeyRequest(&input)
_, err = prototypeRequest.Send(context.TODO())

if err != nil {
fmt.Println(err)
t.Error(err)
}

if diff := cmp.Diff(request.HTTPRequest.Header, prototypeRequest.HTTPRequest.Header); diff != "" {
Expand Down
32 changes: 16 additions & 16 deletions service/apigateway/proto_api_op_GetApiKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
)

const protoOpGetApiKey = "GetApiKey"
const protoOpGetAPIKey = "GetApiKey"

// ProtoGetApiKeyRequest returns a request value for making API operation for
// ProtoGetAPIKeyRequest returns a request value for making API operation for
// Amazon API Gateway.
//
// Gets information about the current ApiKey resource.
//
// // Example sending a request using ProtoGetApiKeyRequest.
// req := client.ProtoGetApiKeyRequest(params)
// // Example sending a request using ProtoGetAPIKeyRequest.
// req := client.ProtoGetAPIKeyRequest(params)
// resp, err := req.Send(context.TODO())
// if err == nil {
// fmt.Println(resp)
// }
func (c *Client) ProtoGetApiKeyRequest(input *types.GetApiKeyInput) ProtoGetApiKeyRequest {
func (c *Client) ProtoGetAPIKeyRequest(input *types.GetApiKeyInput) ProtoGetAPIKeyRequest {
op := &aws.Operation{
Name: protoOpGetApiKey,
Name: protoOpGetAPIKey,
HTTPMethod: "GET",
HTTPPath: "/apikeys/{api_Key}",
}
Expand All @@ -35,44 +35,44 @@ func (c *Client) ProtoGetApiKeyRequest(input *types.GetApiKeyInput) ProtoGetApiK

req := c.newRequest(op, input, &types.GetApiKeyOutput{})
// swap existing build handler on svc, with a new named build handler
req.Handlers.Build.Swap(restjson.BuildHandler.Name, awsrestjson.ProtoGetApiKeyMarshaler{Input: input}.GetNamedBuildHandler())
return ProtoGetApiKeyRequest{Request: req, Input: input, Copy: c.ProtoGetApiKeyRequest}
req.Handlers.Build.Swap(restjson.BuildHandler.Name, awsrestjson.ProtoGetAPIKeyMarshaler{Input: input}.GetNamedBuildHandler())
return ProtoGetAPIKeyRequest{Request: req, Input: input, Copy: c.ProtoGetAPIKeyRequest}
}

// ProtoGetApiKeyRequest is the request type for the
// ProtoGetAPIKeyRequest is the request type for the
// GetApiKey API operation.
type ProtoGetApiKeyRequest struct {
type ProtoGetAPIKeyRequest struct {
*aws.Request
Input *types.GetApiKeyInput
Copy func(*types.GetApiKeyInput) ProtoGetApiKeyRequest
Copy func(*types.GetApiKeyInput) ProtoGetAPIKeyRequest
}

// Send marshals and sends the GetApiKey API request.
func (r ProtoGetApiKeyRequest) Send(ctx context.Context) (*ProtoGetApiKeyResponse, error) {
func (r ProtoGetAPIKeyRequest) Send(ctx context.Context) (*ProtoGetAPIKeyResponse, error) {
r.Request.SetContext(ctx)
err := r.Request.Send()
if err != nil {
return nil, err
}

resp := &ProtoGetApiKeyResponse{
resp := &ProtoGetAPIKeyResponse{
GetApiKeyOutput: r.Request.Data.(*types.GetApiKeyOutput),
response: &aws.Response{Request: r.Request},
}

return resp, nil
}

// ProtoGetApiKeyResponse is the response type for the
// ProtoGetAPIKeyResponse is the response type for the
// GetApiKey API operation.
type ProtoGetApiKeyResponse struct {
type ProtoGetAPIKeyResponse struct {
*types.GetApiKeyOutput

response *aws.Response
}

// SDKResponseMetdata returns the response metadata for the
// GetApiKey request.
func (r *ProtoGetApiKeyResponse) SDKResponseMetdata() *aws.Response {
func (r *ProtoGetAPIKeyResponse) SDKResponseMetdata() *aws.Response {
return r.response
}
105 changes: 105 additions & 0 deletions service/s3/internal/awsrestxml/marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package awsrestxml

import (
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/private/protocol"
v2 "github.com/aws/aws-sdk-go-v2/private/protocol/rest/v2"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)

// ProtoPutObjectMarshaler defines marshaler for ProtoPutObject operation
type ProtoPutObjectMarshaler struct {
Input *types.PutObjectInput
}

// MarshalOperation is the top level method used within a handler stack to marshal an operation
// This method calls appropriate marshal shape functions as per the input shape and protocol used by the service.
func (m ProtoPutObjectMarshaler) MarshalOperation(r *aws.Request) {
var err error
encoder := v2.NewEncoder(r.HTTPRequest)

err = MarshalPutObjectInputShapeAWSREST(m.Input, encoder)
if err != nil {
r.Error = err
}

// since body payloadtype here is blob
if err = MarshalPutObjectInputShapeAWSXML(r, m.Input); err != nil {
r.Error = err
}
}

// MarshalPutObjectInputShapeAWSREST is a stand alone function used to marshal the HTTP bindings a input shape.
// This method uses the rest encoder utility
func MarshalPutObjectInputShapeAWSREST(input *types.PutObjectInput, encoder *v2.Encoder) error {
// Encoding shapes with location `headers`
marshalShapeMapForHeaders(encoder, "x-amz-meta-", input.Metadata)
// Encoding shapes with location `header`
encoder.AddHeader("Cache-Control").String(*input.CacheControl)
encoder.AddHeader("Content-Disposition").String(*input.ContentDisposition)
encoder.AddHeader("Content-Language").String(*input.ContentLanguage)
encoder.AddHeader("Content-Length").Integer(*input.ContentLength)
encoder.AddHeader("Content-Md5").String(*input.ContentMD5)
encoder.AddHeader("Content-Type").String(*input.ContentType)
encoder.AddHeader("x-amz-acl").String(string(input.ACL))
encoder.AddHeader("x-amz-grant-full-control").String(*input.GrantFullControl)
encoder.AddHeader("x-amz-grant-read").String(*input.GrantRead)
encoder.AddHeader("x-amz-grant-read-acp").String(*input.GrantReadACP)
encoder.AddHeader("x-amz-grant-write-acp").String(*input.GrantWriteACP)
encoder.AddHeader("x-amz-object-lock-legal-hold").String(string(input.ObjectLockLegalHoldStatus))
encoder.AddHeader("x-amz-object-lock-mode").String(string(input.ObjectLockMode))
encoder.AddHeader("x-amz-tagging").String(*input.Tagging)
encoder.AddHeader("x-amz-request-payer").String(string(input.RequestPayer))
encoder.AddHeader("x-amz-server-side-encryption-context").String(*input.SSEKMSEncryptionContext)
encoder.AddHeader("x-amz-server-side-encryption-aws-kms-key-id").String(*input.SSEKMSKeyId)
encoder.AddHeader("x-amz-server-side-encryption-customer-key-MD5").String(*input.SSECustomerKeyMD5)
encoder.AddHeader("x-amz-server-side-encryption-customer-algorithm").String(*input.SSECustomerAlgorithm)
encoder.AddHeader("x-amz-website-redirect-location").String(*input.WebsiteRedirectLocation)
encoder.AddHeader("x-amz-storage-class").String(string(input.StorageClass))
encoder.AddHeader("x-amz-server-side-encryption").String(string(input.ServerSideEncryption))
if err := encoder.AddHeader("Expires").Time(*input.Expires, protocol.RFC822TimeFormatName); err != nil {
log.Fatalf("Failed to encode header for shape Expires: \n \t %v", err)
}
if err := encoder.AddHeader("x-amz-object-lock-retain-until-date").Time(*input.ObjectLockRetainUntilDate, protocol.ISO8601TimeFormatName); err != nil {
log.Fatalf("Failed to encode header for shape Expires: \n \t %v", err)
}
// Encoding shapes with location `uri`
if err := encoder.SetURI("Bucket").String(*input.Bucket); err != nil {
log.Fatalf("Failed to encode URI, \n\t %v\n", err)
}
if err := encoder.SetURI("Key").String(*input.Key); err != nil {
log.Fatalf("Failed to encode URI, \n\t %v\n", err)
}
// encode using rest encoder utility
encoder.Encode()
return nil
}

// MarshalPutObjectInputShapeAWSXML is a stand alone function used to marshal the xml payload
// This should be generated according to the payload type for rest-xml protocol
func MarshalPutObjectInputShapeAWSXML(r *aws.Request, input *types.PutObjectInput) error {
if input.Body != nil {
r.SetReaderBody(input.Body)
}
return nil
}

// marshalShapeMapForHeaders is marshal function that takes in a map[string]string as an input along with an encoder
// and location Name which should be used to marshal the shape with location headers.
func marshalShapeMapForHeaders(encoder *v2.Encoder, locationName string, input map[string]string) {
headerObject := encoder.Headers(locationName)
for k, v := range input {
headerObject.AddHeader(k).String(v)
}
}

// GetNamedBuildHandler returns a Named Build Handler for an operation marshal function
func (m ProtoPutObjectMarshaler) GetNamedBuildHandler() aws.NamedHandler {
const BuildHandler = "ProtoPutBucket.BuildHandler"
return aws.NamedHandler{
Name: BuildHandler,
Fn: m.MarshalOperation,
}
}
Loading

0 comments on commit 77999c9

Please sign in to comment.