-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
58 lines (53 loc) · 1.16 KB
/
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
package client
import (
"net/http"
"context"
"net"
"encoding/json"
"bytes"
"github.com/kataras/iris/core/errors"
"io"
"fmt"
)
type (
Cli struct {
Client *http.Client
}
)
func (c *Cli) Get(uri string) (io.ReadCloser, error) {
if res, err := c.Client.Get("http://unix" + uri); err != nil {
return nil, err
} else {
if res.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("%s %d", "response code is ", res.StatusCode))
}
return res.Body, nil
}
}
func (c *Cli) Post(uri string, req interface{}) (io.ReadCloser, error) {
if byteReq, err := json.Marshal(req); err != nil {
return nil, err
} else {
if res, err := c.Client.Post("http://unix"+uri,
"application/json;charset=utf-8",
bytes.NewBuffer(byteReq)); err != nil {
return nil, err
} else {
if res.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("%s %d", "response code is ", res.StatusCode))
}
return res.Body, nil
}
}
}
func NewClient(sock string) *Cli {
return &Cli{
&http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("unix", sock)
},
},
},
}
}