forked from aiven/aiven-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
57 lines (46 loc) · 1.12 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
// Copyright (c) 2017 jelmersnoeck
// Copyright (c) 2018 Aiven, Helsinki, Finland. https://aiven.io/
package aiven
import (
"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 r == nil {
r = new(APIResponse)
}
if err := json.Unmarshal(bts, &r); err != nil {
return fmt.Errorf("cannot unmarshal JSON `%s`, error: %w", bts, err)
}
if r == nil {
return ErrNoResponseData
}
return r.GetError()
}
func buildPath(parts ...string) string {
finalParts := make([]string, len(parts))
for idx, part := range parts {
finalParts[idx] = url.PathEscape(part)
}
return "/" + strings.Join(finalParts, "/")
}