-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnrest.go
More file actions
102 lines (86 loc) · 4.03 KB
/
snrest.go
File metadata and controls
102 lines (86 loc) · 4.03 KB
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
package snrest
import (
"net/http"
"github.com/dghubble/sling"
)
type GetParams struct {
Query string `url:"sysparm_query,omitempty"`
DisplayValue string `url:"sysparm_display_value,omitempty"`
Fields string `url:"sysparm_fields,omitempty"`
View string `url:"sysparm_view,omitempty"`
Limit int `url:"sysparm_limit,omitempty"`
Offset int `url:"sysparm_offset,omitempty"`
ExcludeReferenceLink bool `url:"sysparm_exclude_reference_link,omitempty"`
}
type PostParams struct {
DisplayValue string `url:"sysparm_display_value,omitempty"`
Fields string `url:"sysparm_fields,omitempty"`
View string `url:"sysparm_view,omitempty"`
ExcludeReferenceLink bool `url:"sysparm_exclude_reference_link,omitempty"`
InputDisplayValue bool `url:"sysparm_input_display_value,omitempty"`
}
type PutParams struct {
DisplayValue string `url:"sysparm_display_value,omitempty"`
Fields string `url:"sysparm_fields,omitempty"`
View string `url:"sysparm_view,omitempty"`
ExcludeReferenceLink bool `url:"sysparm_exclude_reference_link,omitempty"`
InputDisplayValue bool `url:"sysparm_input_display_value,omitempty"`
}
type JsonV2Params struct {
Action string `url:"sysparm_action,omitempty"`
SysID string `url:"sysparm_sys_id,omitempty"`
Query string `url:"sysparm_query,omitempty"`
View string `url:"sysparm_view,omitempty"`
RecordCount int `url:"sysparm_record_count,omitempty"`
DisplayValue string `url:"displayvalue,omitempty"`
DisplayVariables bool `url:"displayvariables,omitempty"`
}
type RestClient struct {
sling *sling.Sling
}
func New(instance string, user string, pass string) *RestClient {
baseUrl := "https://" + instance + ".service-now.com/"
return &RestClient{
sling: sling.New().Client(&http.Client{}).Base(baseUrl).Set("Accept", "application/json").SetBasicAuth(user, pass),
}
}
func (c *RestClient) GetKeys(table string, params JsonV2Params) ([]string, error) {
type GetKeysResult struct {
Records []string `json:"records"`
}
params.Action = "getKeys"
result := GetKeysResult{}
_, err := c.sling.Get(table + ".do?JSONv2").QueryStruct(params).ReceiveSuccess(&result)
return result.Records, err
}
func (c *RestClient) Get(table string, sysID string, params GetParams, successV interface{}) (*http.Response, error) {
return c.sling.Get("/api/now/table/" + table + "/" + sysID).QueryStruct(params).ReceiveSuccess(successV)
}
func (c *RestClient) GetMultiple(table string, params GetParams, successV interface{}) (*http.Response, error) {
return c.sling.Get("/api/now/table/" + table).QueryStruct(params).ReceiveSuccess(successV)
}
func (c *RestClient) Insert(table string, params PostParams, data interface{}, successV interface{}) (*http.Response, error) {
return c.sling.Post("/api/now/table/" + table).QueryStruct(params).BodyJSON(data).ReceiveSuccess(successV)
}
func (c *RestClient) InsertMultiple(table string, params JsonV2Params, data interface{}, successV interface{}) (*http.Response, error) {
return c.sling.Post("/" + table + ".do?JSONv2").QueryStruct(params).BodyJSON(data).ReceiveSuccess(successV)
}
func (c *RestClient) Update(table string, params PutParams, data interface{}, successV interface{}) (*http.Response, error) {
return c.sling.Put("/api/now/table/" + table).QueryStruct(params).BodyJSON(data).ReceiveSuccess(successV)
}
func (c *RestClient) Delete(table string, sysID string) (*http.Response, error) {
type Dummy struct{}
d := Dummy{}
return c.sling.Delete("/api/now/table/" + table + "/" + sysID).ReceiveSuccess(d)
}
func (c *RestClient) DeleteMultiple(table string, params JsonV2Params) (int, error) {
type DeleteMultipleResult struct {
Records []struct {
Count int `json:"count"`
} `json:"records"`
}
params.Action = "deleteMultiple"
result := DeleteMultipleResult{}
_, err := c.sling.Get("/" + table + ".do?JSONv2").QueryStruct(params).ReceiveSuccess(&result)
return result.Records[0].Count, err
}