-
Notifications
You must be signed in to change notification settings - Fork 3
/
User.go
139 lines (103 loc) · 2.95 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
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
package HologramGo
import (
"fmt"
"os"
"strconv"
)
// Users is just a list of User(s).
type Users []User
// User implements the User type returned from the response.
type User map[string]interface{}
// CreateUser makes a HTTP Post call to create a new user.
func CreateUser(id int) User {
var params Parameters
req := createPostRequest("/users/", params)
resp, err := sendRequest(req)
if err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
return unmarshallIntoObject(resp)
}
// GetUserAccountDetails returns the user's account details based on the given userid.
func GetUserAccountDetails(id int) User {
req := createGetRequest("/users/" + strconv.Itoa(id))
resp, err := sendRequest(req)
if err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
return unmarshallIntoObject(resp)
}
// ChangeUserPassword changes the user's password.
func ChangeUserPassword(password string) User {
var params Parameters
req := createPostRequest("/users/me/password/", params)
resp, err := sendRequest(req)
if err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
return unmarshallIntoObject(resp)
}
// GetUserAddresses retrieves user addresses.
func GetUserAddresses() User {
req := createGetRequest("/users/me/addresses")
resp, err := sendRequest(req)
if err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
return unmarshallIntoObject(resp)
}
// AddUserAddress adds a new address to the user.
func AddUserAddress() User {
var params Parameters
req := createPostRequest("/users/me/addresses", params)
resp, err := sendRequest(req)
if err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
return unmarshallIntoObject(resp)
}
// GetAPIKey returns the user's API key.
func GetAPIKey() User {
req := createGetRequest("/users/me/apikey")
resp, err := sendRequest(req)
if err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
return unmarshallIntoObject(resp)
}
// GenerateNewAPIKey generates a new API key.
func GenerateNewAPIKey() User {
var params Parameters
req := createPostRequest("/users/me/apikey", params)
resp, err := sendRequest(req)
if err != nil {
fmt.Printf("Could not send request: %v\n", err)
os.Exit(1)
}
return unmarshallIntoObject(resp)
}
///////////////////////////////////////////////////
// GENERIC USER GETTER FUNCTIONS
///////////////////////////////////////////////////
// GetUserFirstName returns the first name of the given user.
func (user User) GetUserFirstName() string {
return user["first"].(string)
}
// GetUserLastName returns the last name of the given user.
func (user User) GetUserLastName() string {
return user["last"].(string)
}
// GetUserRole returns the role of the user.
func (user User) GetUserRole() string {
return user["role"].(string)
}
// GetUserAPIKey returns the user's API key.
func (user User) GetUserAPIKey() string {
return user["apikey"].(string)
}