-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusers.go
108 lines (96 loc) · 2.66 KB
/
users.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
package jumpcloud
import (
"encoding/json"
"io"
"net/http"
"net/url"
)
// GetUser returns the details of a user by ID
func (c *Client) GetUser(userId string) (user SystemUser, err error) {
// Prepare request
c.HostURL.Path = "/api/systemusers/" + userId
req, err := http.NewRequest(http.MethodGet, c.HostURL.String(), nil)
req.Header = c.Headers
// Send request
response, err := c.HTTPClient.Do(req)
defer response.Body.Close()
if err != nil {
return user, err
}
// Parse response
body, _ := io.ReadAll(response.Body)
err = json.Unmarshal(body, &user)
return user, err
}
// GetUserIDFromEmail returns the details of a user
func (c *Client) GetUserIDFromEmail(userEmail string) (string, error) {
// Prepare request
var searchStruct struct {
TotalCount int `json:"totalCount"`
Results []struct {
ID string `json:"_id"`
}
}
// Filter on email and return _id field only
params := url.Values{
"filter": {"email:$eq:" + userEmail},
"fields": {"_id"},
}
c.HostURL.Path = "/api/systemusers"
c.HostURL.RawQuery = params.Encode()
req, err := http.NewRequest(http.MethodGet, c.HostURL.String(), nil)
req.Header = c.Headers
// Send request
response, err := c.HTTPClient.Do(req)
defer response.Body.Close()
if err != nil {
return "", err
}
// Parse response
body, _ := io.ReadAll(response.Body)
err = json.Unmarshal(body, &searchStruct)
// Returning the first result
// If no results the return empty string, this means that the search failed
if len(searchStruct.Results) == 0 {
return "", err
} else {
// If we have results, return the first result
return searchStruct.Results[0].ID, err
}
}
// GetUserEmailFromID returns the details of a user
func (c *Client) GetUserEmailFromID(userID string) (string, error) {
// Prepare request
var searchStruct struct {
TotalCount int `json:"totalCount"`
Results []struct {
Email string `json:"email"`
}
}
// Filter on userId and return email field only
params := url.Values{
"filter": {"_id:$eq:" + userID},
"fields": {"email"},
}
c.HostURL.Path = "/api/systemusers"
c.HostURL.RawQuery = params.Encode()
req, err := http.NewRequest(http.MethodGet, c.HostURL.String(), nil)
req.Header = c.Headers
// Send request
response, err := c.HTTPClient.Do(req)
defer response.Body.Close()
if err != nil {
return "", err
}
// Parse response
body, _ := io.ReadAll(response.Body)
err = json.Unmarshal(body, &searchStruct)
// Returning the first result
// If no results the return empty string, this means that the search failed
if len(searchStruct.Results) == 0 {
return "", err
} else {
// If we have results, return the first result
return searchStruct.Results[0].Email, err
}
}