This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
client.go
133 lines (111 loc) · 3.36 KB
/
client.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package gosnow
import (
"errors"
"net/url"
"regexp"
"github.com/levigross/grequests"
)
const pathFormat string = "^/(?:[._a-zA-Z0-9-]/?)+[^/]$"
//isValidatePath assesses whether the given path is valid
//
//expected format is /<component>[/component]
func isValidatePath(path string) bool {
if match, _ := regexp.MatchString(pathFormat, path); !match {
logger.Printf("Path validation failed - Expected: '/<component>[/component], got: %s\n", path)
return false
}
return true
}
//Client used as main client for service-now
type Client struct {
Username string `validate:"required"`
Instance string
Use_ssl bool
ready bool
Session *grequests.Session
BaseURL *url.URL
}
const sharedBase = "/api"
// New Creates a new Client struct using the provided username, password, and instance
func New(username, password, instance string) (C Client, err error) {
if username == "" {
err = NewMissingParameter("no username provided.")
logger.Println(err)
return C, err
} else if password == "" {
err = NewMissingParameter("no password provided.")
logger.Println(err)
return C, err
} else if instance == "" {
err = NewMissingParameter("no instance provided.")
logger.Println(err)
return C, err
} else {
C.Username = username
C.BaseURL = &url.URL{
Scheme: "https",
Host: instance + ".service-now.com",
}
C.Session = grequests.NewSession(&grequests.RequestOptions{Auth: []string{username, password}})
C.ready = true
}
return C, nil
}
// Resource is used to create table resources
// Each new table that can be queried needs its own .Resource
func (C Client) Resource(apiPath string) (Resource, error) {
basePath := sharedBase + "/now"
if !C.ready {
err := NewInvalidResource("failed to create resource, empty client.")
logger.Println(err)
return Resource{}, err
}
for _, path := range []string{apiPath, basePath} {
if !isValidatePath(path) {
err := NewInvalidResource("invalid web address")
logger.Println(err)
return Resource{}, err
}
}
return NewResource(C.BaseURL, basePath, apiPath, C.Session, 8192), nil
}
//Table returns a new instance of the Table API
func (C Client) Table(tableName string) (Resource, error) {
basePath := sharedBase + "/now"
apiPath := "/table/" + tableName
if !C.ready {
err := NewInvalidResource("failed to create resource, empty client.")
logger.Println(err)
return Resource{}, err
}
for _, path := range []string{apiPath, basePath} {
if !isValidatePath(path) {
err := errors.New("invalid web address")
logger.Println(err)
return Resource{}, err
}
}
return NewResource(C.BaseURL, basePath, apiPath, C.Session, 8192), nil
}
//Attachments returns a new instance of the Attachments API
func (C Client) Attachments() (Attachment, error) {
resource, _ := C.Resource("/attachment")
return resource.attachment()
}
//ServiceCatalog returns a new instance of the Service Catalog API
func (C Client) ServiceCatalog(apiPath string) (ServiceCatalog, error) {
basePath := sharedBase + "/sn_sc"
if !C.ready {
err := errors.New("failed to create service catalog, empty client")
logger.Println(err)
return ServiceCatalog{}, err
}
for _, path := range []string{apiPath, basePath} {
if !isValidatePath(path) {
err := errors.New("invalid web address")
logger.Println(err)
return ServiceCatalog{}, err
}
}
return NewServiceCatalog(C.BaseURL, basePath, apiPath, C.Session, 8192), nil
}