forked from selectel-vds/go-vscale
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
177 lines (144 loc) · 4.64 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package vscale_api_go
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/websocket"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
)
// Interface of Client for mocking data receiving in tests
type Client interface {
ExecuteRequest(method, url string, body []byte, object interface{}) (*http.Response, error)
WSSConn() (*websocket.Conn, error)
WaitTask(c *websocket.Conn, taskID string) (bool, error)
}
type WebClient struct {
Token string
HTTPClient *http.Client
// TODO Change to type url
// In fact it is host url and API version, example: https://api.vscale.io/v1/
BaseURL string
// Services which is used for accessing API
Account *AccountService
Scalet *ScaletService
Background *BackgroundService
Configuration *ConfigurationService
SSHKey *SSHKeyService
Notification *NotificationService
Billing *BillingService
Domain *DomainService
DomainRecord *DomainRecordService
DomainTag *DomainTagService
PTRRecord *PTRRecordService
Backup *BackupService
ServerTag *ServerTagService
}
type Error struct {
Error string `json:"error,omitempty"`
Field string `json:"field,omitempty"`
}
// Web client creating
func NewClient(token string) *WebClient {
// TODO Maybe it will be better to check if token is empty
client := &WebClient{
Token: token,
HTTPClient: new(http.Client),
BaseURL: "https://api.vscale.io/v1/",
}
// TODO Maybe it will be better to add account checking here via token, to be sure that token is valid and user exists
// Passing client to all services for easy client mocking in future and not passing it to every function
client.Account = &AccountService{client}
client.Scalet = &ScaletService{client}
client.Background = &BackgroundService{client}
client.Configuration = &ConfigurationService{client}
client.SSHKey = &SSHKeyService{client}
client.Notification = &NotificationService{client}
client.Billing = &BillingService{client}
client.Domain = &DomainService{client}
client.DomainRecord = &DomainRecordService{client}
client.DomainTag = &DomainTagService{client}
client.PTRRecord = &PTRRecordService{client}
client.Backup = &BackupService{client}
client.ServerTag = &ServerTagService{client}
return client
}
// Executing HTTP Request (receiving info from API)
func (client *WebClient) ExecuteRequest(method, url string, body []byte, object interface{}) (*http.Response, error) {
req, err := http.NewRequest(method, fmt.Sprint(client.BaseURL, url), bytes.NewBuffer(body))
if err != nil {
return new(http.Response), err
}
req.Header.Set("X-Token", client.Token)
req.Header.Set("Content-Type", "application/json;charset=utf-8")
res, err := client.HTTPClient.Do(req)
if err != nil {
return res, err
}
defer res.Body.Close()
// Cloning response body for future using
buf, _ := ioutil.ReadAll(res.Body)
reader := ioutil.NopCloser(bytes.NewBuffer(buf))
res.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
if res.Header.Get("Vscale-Error-Message") != "None" && res.Header.Get("Vscale-Error-Message") != "" {
return res, errors.New(res.Header.Get("Vscale-Error-Message"))
}
if !IsSuccess(res.StatusCode) {
return res, errors.New("Not successful status code")
}
if object != nil && res.StatusCode == 200 {
err := json.NewDecoder(reader).Decode(object)
// EOF means empty response body, this error is not needed
if err != nil && err != io.EOF {
log.Println(err)
return res, err
}
}
return res, nil
}
func (client *WebClient) WSSConn() (*websocket.Conn, error) {
u := url.URL{Scheme: "wss", Host: "ws.api.vscale.io", Path: "/v1/ws"}
header := http.Header{}
header["X-Token"] = []string{client.Token}
c, _, err := websocket.DefaultDialer.Dial(u.String(), header)
if err != nil {
return c, err
}
return c, nil
}
// Waiting until operation will be complete
func (client *WebClient) WaitTask(c *websocket.Conn, taskID string) (bool, error) {
defer c.Close()
msg := struct {
ID int64 `json:"id,omitempty"`
Result struct {
Type string `json:"type,omitempty"`
Error string `json:"error,omitempty"`
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
Done bool `json:"done,omitempty"`
ID string `json:"id,omitempty"`
} `json:"result,omitempty"`
Time string `json:"time,omitempty"`
}{}
for {
code, message, err := c.ReadMessage()
if err != nil {
return false, err
}
if code == 1 {
json.Unmarshal(message, &msg)
if msg.Result.ID == taskID && msg.Result.Done == true {
if msg.Result.Error != "" {
return false, errors.New(msg.Result.Error)
}
return true, nil
}
}
}
}