-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.go
68 lines (54 loc) · 1.57 KB
/
User.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
package libdatamanager
import "strings"
// Login login into the server
func (libdm LibDM) Login(username, password string) (*LoginResponse, error) {
var response LoginResponse
// Do http request
resp, err := libdm.NewRequest(EPLogin, CredentialsRequest{
Password: password,
Username: strings.ToLower(username),
MachineID: libdm.Config.MachineID,
}).Do(&response)
// Return new error on ... error
if err != nil || resp.Status == ResponseError {
return nil, NewErrorFromResponse(resp, err)
}
return &response, nil
}
// Register create a new account. Return true on success
func (libdm LibDM) Register(username, password string) (*RestRequestResponse, error) {
// Do http request
resp, err := libdm.NewRequest(EPRegister, CredentialsRequest{
Username: strings.ToLower(username),
Password: password,
}).Do(nil)
if err != nil {
return resp, NewErrorFromResponse(resp, err)
}
return resp, nil
}
// Stats for user
func (libdm LibDM) Stats(namespace string) (*StatsResponse, error) {
var response StatsResponse
if _, err := libdm.Request(EPUserStats, &StatsRequestStruct{
Namespace: namespace,
}, &response, true); err != nil {
return nil, err
}
return &response, nil
}
// Ping pings a server the REST way to
// ensure it is reachable
func (libdm LibDM) Ping() (*StringResponse, error) {
var response StringResponse
// Do ping request
req := libdm.NewRequest(EPPing, PingRequest{Payload: "ping"})
if libdm.Config.SessionToken != "" {
req.WithAuthFromConfig()
}
_, err := req.Do(&response)
if err != nil {
return nil, err
}
return &response, nil
}