Skip to content

Commit

Permalink
adds prototyped apigateway GetApiKeyRequest that uses rest encoder ut…
Browse files Browse the repository at this point in the history
…ility
  • Loading branch information
skotambkar committed Dec 6, 2019
1 parent 1e5d3b4 commit 44d014c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 92 deletions.
35 changes: 22 additions & 13 deletions service/apigateway/internal/awsrestjson/marshal.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
package awsrestjson

import (
"log"

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

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

func (m ProtoCreateApiKeyMarshaler) MarshalOperation(r *aws.Request) {
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 = MarshalCreateApiKeyInputShapeAWSREST(m.Input, r)
err = MarshalGetApiKeyInputShapeAWSREST(m.Input, encoder)
if err != nil {
r.Error = err
}

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

}

func MarshalCreateApiKeyInputShapeAWSREST(v *types.CreateApiKeyInput, r *aws.Request) error {
// delegate to reflection based marshaling
rest.Build(r)
func MarshalGetApiKeyInputShapeAWSREST(input *types.GetApiKeyInput, encoder *v2.Encoder) error {
// encode using rest encoder utility
if err := encoder.AddURI("api_Key").String(*input.ApiKey); err != nil {
log.Fatalf("Failed to marshal API KEY to URI using REST encoder:\n \t %v", err.Error())
}
encoder.SetQuery("includeValue").Boolean(*input.IncludeValue)
encoder.Encode()
return nil
}
func MarshalCreateApiKeyInputShapeAWSJSON(v *types.CreateApiKeyInput, r *aws.Request) error {
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 @@ -41,8 +50,8 @@ func MarshalCreateApiKeyInputShapeAWSJSON(v *types.CreateApiKeyInput, r *aws.Req
}

// GetNamedBuildHandler returns a Named Build Handler for an operation marshal function
func (m ProtoCreateApiKeyMarshaler) GetNamedBuildHandler() aws.NamedHandler {
const BuildHandler = "CreateApiKey.BuildHandler"
func (m ProtoGetApiKeyMarshaler) GetNamedBuildHandler() aws.NamedHandler {
const BuildHandler = "ProtoGetApiKey.BuildHandler"
return aws.NamedHandler{
Name: BuildHandler,
Fn: m.MarshalOperation,
Expand Down
39 changes: 39 additions & 0 deletions service/apigateway/protoGetAPIKeyRequest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package apigateway_test

import (
"context"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/internal/awstesting/mock"
"github.com/aws/aws-sdk-go-v2/service/apigateway"
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
"github.com/google/go-cmp/cmp"
)

func TestProtoGetApiKeyRequest_Diff(t *testing.T) {
svc := apigateway.New(mock.Config())

input := types.GetApiKeyInput{
ApiKey: aws.String("mock key"),
IncludeValue: aws.Bool(true),
}

request := svc.GetApiKeyRequest(&input)
_, err := request.Send(context.TODO())
if err != nil {
fmt.Println(err)
}

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

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

if diff := cmp.Diff(request.HTTPRequest.Header, prototypeRequest.HTTPRequest.Header); diff != "" {
t.Errorf("Found diff: %v", diff)
}
}
79 changes: 0 additions & 79 deletions service/apigateway/proto_api_op_CreateApiKey.go

This file was deleted.

78 changes: 78 additions & 0 deletions service/apigateway/proto_api_op_GetApiKey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package apigateway

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/private/protocol/restjson"
"github.com/aws/aws-sdk-go-v2/service/apigateway/internal/awsrestjson"
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
)

const protoOpGetApiKey = "GetApiKey"

// 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)
// resp, err := req.Send(context.TODO())
// if err == nil {
// fmt.Println(resp)
// }
func (c *Client) ProtoGetApiKeyRequest(input *types.GetApiKeyInput) ProtoGetApiKeyRequest {
op := &aws.Operation{
Name: protoOpGetApiKey,
HTTPMethod: "GET",
HTTPPath: "/apikeys/{api_Key}",
}

if input == nil {
input = &types.GetApiKeyInput{}
}

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}
}

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

// Send marshals and sends the GetApiKey API request.
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{
GetApiKeyOutput: r.Request.Data.(*types.GetApiKeyOutput),
response: &aws.Response{Request: r.Request},
}

return resp, nil
}

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

response *aws.Response
}

// SDKResponseMetdata returns the response metadata for the
// GetApiKey request.
func (r *ProtoGetApiKeyResponse) SDKResponseMetdata() *aws.Response {
return r.response
}

0 comments on commit 44d014c

Please sign in to comment.