-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.go
83 lines (65 loc) · 1.66 KB
/
server.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
package client
import (
"encoding/json"
"fmt"
neturl "net/url"
"github.com/nl2go/hrobot-go/models"
)
func (c *Client) ServerGetList() ([]models.Server, error) {
url := c.baseURL + "/server"
bytes, err := c.doGetRequest(url)
if err != nil {
return nil, err
}
var servers []models.ServerResponse
err = json.Unmarshal(bytes, &servers)
if err != nil {
return nil, err
}
var data []models.Server
for _, server := range servers {
data = append(data, server.Server)
}
return data, nil
}
func (c *Client) ServerGet(ip string) (*models.Server, error) {
url := fmt.Sprintf(c.baseURL+"/server/%s", ip)
bytes, err := c.doGetRequest(url)
if err != nil {
return nil, err
}
var serverResp models.ServerResponse
err = json.Unmarshal(bytes, &serverResp)
if err != nil {
return nil, err
}
return &serverResp.Server, nil
}
func (c *Client) ServerSetName(ip string, input *models.ServerSetNameInput) (*models.Server, error) {
url := fmt.Sprintf(c.baseURL+"/server/%s", ip)
formData := neturl.Values{}
formData.Set("server_name", input.Name)
bytes, err := c.doPostFormRequest(url, formData)
if err != nil {
return nil, err
}
var serverResp models.ServerResponse
err = json.Unmarshal(bytes, &serverResp)
if err != nil {
return nil, err
}
return &serverResp.Server, nil
}
func (c *Client) ServerReverse(ip string) (*models.Cancellation, error) {
url := fmt.Sprintf(c.baseURL+"/server/%s/reversal", ip)
bytes, err := c.doPostFormRequest(url, nil)
if err != nil {
return nil, err
}
var cancelResp models.CancellationResponse
err = json.Unmarshal(bytes, &cancelResp)
if err != nil {
return nil, err
}
return &cancelResp.Cancellation, nil
}