-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
105 lines (84 loc) · 2.69 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
package main
import (
"bytes"
"fmt"
"net/http"
"net/url"
"time"
)
type Credentials struct {
InstanceURL string
Username string
Password string
}
type Client struct {
*Credentials
httpClient *http.Client
}
func NewClient(c *Credentials) Client {
client := &http.Client{
Timeout: time.Second * 30,
}
return Client{
Credentials: c,
httpClient: client,
}
}
func (c Client) Get(endpoint string, params url.Values) (*http.Response, error) {
uri := fmt.Sprintf("%s/api/now/table/%s?%s", c.InstanceURL, endpoint, params.Encode())
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("[get] failed to create request: %w", err)
}
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(c.Username, c.Password)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("[get] failed to get response: %w", err)
}
return res, nil
}
func (c Client) Post(endpoint string, params url.Values, body []byte) (*http.Response, error) {
uri := fmt.Sprintf("%s/api/now/table/%s?%s", c.InstanceURL, endpoint, params.Encode())
req, err := http.NewRequest(http.MethodPost, uri, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("[post] failed to create request: %w", err)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(c.Username, c.Password)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("[post] failed to get response: %w", err)
}
return res, nil
}
func (c Client) Patch(endpoint string, params url.Values, body []byte) (*http.Response, error) {
uri := fmt.Sprintf("%s/api/now/table/%s?%s", c.InstanceURL, endpoint, params.Encode())
req, err := http.NewRequest(http.MethodPatch, uri, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("[patch] failed to create request: %w", err)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(c.Username, c.Password)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("[patch] failed to get response: %w", err)
}
return res, nil
}
func (c Client) Delete(endpoint string, params url.Values) (*http.Response, error) {
uri := fmt.Sprintf("%s/api/now/table/%s?%s", c.InstanceURL, endpoint, params.Encode())
req, err := http.NewRequest(http.MethodDelete, uri, nil)
if err != nil {
return nil, fmt.Errorf("[delete] failed to create request: %w", err)
}
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(c.Username, c.Password)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("[delete] failed to get response: %w", err)
}
return res, nil
}