...
1 package http
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7
8 smithy "github.com/aws/smithy-go"
9 "github.com/aws/smithy-go/middleware"
10 )
11
12
13 type ClientDo interface {
14 Do(*http.Request) (*http.Response, error)
15 }
16
17
18
19 type ClientDoFunc func(*http.Request) (*http.Response, error)
20
21
22 func (fn ClientDoFunc) Do(r *http.Request) (*http.Response, error) {
23 return fn(r)
24 }
25
26
27
28 type ClientHandler struct {
29 client ClientDo
30 }
31
32
33 func NewClientHandler(client ClientDo) ClientHandler {
34 return ClientHandler{
35 client: client,
36 }
37 }
38
39
40
41
42 func (c ClientHandler) Handle(ctx context.Context, input interface{}) (
43 out interface{}, metadata middleware.Metadata, err error,
44 ) {
45 req, ok := input.(*Request)
46 if !ok {
47 return nil, metadata, fmt.Errorf("expect Smithy http.Request value as input, got unsupported type %T", input)
48 }
49
50 builtRequest := req.Build(ctx)
51 if err := ValidateEndpointHost(builtRequest.Host); err != nil {
52 return nil, metadata, err
53 }
54
55 resp, err := c.client.Do(builtRequest)
56 if resp == nil {
57
58
59 resp = &http.Response{
60 Header: http.Header{},
61 Body: http.NoBody,
62 }
63 }
64 if err != nil {
65 err = &RequestSendError{Err: err}
66
67
68 select {
69 case <-ctx.Done():
70 err = &smithy.CanceledError{Err: ctx.Err()}
71 default:
72 }
73 }
74
75
76
77
78 if builtRequest.Body != nil {
79 _ = builtRequest.Body.Close()
80 }
81
82 return &Response{Response: resp}, metadata, err
83 }
84
85
86
87
88
89
90 type RequestSendError struct {
91 Err error
92 }
93
94
95
96 func (e *RequestSendError) ConnectionError() bool {
97 return true
98 }
99
100
101 func (e *RequestSendError) Unwrap() error {
102 return e.Err
103 }
104
105 func (e *RequestSendError) Error() string {
106 return fmt.Sprintf("request send failed, %v", e.Err)
107 }
108
109
110
111 type NopClient struct{}
112
113
114 func (NopClient) Do(r *http.Request) (*http.Response, error) {
115 return &http.Response{
116 StatusCode: 200,
117 Header: http.Header{},
118 Body: http.NoBody,
119 }, nil
120 }
121
View as plain text