Skip to content

Commit

Permalink
Add HTTP client middleware handler wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jasdel committed Jun 3, 2020
1 parent 06bc5ae commit b83805c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions transport/http/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package http

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

"github.com/awslabs/smithy-go/middleware"
)

// ClientDo provides the interface for custom HTTP client implementations.
type ClientDo interface {
Do(*http.Request) (*http.Response, error)
}

// ClientDoFunc provides a helper to wrap an function as an HTTP client for
// round tripping requests.
type ClientDoFunc func(*http.Request) (*http.Response, error)

// Do will invoke the underlying func, returning the result.
func (fn ClientDoFunc) Do(r *http.Request) (*http.Response, error) {
return fn(r)
}

// ClientHandler wraps a client that implements the HTTP Do method. Standard
// implementation is http.Client.
type ClientHandler struct {
client ClientDo
}

// NewClientHandler returns an initialized middleware handler for the client.
func NewClientHandler(client ClientDo) ClientHandler {
return ClientHandler{
client: client,
}
}

// Handle implements the middleware Handler interface, that will invoke the
// underlying HTTP client. Requires the input to be an Smithy *Request. Returns
// a smithy *Response, or error if the request failed.
func (c ClientHandler) Handle(ctx context.Context, input interface{}) (
out interface{}, metadata middleware.Metadata, err error,
) {
req, ok := input.(*Request)
if !ok {
return nil, metadata, fmt.Errorf("expect Smithy http.Request value as input, got unsupported type %T", input)
}

resp, err := c.client.Do(req.Build(ctx))
if err != nil {
return nil, metadata, err
}

return &Response{Response: resp}, metadata, nil
}

0 comments on commit b83805c

Please sign in to comment.