-
Notifications
You must be signed in to change notification settings - Fork 4
/
user.go
69 lines (58 loc) · 1.7 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
package mite
import (
"fmt"
"strconv"
"time"
)
// -------------------------------------------------------------
// ~ Types
// -------------------------------------------------------------
// User mite object
type User struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Note string `json:"note"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Archived bool `json:"archived"`
Language string `json:"language"`
Role string `json:"role"`
}
func (u *User) String() string {
return fmt.Sprintf("%s, %s (archived: %t, id: %d, role: %s)", u.Name, u.Email, u.Archived, u.ID, u.Role)
}
type getUsersResponseWrapper struct {
User *User `json:"user"`
}
// -------------------------------------------------------------
// ~ Functions
// -------------------------------------------------------------
// GetUsers returns all users in a mite workspace
func (m *Mite) GetUsers(archived bool) ([]*User, error) {
var usersResponse []*getUsersResponseWrapper
var resource = "users.json"
if archived {
resource = "/users/archived.json"
}
err := m.getAndDecodeFromSuffix(resource, &usersResponse, nil)
if err != nil {
return nil, err
}
users := make([]*User, len(usersResponse))
// Unwrap all the data
for i, u := range usersResponse {
users[i] = u.User
// fmt.Println(u.User.Name, u.User.Email)
}
return users, nil
}
// GetUser returns a mite user for id
func (m *Mite) GetUser(id uint64) (*User, error) {
var resp *getUsersResponseWrapper
err := m.getAndDecodeFromSuffix("users/"+strconv.FormatUint(id, 10)+".json", &resp, nil)
if err != nil {
return nil, err
}
return resp.User, nil
}