This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.go
85 lines (67 loc) · 2.06 KB
/
extension.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
package pagerduty
import (
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
type Extension struct {
APIObject
Name string `json:"name"`
EndpointURL string `json:"endpoint_url"`
ExtensionObjects []APIObject `json:"extension_objects"`
ExtensionSchema APIObject `json:"extension_schema"`
Config interface{} `json:"config"`
}
type ListExtensionResponse struct {
APIListObject
Extensions []Extension `json:"extensions"`
}
type ListExtensionOptions struct {
APIListObject
ExtensionObjectID string `url:"extension_object_id,omitempty"`
ExtensionSchemaID string `url:"extension_schema_id,omitempty"`
Query string `url:"query,omitempty"`
}
func (c *Client) ListExtensions(o ListExtensionOptions) (*ListExtensionResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get("/extensions?" + v.Encode())
if err != nil {
return nil, err
}
var result ListExtensionResponse
return &result, c.decodeJSON(resp, &result)
}
func (c *Client) CreateExtension(e *Extension) (*Extension, error) {
resp, err := c.post("/extensions", e, nil)
return getExtensionFromResponse(c, resp, err)
}
func (c *Client) DeleteExtension(id string) error {
_, err := c.delete("/extensions/" + id)
return err
}
func (c *Client) GetExtension(id string) (*Extension, error) {
resp, err := c.get("/extensions/" + id)
return getExtensionFromResponse(c, resp, err)
}
func (c *Client) UpdateExtension(id string, e *Extension) (*Extension, error) {
resp, err := c.put("/extensions/"+id, e, nil)
return getExtensionFromResponse(c, resp, err)
}
func getExtensionFromResponse(c *Client, resp *http.Response, err error) (*Extension, error) {
if err != nil {
return nil, err
}
var target map[string]Extension
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
rootNode := "extension"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}