-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_util.go
More file actions
110 lines (100 loc) · 2.52 KB
/
client_util.go
File metadata and controls
110 lines (100 loc) · 2.52 KB
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
105
106
107
108
109
110
package sonarapi
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
)
// Do sends an API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred. If v implements the io.Writer
// interface, the raw response body will be written to v, without attempting to
// first decode it.
func Do(c *http.Client, req *http.Request, v interface{}) (*http.Response, error) {
isText := false
if _, ok := v.(*string); ok {
req.Header.Set("Accept", "text/plain")
isText = true
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
err = CheckResponse(resp)
if err != nil {
return resp, err
}
if v != nil {
defer resp.Body.Close()
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, resp.Body)
} else {
if isText {
byts, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp, err
}
w := v.(*string)
*w = string(byts)
} else {
decoder := json.NewDecoder(resp.Body)
decoder.DisallowUnknownFields()
err = decoder.Decode(v)
}
}
}
return resp, err
}
type ErrorResponse struct {
Body []byte
Response *http.Response
Message string
}
func (e *ErrorResponse) Error() string {
path, _ := url.QueryUnescape(e.Response.Request.URL.Path)
u := fmt.Sprintf("%s://%s%s", e.Response.Request.URL.Scheme, e.Response.Request.URL.Host, path)
return fmt.Sprintf("%s %s: %d %s", e.Response.Request.Method, u, e.Response.StatusCode, e.Message)
}
func CheckResponse(r *http.Response) error {
switch r.StatusCode {
case 200, 201, 202, 204, 304:
return nil
}
errorResponse := &ErrorResponse{Response: r}
data, err := ioutil.ReadAll(r.Body)
if err == nil && data != nil {
errorResponse.Body = data
var raw interface{}
if err := json.Unmarshal(data, &raw); err != nil {
errorResponse.Message = string(data)
} else {
errorResponse.Message = parseError(raw)
}
}
return errorResponse
}
func parseError(raw interface{}) string {
switch raw := raw.(type) {
case string:
return raw
case []interface{}:
var errs []string
for _, v := range raw {
errs = append(errs, parseError(v))
}
return fmt.Sprintf("[%s]", strings.Join(errs, ", "))
case map[string]interface{}:
var errs []string
for k, v := range raw {
errs = append(errs, fmt.Sprintf("{%s: %s}", k, parseError(v)))
}
sort.Strings(errs)
return strings.Join(errs, ", ")
default:
return fmt.Sprintf("failed to parse unexpected error type: %T", raw)
}
}