forked from PagerDuty/go-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension_schema.go
115 lines (96 loc) · 3.75 KB
/
extension_schema.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
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
111
112
113
114
115
package pagerduty
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// ExtensionSchema represnts the object presented by the API for each extension
// schema.
type ExtensionSchema struct {
APIObject
IconURL string `json:"icon_url"`
LogoURL string `json:"logo_url"`
Label string `json:"label"`
Key string `json:"key"`
Description string `json:"description"`
GuideURL string `json:"guide_url"`
SendTypes []string `json:"send_types"`
URL string `json:"url"`
}
// ListExtensionSchemaResponse is the object presented in response to the
// request to list all extension schemas.
type ListExtensionSchemaResponse struct {
APIListObject
ExtensionSchemas []ExtensionSchema `json:"extension_schemas"`
}
// ListExtensionSchemaOptions are the options to send with the
// ListExtensionSchema reques(s).
type ListExtensionSchemaOptions struct {
// Limit is the pagination parameter that limits the number of results per
// page. PagerDuty defaults this value to 25 if omitted, and sets an upper
// bound of 100.
Limit uint `url:"limit,omitempty"`
// Offset is the pagination parameter that specifies the offset at which to
// start pagination results. When trying to request the next page of
// results, the new Offset value should be currentOffset + Limit.
Offset uint `url:"offset,omitempty"`
// Total is the pagination parameter to request that the API return the
// total count of items in the response. If this field is omitted or set to
// false, the total number of results will not be sent back from the PagerDuty API.
//
// Setting this to true will slow down the API response times, and so it's
// recommended to omit it unless you've a specific reason for wanting the
// total count of items in the collection.
Total bool `url:"total,omitempty"`
}
// ListExtensionSchemas lists all of the extension schemas. Each schema
// represents a specific type of outbound extension.
//
// Deprecated: Use ListExtensionSchemasWithContext instead.
func (c *Client) ListExtensionSchemas(o ListExtensionSchemaOptions) (*ListExtensionSchemaResponse, error) {
return c.ListExtensionSchemasWithContext(context.Background(), o)
}
// ListExtensionSchemasWithContext lists all of the extension schemas. Each
// schema represents a specific type of outbound extension.
func (c *Client) ListExtensionSchemasWithContext(ctx context.Context, o ListExtensionSchemaOptions) (*ListExtensionSchemaResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}
resp, err := c.get(ctx, "/extension_schemas?"+v.Encode())
if err != nil {
return nil, err
}
var result ListExtensionSchemaResponse
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}
return &result, nil
}
// GetExtensionSchema gets a single extension schema.
//
// Deprecated: Use GetExtensionSchemaWithContext instead.
func (c *Client) GetExtensionSchema(id string) (*ExtensionSchema, error) {
return c.GetExtensionSchemaWithContext(context.Background(), id)
}
// GetExtensionSchemaWithContext gets a single extension schema.
func (c *Client) GetExtensionSchemaWithContext(ctx context.Context, id string) (*ExtensionSchema, error) {
resp, err := c.get(ctx, "/extension_schemas/"+id)
return getExtensionSchemaFromResponse(c, resp, err)
}
func getExtensionSchemaFromResponse(c *Client, resp *http.Response, err error) (*ExtensionSchema, error) {
if err != nil {
return nil, err
}
var target map[string]ExtensionSchema
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
const rootNode = "extension_schema"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}
return &t, nil
}