added GetUser function
This commit is contained in:
parent
6df7c52e9e
commit
b665bf63a0
5 changed files with 112 additions and 6 deletions
65
client.go
65
client.go
|
|
@ -8,6 +8,7 @@ import (
|
|||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"encoding/json"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
|
@ -85,3 +86,67 @@ func truncate(b []byte) string {
|
|||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// apiUser mirrors the ST user payload. Custom properties are heterogeneous,
|
||||
// so they stay raw until read individually.
|
||||
type apiUser struct {
|
||||
ID int64 `json:"id"`
|
||||
Email string `json:"email"`
|
||||
ChapterID int64 `json:"chapter_id"`
|
||||
CustomProps map[string]json.RawMessage `json:"custom_user_properties"`
|
||||
}
|
||||
|
||||
func (a apiUser) toUser() *User {
|
||||
return &User{
|
||||
ID: a.ID,
|
||||
Email: a.Email,
|
||||
ChapterID: a.ChapterID,
|
||||
Department: departmentOf(a.CustomProps),
|
||||
}
|
||||
}
|
||||
|
||||
// departmentOf reads the free-text "department-1" property.
|
||||
func departmentOf(props map[string]json.RawMessage) string {
|
||||
raw, ok := props["department-1"]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
var s string
|
||||
if err := json.Unmarshal(raw, &s); err != nil {
|
||||
return ""
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// isRep tolerates all three shapes "department-rep" has been seen in:
|
||||
// array of options, bare string, bare bool.
|
||||
func isRep(props map[string]json.RawMessage) bool {
|
||||
raw, ok := props["department-rep"]
|
||||
if !ok || len(raw) == 0 {
|
||||
return false
|
||||
}
|
||||
var arr []struct{ Value string }
|
||||
if err := json.Unmarshal(raw, &arr); err == nil {
|
||||
return len(arr) > 0
|
||||
}
|
||||
var s string
|
||||
if err := json.Unmarshal(raw, &s); err == nil {
|
||||
return s != "" && s != "false"
|
||||
}
|
||||
var b bool
|
||||
if err := json.Unmarshal(raw, &b); err == nil {
|
||||
return b
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (c *Client) GetUser(ctx context.Context, id int64) (*User, error) {
|
||||
raw, err := c.do(ctx, http.MethodGet, "/users/"+strconv.FormatInt(id, 10), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var u apiUser
|
||||
if err := json.Unmarshal(raw, &u); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return u.toUser(), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue