-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
578 lines (501 loc) · 15.5 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package utopiago
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)
const (
MaxCharactersInPaymentComment = 148
DefaultCurrencyTag = "CRP"
)
const (
ChannelTypeRegistered ChannelType = iota
ChannelTypeRecent
ChannelTypeMy
ChannelTypeFriends
ChannelTypeBookmarked
ChannelTypeJoined
ChannelTypeOpened
ChannelTypeBlackList
ChannelTypeDeleted
)
const (
SortChannelsByCreated SortChannelsBy = iota + 1
SortChannelsByIsPrivate
SortChannelsByModified
SortChannelsByName
SortChannelsByDescription
)
type SortChannelsBy int
type ChannelType int
// NewClient - create client with default data:
// http protocol, localhost, default port (20000)
func NewClient(token string) *UtopiaClient {
return &UtopiaClient{
Protocol: "http",
Host: "localhost",
Token: token,
Port: 20000,
WsPort: 25000,
}
}
func (c *UtopiaClient) SetToken(token string) *UtopiaClient {
c.Token = token
return c
}
func (c *UtopiaClient) SetProtocol(proto string) *UtopiaClient {
c.Protocol = proto
return c
}
func (c *UtopiaClient) SetPort(port int) *UtopiaClient {
c.Port = port
return c
}
func (c *UtopiaClient) SetWsPort(wsPort int) *UtopiaClient {
c.WsPort = wsPort
return c
}
func (c *UtopiaClient) SetLogsCallback(cb LogCallback) {
c.logCallback = cb
}
// GetProfileStatus gets data about the status of the current account
func (c *UtopiaClient) GetProfileStatus() (map[string]interface{}, error) {
return c.apiQuery("getProfileStatus", nil)
}
// GetSystemInfo retrieves client system information
func (c *UtopiaClient) GetSystemInfo() (map[string]interface{}, error) {
return c.apiQuery("getSystemInfo", nil)
}
// SetProfileStatus updates data about the status of the current account
func (c *UtopiaClient) SetProfileStatus(status string, mood string) error {
queryMap := make(map[string]interface{})
queryMap["status"] = status
queryMap["mood"] = mood
result, err := c.queryResultToBool("setProfileStatus", queryMap)
if err != nil {
return err
}
if !result {
return errors.New("failed to set profile status")
}
return nil
}
// GetOwnContact asks for full details of the current account
func (c *UtopiaClient) GetOwnContact() (OwnContactData, error) {
response, err := c.apiQuery("getOwnContact", nil)
if err != nil {
return OwnContactData{}, err
}
// check result exists
result, isResultFound := response["result"]
if !isResultFound {
return OwnContactData{}, errors.New("accaptable result doesn't exists in client response")
}
// convert result
jsonBytes, err := json.Marshal(result)
if err != nil {
return OwnContactData{}, errors.New("failed to encode response result: " + err.Error())
}
ownContact := OwnContactData{}
err = json.Unmarshal(jsonBytes, &ownContact)
if err != nil {
return OwnContactData{}, errors.New("failed to decode reconverted result: " + err.Error())
}
return ownContact, nil
}
// CheckClientConnection - checks if there are any errors when contacting the client
func (c *UtopiaClient) CheckClientConnection() bool {
_, err := c.GetSystemInfo()
return err == nil
}
// UseVoucher - uses the voucher and returns an error on failure
func (c *UtopiaClient) UseVoucher(voucherID string) (string, error) {
params := map[string]interface{}{
"voucherid": voucherID,
}
return c.queryResultToString("useVoucher", params)
}
// GetFinanceHistory request the necessary financial statistics
func (c *UtopiaClient) GetFinanceHistory(filters string, referenceNumber string) ([]interface{}, error) {
params := map[string]interface{}{
"filters": filters,
"referenceNumber": referenceNumber,
}
return c.queryResultToInterfaceArray("getFinanceSystemInformation", params)
}
// GetBalance request account Crypton balance
func (c *UtopiaClient) GetBalance() (float64, error) {
result, err := c.queryResultToFloat64("getBalance", map[string]interface{}{})
if err != nil {
return 0, err
}
return result, nil
}
// GetUUSDBalance request account UUSD balance
func (c *UtopiaClient) GetUUSDBalance() (float64, error) {
params := map[string]interface{}{
"currency": "USD",
}
result, err := c.queryResultToFloat64("getBalance", params)
if err != nil {
return 0, err
}
return result, nil
}
func (c *UtopiaClient) createCoinVoucher(amount float64, coin string) (string, error) {
params := map[string]interface{}{
"amount": amount,
"currency": coin,
}
result, err := c.queryResultToString("createVoucher", params)
if err != nil {
return "", err
}
if result == "" {
return "", errors.New("failed to create voucher, empty string in client response")
}
return result, nil
}
// CreateVoucher requests the creation of a new Crypton voucher. it returns referenceNumber
func (c *UtopiaClient) CreateVoucher(amount float64) (string, error) {
return c.createCoinVoucher(amount, "CRP")
}
// CreateUUSDVoucher requests the creation of a new UUSD voucher. it returns referenceNumber
func (c *UtopiaClient) CreateUUSDVoucher(amount float64) (string, error) {
return c.createCoinVoucher(amount, "UUSD")
}
// SetWebSocketState - set WSS Notification state
func (c *UtopiaClient) SetWebSocketState(task SetWsStateTask) error {
params := map[string]interface{}{
"enabled": strconv.FormatBool(task.Enabled),
"port": strconv.Itoa(task.Port),
}
if task.EnableSSL {
params["enablessl"] = strconv.FormatBool(task.EnableSSL)
}
if task.Notifications != "" {
params["notifications"] = task.Notifications
}
result, err := c.queryResultToString("setWebSocketState", params)
if err != nil {
return err
}
if result == "" {
return errors.New("failed to set websocker state")
}
return nil
}
// GetWebSocketState - returns WSS Notifications state, 0 - disabled or active listening port number.
func (c *UtopiaClient) GetWebSocketState() (int64, error) {
result, err := c.queryResultToInt("getWebSocketState", nil)
if err != nil {
return 0, err
}
return result, nil
}
// SendChannelMessage - send channel message & get message ID
func (c *UtopiaClient) SendChannelMessage(channelID, message string) (string, error) {
params := map[string]interface{}{
"channelid": channelID,
"message": message,
}
return c.queryResultToString("sendChannelMessage", params)
}
// SendChannelContactMessage - send channel message to contact in private mode
func (c *UtopiaClient) SendChannelContactMessage(channelID, contactPubkeyHash, message string) (string, error) {
params := map[string]interface{}{
"channelid": channelID,
"contactHashedPk": contactPubkeyHash,
"message": message,
}
return c.queryResultToString("sendChannelPrivateMessageToContact", params)
}
// SendChannelPicture - send channel picture & get message ID
func (c *UtopiaClient) SendChannelPicture(channelID, base64Image, comment, filenameForImage string) (string, error) {
params := map[string]interface{}{
"channelid": channelID,
"base64_image": base64Image,
"comment": comment,
"filename_image": filenameForImage,
}
return c.queryResultToString("sendChannelPicture", params)
}
// GetStickerNamesByCollection returns available names from corresponded collection
func (c *UtopiaClient) GetStickerNamesByCollection(collectionName string) ([]string, error) {
params := map[string]interface{}{
"collection_name": collectionName,
}
return c.queryResultToStringsArray("getStickerNamesByCollection", params)
}
// GetStickerImage returns sticker image in base64
func (c *UtopiaClient) GetStickerImage(collectionName, stickerName string) (string, error) {
params := map[string]interface{}{
"collection_name": collectionName,
"sticker_name": stickerName,
"coder": "BASE64",
}
return c.queryResultToString("getImageSticker", params)
}
// UCodeEncode - encode data to uCode image.
// coder: BASE64 for example
// format: JPG or PNG
func (c *UtopiaClient) UCodeEncode(dataHexCode, coder, format string, imageSize int) (string, error) {
return c.queryResultToString("ucodeEncode", map[string]interface{}{
"hex_code": dataHexCode,
"size_image": imageSize,
"coder": "BASE64",
"format": format,
})
}
// SendAuthRequest - send auth request to user
func (c *UtopiaClient) SendAuthRequest(pubkey, message string) (bool, error) {
params := map[string]interface{}{
"pk": pubkey,
"message": message,
}
return c.queryResultToBool("sendAuthorizationRequest", params)
}
// AcceptAuthRequest - accept auth request
func (c *UtopiaClient) AcceptAuthRequest(pubkey, message string) (bool, error) {
params := map[string]interface{}{
"pk": pubkey,
"message": message,
}
return c.queryResultToBool("acceptAuthorizationRequest", params)
}
// RejectAuthRequest - reject user auth request
func (c *UtopiaClient) RejectAuthRequest(pubkey, message string) (bool, error) {
params := map[string]interface{}{
"pk": pubkey,
"message": message,
}
return c.queryResultToBool("rejectAuthorizationRequest", params)
}
// SendInstantMessage - send message to contact (PM).
// to -- pubkey or uNS entry name
func (c *UtopiaClient) SendInstantMessage(to string, message string) (int64, error) {
params := map[string]interface{}{
"to": to,
"text": message,
}
return c.queryResultToInt("sendInstantMessage", params)
}
// GetContacts - get account contacts.
// params: filter - contact pubkey or nickname
func (c *UtopiaClient) GetContacts(filter string) ([]ContactData, error) {
// send request
params := map[string]interface{}{}
if filter != "" {
params["filter"] = filter
}
response, err := c.apiQuery("getContacts", params)
if err != nil {
return nil, err
}
data := []ContactData{}
if err := convertResult(response, &data); err != nil {
return nil, err
}
return data, nil
}
// GetContact data
func (c *UtopiaClient) GetContact(pubkeyOrNick string) (ContactData, error) {
contacts, err := c.GetContacts(pubkeyOrNick)
if err != nil {
return ContactData{}, err
}
if len(contacts) == 0 {
return ContactData{}, errors.New("contact not found")
}
return contacts[0], nil
}
// IsOnline - is contact online?
func (d *ContactData) IsOnline() bool {
return d.Status == 4096
}
// IsOffline - is contact offline?
func (d *ContactData) IsOffline() bool {
return d.Status == 65536
}
// IsAway - is contact away?
func (d *ContactData) IsAway() bool {
return d.Status == 4097
}
// IsDoNotDisturb - is contact marked to do not disturb mode?
func (d *ContactData) IsDoNotDisturb() bool {
return d.Status == 4099
}
// IsInvisible - is contact invisible?
func (d *ContactData) IsInvisible() bool {
return d.Status == 32768
}
// JoinChannel - join to channel or chat.
// password is optional. returns join status (bool) and error
func (c *UtopiaClient) JoinChannel(channelID string, password ...string) (bool, error) {
params := map[string]interface{}{
"ident": channelID,
}
if len(password) > 0 {
params["password"] = password[0]
}
return c.queryResultToBool("joinChannel", params)
}
// GetChannelContacts - get channel contacts
func (c *UtopiaClient) GetChannelContacts(channelID string) ([]ChannelContactData, error) {
// send request
params := map[string]interface{}{
"channelid": channelID,
}
response, err := c.apiQuery("getChannelContacts", params)
if err != nil {
return nil, err
}
data := []ChannelContactData{}
if err := convertResult(response, &data); err != nil {
return nil, err
}
return data, nil
}
func (c *UtopiaClient) EnableReadOnly(channelID string, readOnly bool) error {
_, err := c.queryResultToBool("modifyChannel", map[string]interface{}{
"channelid": channelID,
"read_only": readOnly,
})
return err
}
// RemoveChannelMessage - remove channel message
func (c *UtopiaClient) RemoveChannelMessage(channelID string, messageID int64) error {
params := map[string]interface{}{
"channelid": channelID,
"id_message": messageID,
}
_, err := c.queryResultToString("removeChannelMessage", params)
return err
}
// GetChannelMessages - get channel messages with filter (offset, max messages count)
func (c *UtopiaClient) GetChannelMessages(channelID string, offset int, maxMessages int) ([]ChannelMessage, error) {
// send request
params := map[string]interface{}{
"channelid": channelID,
}
filters := map[string]interface{}{
"offset": offset,
"limit": maxMessages,
}
response, err := c.apiQueryWithFilters("getChannelMessages", params, filters)
if err != nil {
return nil, err
}
data := []ChannelMessage{}
if err := convertResult(response, &data); err != nil {
return nil, err
}
return data, nil
}
type SendPaymentTask struct {
// required
To string `json:"to"` // pubkey, nickname or card ID
Amount float64 `json:"amount"` // more than zero, no more than 9 decimal places
// optional
CurrencyTag string `json:"currency"` // example: "CRP", "UUSD". by default: "CRP"
FromCardID string `json:"fromCardID"` // specify here your card ID
Comment string `json:"comment"`
}
// SendPayment - send coins
func (c *UtopiaClient) SendPayment(task SendPaymentTask) (string, error) {
if task.Comment != "" && len(task.Comment) > MaxCharactersInPaymentComment {
return "", fmt.Errorf("comment max length is %v characters", MaxCharactersInPaymentComment)
}
if task.CurrencyTag == "" {
task.CurrencyTag = DefaultCurrencyTag
}
params := map[string]interface{}{
"to": task.To,
"comment": task.Comment,
"cardid": task.FromCardID,
"amount": task.Amount,
"currency": task.CurrencyTag,
}
return c.queryResultToString("sendPayment", params)
}
// GetChannelInfo - get specific channel info
func (c *UtopiaClient) GetChannelInfo(channelID string) (ChannelData, error) {
params := map[string]interface{}{
"channelid": channelID,
}
response, err := c.apiQuery("getChannelInfo", params)
if err != nil {
return ChannelData{}, err
}
data := ChannelData{}
if err := convertResult(response, &data); err != nil {
return ChannelData{}, err
}
return data, nil
}
type GetChannelsTask struct {
// optional
SearchFilter string // part of channel name or channel ID, etc
ChannelType ChannelType // by default: 0 - registered
FromDate string // date example: 2019-11-23T10:00:00.001
ToDate string
SortBy SortChannelsBy
}
// GetChannels get available channels
func (c *UtopiaClient) GetChannels(task GetChannelsTask) ([]SearchChannelData, error) {
params := map[string]interface{}{
"filter": task.SearchFilter,
"channel_type": task.ChannelType,
}
if task.FromDate != "" {
params["from"] = task.FromDate
}
if task.ToDate != "" {
params["to"] = task.ToDate
}
filters := map[string]interface{}{}
switch task.SortBy {
case SortChannelsByCreated:
filters["sortBy"] = "created"
case SortChannelsByIsPrivate:
filters["sortBy"] = "isprivate"
case SortChannelsByName:
filters["sortBy"] = "name"
case SortChannelsByModified:
filters["sortBy"] = "modified"
case SortChannelsByDescription:
filters["sortBy"] = "description"
}
response, err := c.apiQueryWithFilters("getChannels", params, filters)
if err != nil {
return nil, err
}
data := []SearchChannelData{}
if err := convertResult(response, &data); err != nil {
return nil, err
}
return data, nil
}
func (c *UtopiaClient) ToogleChannelNotifications(channelID string, enabled bool) error {
params := map[string]interface{}{
"channelid": channelID,
"enabled": enabled,
}
if _, err := c.queryResultToBool("enableChannelNotification", params); err != nil {
return err
}
return nil
}
// GetNetworkConnections - get current network peers
func (c *UtopiaClient) GetNetworkConnections(channelID string) ([]PeerInfo, error) {
response, err := c.apiQuery("getNetworkConnections", map[string]interface{}{})
if err != nil {
return nil, err
}
data := []PeerInfo{}
if err := convertResult(response, &data); err != nil {
return nil, err
}
return data, nil
}