forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 2
/
response.go
57 lines (47 loc) · 1.66 KB
/
response.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
package zabbix
import (
"encoding/json"
"fmt"
)
// Response represents the response from a JSON-RPC API request.
//
// This struct maps to the JSON response body described in the Zabbix API
// documentation:
// https://www.zabbix.com/documentation/2.2/manual/api#authentication.
type Response struct {
// HTTP status code of the API response e.g. 200
StatusCode int `json:"-"`
// JSONRPCVersion is the version string of the Zabbix API. This should
// always be set to "2.0".
JSONRPCVersion string `json:"jsonrpc"`
// Body represents the response body as an array of bytes.
//
// The Body may be decoded later into a struct with Bind or json.Unmarshal.
Body json.RawMessage `json:"result"`
// RequestID is an abitrary identifier which matches the RequestID set in
// the corresponding API Request.
RequestID int `json:"id"`
// Error is populated with error information if the JSON-RPC request
// succeeded but there was an API error.
//
// This struct maps to the JSON response body described in the Zabbix API
// documentation:
// https://www.zabbix.com/documentation/2.2/manual/api#error_handling.
Error APIError `json:"error"`
}
// Err returns an error if the Response includes any error information returned
// from the Zabbix API.
func (c *Response) Err() error {
if c.Error.Code != 0 {
return fmt.Errorf("HTTP %d %s (%d)\n%s", c.StatusCode, c.Error.Message, c.Error.Code, c.Error.Data)
}
return nil
}
// Bind unmarshals the JSON body of the Response into the given interface.
func (c *Response) Bind(v interface{}) error {
err := json.Unmarshal(c.Body, v)
if err != nil {
return fmt.Errorf("Error decoding JSON response body: %v", err)
}
return nil
}