-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathusers.go
More file actions
90 lines (75 loc) · 1.93 KB
/
users.go
File metadata and controls
90 lines (75 loc) · 1.93 KB
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
package gokick
import (
"context"
"fmt"
"net/http"
"net/url"
)
type (
UsersResponseWrapper Response[[]UserResponse]
UserResponseWrapper Response[UserResponse]
TokenIntrospectResponseWrapper Response[TokenIntrospectResponse]
)
type UserResponse struct {
Email string `json:"email"`
Name string `json:"name"`
ProfilePicture string `json:"profile_picture"`
UserID int `json:"user_id"`
}
type TokenIntrospectResponse struct {
Active bool `json:"active"`
ClientID string `json:"client_id"`
Exp int `json:"exp"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
}
type UserListFilter struct {
queryParams url.Values
}
func NewUserListFilter() UserListFilter {
return UserListFilter{queryParams: make(url.Values)}
}
func (f UserListFilter) SetID(id int) UserListFilter {
f.queryParams.Set("id", fmt.Sprintf("%d", id))
return f
}
func (f UserListFilter) SetIDs(ids []int) UserListFilter {
for i := range ids {
f.queryParams.Add("id", fmt.Sprintf("%d", ids[i]))
}
return f
}
func (f UserListFilter) ToQueryString() string {
if len(f.queryParams) == 0 {
return ""
}
return "?" + f.queryParams.Encode()
}
func (c *Client) TokenIntrospect(ctx context.Context) (TokenIntrospectResponseWrapper, error) {
response, err := makeRequest[TokenIntrospectResponse](
ctx,
c,
http.MethodPost,
"/public/v1/token/introspect",
http.StatusOK,
http.NoBody,
)
if err != nil {
return TokenIntrospectResponseWrapper{}, err
}
return TokenIntrospectResponseWrapper(response), nil
}
func (c *Client) GetUsers(ctx context.Context, filter UserListFilter) (UsersResponseWrapper, error) {
response, err := makeRequest[[]UserResponse](
ctx,
c,
http.MethodGet,
fmt.Sprintf("/public/v1/users%s", filter.ToQueryString()),
http.StatusOK,
http.NoBody,
)
if err != nil {
return UsersResponseWrapper{}, err
}
return UsersResponseWrapper(response), nil
}