-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
default.go
99 lines (77 loc) · 2.22 KB
/
default.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package httpdriver
import (
"context"
"io"
"net/http"
"net/url"
"time"
)
// DefaultClient implements Client and wraps around the stdlib Client.
type DefaultClient http.Client
var _ Client = (*DefaultClient)(nil)
// WrapClient wraps around the standard library's http.Client and returns an
// implementation that's compatible with the Client driver interface.
func WrapClient(client http.Client) Client {
return DefaultClient(client)
}
// NewClient creates a new client around the standard library's http.Client. The
// client will have a timeout of 10 seconds.
func NewClient() Client {
return WrapClient(http.Client{
Timeout: 10 * time.Second,
})
}
func (d DefaultClient) NewRequest(ctx context.Context, method, url string) (Request, error) {
r, err := http.NewRequestWithContext(ctx, method, url, nil)
if err != nil {
return nil, err
}
return (*DefaultRequest)(r), nil
}
func (d DefaultClient) Do(req Request) (Response, error) {
// Implementations can safely assert this.
request := req.(*DefaultRequest)
r, err := (*http.Client)(&d).Do((*http.Request)(request))
if err != nil {
return nil, err
}
return (*DefaultResponse)(r), nil
}
// DefaultRequest wraps around the stdlib Request and satisfies the Request
// interface.
type DefaultRequest http.Request
var _ Request = (*DefaultRequest)(nil)
func (r *DefaultRequest) GetPath() string {
return r.URL.Path
}
func (r *DefaultRequest) GetContext() context.Context {
return (*http.Request)(r).Context()
}
func (r *DefaultRequest) AddQuery(values url.Values) {
var qs = r.URL.Query()
for k, v := range values {
qs[k] = append(qs[k], v...)
}
r.URL.RawQuery = qs.Encode()
}
func (r *DefaultRequest) AddHeader(header http.Header) {
for key, values := range header {
r.Header[key] = append(r.Header[key], values...)
}
}
func (r *DefaultRequest) WithBody(body io.ReadCloser) {
r.Body = body
}
// DefaultResponse wraps around the stdlib Response and satisfies the Response
// interface.
type DefaultResponse http.Response
var _ Response = (*DefaultResponse)(nil)
func (r *DefaultResponse) GetStatus() int {
return r.StatusCode
}
func (r *DefaultResponse) GetHeader() http.Header {
return r.Header
}
func (r *DefaultResponse) GetBody() io.ReadCloser {
return r.Body
}