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
|
package grocy
import (
"context"
"errors"
"fmt"
"net/http"
)
type (
User struct {
Id int `json:"id"`
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
RowCreated string `json:"row_created_timestamp"`
DisplayName string `json:"display_name"`
PictureFileName string `json:"picture_file_name"`
}
)
func (c *Client) GetUsers(ctx context.Context) ([]User, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/users", c.BaseUrl), nil)
if err != nil {
return nil, err
}
req.Header.Add("GROCY-API-KEY", c.apiKey)
req.WithContext(ctx)
var res []User
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return res, nil
}
func (c *Client) GetUser(ctx context.Context, userId int) (User, error) {
// This is kinda hacky because grocy doesn't support getting LDAP users by
// grocy ID :(
res, err := c.GetUsers(ctx)
if err != nil {
return *new(User), err
}
for _, u := range res {
if u.Id == userId {
return u, nil
}
}
return *new(User), errors.New("User Not Found")
}
func (c *Client) GetUserFields(ctx context.Context, userId int) (map[string]string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/userfields/users/%d", c.BaseUrl, userId), nil)
if err != nil {
return nil, err
}
req.WithContext(ctx)
var res map[string]string
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return res, nil
}
|