This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
user_by_id.go
115 lines (95 loc) · 2.02 KB
/
user_by_id.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
109
110
111
112
113
114
115
package uaa
import (
"encoding/json"
"fmt"
"net/url"
)
type UserByIDInterface interface {
UserByID(string) (User, error)
}
type User struct {
Username string
ID string
Name Name
Emails []string
Active bool
Verified bool
}
type Name struct {
FamilyName string
GivenName string
}
func UserByID(u UAA, id string) (User, error) {
user := User{
ID: id,
}
uri, err := url.Parse(fmt.Sprintf("%s/Users/%s", u.uaaURL, id))
if err != nil {
return user, err
}
host := uri.Scheme + "://" + uri.Host
client := NewClient(host, u.VerifySSL).WithAuthorizationToken(u.AccessToken)
code, body, err := client.MakeRequest("GET", uri.RequestURI(), nil)
if err != nil {
return user, err
}
if code > 399 {
return user, NewFailure(code, body)
}
user, err = UserFromJSON(body)
if err != nil {
return user, err
}
return user, nil
}
func UserFromJSON(jsonBytes []byte) (User, error) {
var parsed map[string]interface{}
err := json.Unmarshal(jsonBytes, &parsed)
if err != nil {
return User{}, err
}
return UserFromResource(parsed)
}
func UserFromResource(resource map[string]interface{}) (User, error) {
user := User{}
userName, ok := resource["userName"].(string)
if ok {
user.Username = userName
}
id, ok := resource["id"].(string)
if ok {
user.ID = id
}
active, ok := resource["active"].(bool)
if ok {
user.Active = active
}
verified, ok := resource["verified"].(bool)
if ok {
user.Verified = verified
}
name, ok := resource["name"].(map[string]interface{})
if ok {
givenName, ok := name["givenName"].(string)
if ok {
user.Name.GivenName = givenName
}
familyName, ok := name["familyName"].(string)
if ok {
user.Name.FamilyName = familyName
}
}
emailInterfaces, ok := resource["emails"].([]interface{})
if ok {
for _, emailInterface := range emailInterfaces {
emailMap, ok := emailInterface.(map[string]interface{})
if ok {
email, ok := emailMap["value"].(string)
if ok {
user.Emails = append(user.Emails, email)
}
}
}
}
return user, nil
}