-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
61 lines (47 loc) · 1011 Bytes
/
client.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
package restc
import (
"net/http"
"time"
)
type Interface interface {
Verb(verb string) *Request
Post() *Request
Get() *Request
GetHeader() http.Header
SetHeader(header http.Header)
}
type Opt func(client *RESTClient) error
type RESTClient struct {
protocol string
addr string
port string
retryTimes int
retryDelay time.Duration
headers http.Header
// Set specific behavior of the client. If not set http.DefaultClient will be used.
client *http.Client
}
func (r *RESTClient) Verb(verb string) *Request {
return NewRequest(r).Verb(verb)
}
func (r *RESTClient) Post() *Request {
return r.Verb("POST")
}
func (r *RESTClient) Get() *Request {
return r.Verb("GET")
}
func (r *RESTClient) GetHeader() http.Header {
return r.headers
}
func (r *RESTClient) SetHeader(header http.Header) {
r.headers = header
}
func New(ops ...Opt) (*RESTClient, error) {
c := &RESTClient{}
for _, op := range ops {
if err := op(c); err != nil {
return nil, err
}
}
return c, nil
}