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 hand written prototype for updated client API design. #530

Closed
wants to merge 4 commits into from
Closed
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
63 changes: 63 additions & 0 deletions aws/transport/http/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package http

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/awslabs/smithy-go/middleware"
smithyhttp "github.com/awslabs/smithy-go/transport/http"
)

// ResponseError provides the HTTP centric error type wrapping the underlying
// error with the HTTP response value and the deserialized RequestID.
type ResponseError struct {
*smithyhttp.ResponseError
RequestID string
}

// ServiceRequestID returns the HTTP response wrapping the underlying error.
func (e *ResponseError) ServiceRequestID() string { return e.RequestID }

func (e *ResponseError) Error() string {
return fmt.Sprintf(
"http response error StatusCode: %d, RequestID: %s, %v",
e.Response.StatusCode, e.RequestID, e.Err)
}

// As populates target and returns true if the type of target is a error type
// that the ResponseError embeds, (e.g. Smithy's HTTP ResponseError)
func (e *ResponseError) As(target interface{}) bool {
return errors.As(e.ResponseError, target)
}

// Client provides the interface for the minimum HTTP client behavior to
// invoke a HTTP request.
type Client interface {
Do(*http.Request) (*http.Response, error)
}

// ClientHandler provides a smithy middleware handler wrapper for an standard
// HTTP client.
type ClientHandler struct {
Client Client
}

// Handle invokes the HTTP client with the provided HTTP request. Returns the
// response, or error if the request failed.
func (h ClientHandler) Handle(ctx context.Context, input interface{}) (
out interface{}, metadata middleware.Metadata, err error,
) {
sReq, ok := input.(*smithyhttp.Request)
if !ok {
return nil, metadata, fmt.Errorf(
"invalid input type for HTTP client handlers, expect %T, got %T",
input, (*smithyhttp.Request)(nil))
}

hReq := sReq.Build(ctx)

resp, err := h.Client.Do(hReq)
return resp, metadata, err
}
157 changes: 98 additions & 59 deletions service/smithyprototype/lexruntimeservice/api_client.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package lexruntimeservice_test

import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"
lexruntime "github.com/aws/aws-sdk-go-v2/service/smithyprototype/lexruntimeservice"
)

func ExampleNew() {
client := lexruntime.New(lexruntime.Options{
RegionID: "us-west-2",
Credentials: customCredProvider,
})
res, err := client.GetSession(context.TODO(), &lexruntime.GetSessionInput{
BotAlias: aws.String("botAlias"),
BotName: aws.String("botName"),
UserId: aws.String("userID"),
})
if err != nil {
log.Fatalf("failed to get session, %v", err)
}

fmt.Println("session:", res.SessionId)
}

func ExampleNewFromConfig_customOptions() {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
log.Fatalf("failed to load config, %v", err)
}

client := lexruntime.NewFromConfig(cfg, func(o *lexruntime.Options) {
o.RegionID = "us-west-2"
})
res, err := client.GetSession(context.TODO(), &lexruntime.GetSessionInput{
BotAlias: aws.String("botAlias"),
BotName: aws.String("botName"),
UserId: aws.String("userID"),
})
if err != nil {
log.Fatalf("failed to get session, %v", err)
}

fmt.Println("session:", res.SessionId)
}

var external = mockExternal{}

type mockExternal struct {
}

func (mockExternal) LoadDefaultAWSConfig() (aws.Config, error) {
return aws.Config{}, nil
}

var customCredProvider = unit.Config().Credentials
Loading