forked from nl2go/hrobot-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
150 lines (128 loc) · 3.1 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package client
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/syself/hrobot-go/models"
)
const (
baseURL string = "https://robot-ws.your-server.de"
version = "0.2.6"
userAgent = "hrobot-client/" + version
)
type Client struct {
Username string
Password string
baseURL string
userAgent string
httpClient *http.Client
}
func NewBasicAuthClient(username, password string) RobotClient {
return &Client{
Username: username,
Password: password,
baseURL: baseURL,
userAgent: userAgent,
httpClient: &http.Client{},
}
}
func NewBasicAuthClientWithCustomHttpClient(username, password string, httpClient *http.Client) RobotClient {
return &Client{
Username: username,
Password: password,
baseURL: baseURL,
userAgent: userAgent,
httpClient: httpClient,
}
}
func (c *Client) SetBaseURL(baseURL string) {
c.baseURL = baseURL
}
func (c *Client) SetUserAgent(userAgent string) {
c.userAgent = userAgent
}
func (c *Client) GetVersion() string {
return version
}
func (c *Client) ValidateCredentials() error {
if _, err := c.doGetRequest(c.baseURL); err != nil {
return err
}
return nil
}
func (c *Client) SetCredentials(username, password string) error {
if username == "" {
return fmt.Errorf("username cannot be empty")
}
if password == "" {
return fmt.Errorf("password cannot be empty")
}
c.Username = username
c.Password = password
return nil
}
func (c *Client) doGetRequest(url string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
bytes, err := c.doRequest(req)
if err != nil {
return nil, err
}
return bytes, nil
}
func (c *Client) doDeleteRequest(url string) ([]byte, error) {
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return nil, err
}
bytes, err := c.doRequest(req)
if err != nil {
return nil, err
}
return bytes, nil
}
func (c *Client) doPostFormRequest(url string, formData url.Values) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(formData.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
bytes, err := c.doRequest(req)
if err != nil {
return nil, err
}
return bytes, nil
}
func (c *Client) doRequest(req *http.Request) ([]byte, error) {
req.Header.Set("User-Agent", c.userAgent)
req.SetBasicAuth(c.Username, c.Password)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
return nil, errorFromResponse(resp, body)
}
return body, nil
}
func errorFromResponse(resp *http.Response, body []byte) (reterr error) {
var errorResponse models.ErrorResponse
reterr = fmt.Errorf("server responded with status code %v", resp.StatusCode)
if err := json.Unmarshal(body, &errorResponse); err != nil {
return
}
if errorResponse.Error.Code == "" && errorResponse.Error.Message == "" {
return
}
return errorResponse.Error
}