-
Notifications
You must be signed in to change notification settings - Fork 29
/
api.go
62 lines (51 loc) · 1.13 KB
/
api.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
package aiven
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
)
// APIResponse represents a response returned by the Aiven API.
type APIResponse struct {
Errors []Error `json:"errors,omitempty"`
Message string `json:"message,omitempty"`
}
// Response represents Aiven API response interface
type Response interface {
GetError() error
}
// GetError returns the first error from API Response, if any
func (r APIResponse) GetError() error {
if len(r.Errors) != 0 {
for _, err := range r.Errors {
return err
}
}
return nil
}
func checkAPIResponse(bts []byte, r Response) error {
if len(bts) == 0 {
return nil
}
if r == nil {
r = new(APIResponse)
}
buffer := bytes.NewBuffer(bts)
dec := json.NewDecoder(buffer)
dec.UseNumber()
if err := dec.Decode(&r); err != nil {
return fmt.Errorf("cannot unmarshal JSON `%s`, error: %w", bts, err)
}
if r == nil {
return ErrNoResponseData
}