-
Notifications
You must be signed in to change notification settings - Fork 9
/
requests.go
104 lines (69 loc) · 2.49 KB
/
requests.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
100
101
102
103
104
package bitclient
import (
"fmt"
"net/http"
)
const BASE_URI = "/rest/api/1.0"
type PagedRequest struct {
Limit uint `url:"limit,omitempty"`
Start uint `url:"start,omitempty"`
}
type RequestError struct {
Code int
Message string
}
type ErrorResponse struct {
Errors []Error
}
type PagedResponse struct {
Size uint `json:"size"`
Limit uint `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Start uint `json:"start"`
}
func (r RequestError) Error() string {
return r.Message
}
func (bc *BitClient) checkReponse(resp *http.Response, errorResponse *ErrorResponse) (*http.Response, error) {
if resp != nil && resp.StatusCode > 299 {
message := fmt.Sprintf("%s - %s\n", resp.Status, resp.Request.URL.String())
for _, e := range errorResponse.Errors {
message += e.Context + ": " + e.Message + "\n"
}
return nil, RequestError{
Code: resp.StatusCode,
Message: message,
}
}
return resp, nil
}
func (bc *BitClient) DoGet(uri string, params interface{}, rData interface{}) (*http.Response, error) {
rError := new(ErrorResponse)
resp, _ := bc.sling.New().Get(BASE_URI+uri).QueryStruct(params).Receive(rData, rError)
return bc.checkReponse(resp, rError)
}
func (bc *BitClient) DoPostUrl(uri string, params interface{}, rData interface{}) (*http.Response, error) {
rError := new(ErrorResponse)
resp, _ := bc.sling.New().Post(BASE_URI+uri).QueryStruct(params).Receive(rData, rError)
return bc.checkReponse(resp, rError)
}
func (bc *BitClient) DoPost(uri string, data interface{}, rData interface{}) (*http.Response, error) {
rError := new(ErrorResponse)
resp, _ := bc.sling.New().Post(BASE_URI+uri).BodyJSON(data).Receive(rData, rError)
return bc.checkReponse(resp, rError)
}
func (bc *BitClient) DoPut(uri string, data interface{}, rData interface{}) (*http.Response, error) {
rError := new(ErrorResponse)
resp, _ := bc.sling.New().Put(BASE_URI+uri).BodyJSON(data).Receive(rData, rError)
return bc.checkReponse(resp, rError)
}
func (bc *BitClient) DoPutUrl(uri string, data interface{}, rData interface{}) (*http.Response, error) {
rError := new(ErrorResponse)
resp, _ := bc.sling.New().Put(BASE_URI+uri).QueryStruct(data).Receive(rData, rError)
return bc.checkReponse(resp, rError)
}
func (bc *BitClient) DoDeleteUrl(uri string, params interface{}, rData interface{}) (*http.Response, error) {
rError := new(ErrorResponse)
resp, _ := bc.sling.New().Delete(BASE_URI+uri).QueryStruct(params).Receive(rData, rError)
return bc.checkReponse(resp, rError)
}