-
Notifications
You must be signed in to change notification settings - Fork 0
/
hive.go
73 lines (64 loc) · 1.3 KB
/
hive.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
package hive
import (
"errors"
"io"
"log"
"net/http"
"time"
)
var (
HTTP_GET = "GET"
HTTP_POST = "POST"
HTTP_PUT = "PUT"
HTTP_DELETE = "DELETE"
)
type Client struct {
BaseUrl string
Port string
User string
Timeout time.Duration
}
func New(baseurl, port, user string) *Client {
svc := &Client{
BaseUrl: baseurl,
Port: port,
User: user,
Timeout: 60 * time.Second,
}
return svc
}
func (this *Client) request(method, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, endpoint, body)
if err != nil {
log.Println(err)
return nil, err
}
req.Header.Add("Content-Type", "application/json")
cli := http.Client{Timeout: this.Timeout}
resp, err := cli.Do(req)
if err != nil {
log.Println(err)
return nil, err
}
if 400 < resp.StatusCode {
return nil, errors.New(resp.Status)
}
return resp, nil
}
func (this *Client) requestWithoutJSON(method, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, endpoint, body)
if err != nil {
log.Println(err)
return nil, err
}
cli := http.Client{Timeout: this.Timeout}
resp, err := cli.Do(req)
if err != nil {
log.Println(err)
return nil, err
}
if 400 < resp.StatusCode {
return nil, errors.New(resp.Status)
}
return resp, nil
}