Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit 681b1e6

Browse files
authored
Add more methods to affiliation (#15)
* Add methods to affiliation - GetIdentity - GetOrganizations - GetProfile
1 parent efde4a8 commit 681b1e6

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

affiliation/dto.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,44 @@ type AffiliationsResponse struct {
2525
} `json:"profile"`
2626
UUID string `json:"uuid"`
2727
}
28+
29+
// Enrollment ...
30+
type Enrollment struct {
31+
End time.Time `json:"end"`
32+
ID int `json:"id"`
33+
Organization struct {
34+
ID int `json:"id"`
35+
Name string `json:"name"`
36+
} `json:"organization"`
37+
OrganizationID int `json:"organization_id"`
38+
Role string `json:"role"`
39+
Start time.Time `json:"start"`
40+
UUID string `json:"uuid"`
41+
}
42+
43+
// EnrollmentsResponse ...
44+
type EnrollmentsResponse struct {
45+
Enrollments []Enrollment `json:"enrollments"`
46+
Scope string `json:"scope"`
47+
User string `json:"user"`
48+
UUID string `json:"uuid"`
49+
}
50+
51+
// Profile ...
52+
type Profile struct {
53+
IsBot int `json:"is_bot"`
54+
Name string `json:"name"`
55+
UUID string `json:"uuid"`
56+
Email string `json:"email"`
57+
Gender string `json:"gender"`
58+
GenderAcc int `json:"gender_acc"`
59+
}
60+
61+
// ProfileResponse ...
62+
type ProfileResponse struct {
63+
Enrollments []Enrollment `json:"enrollments"`
64+
Identities []Identity `json:"identities"`
65+
LastModified time.Time `json:"last_modified"`
66+
Profile Profile `json:"profile"`
67+
UUID string `json:"uuid"`
68+
}

affiliation/identity.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (a *Affiliation) AddIdentity(identity *Identity) bool {
8484
queryParams["email"] = identity.Email
8585
queryParams["uuid"] = identity.UUID
8686

87-
endpoint := a.AffBaseURL + "/affiliation/" + url.PathEscape(a.ProjectSlug)+ "/add_identity/" + url.PathEscape(identity.Source)
87+
endpoint := a.AffBaseURL + "/affiliation/" + url.PathEscape(a.ProjectSlug) + "/add_identity/" + url.PathEscape(identity.Source)
8888
_, res, err := a.httpClient.Request(strings.TrimSpace(endpoint), "POST", headers, nil, queryParams)
8989
if err != nil {
9090
log.Println("Repository: AddIdentity: Could not insert the identity: ", err)
@@ -99,6 +99,96 @@ func (a *Affiliation) AddIdentity(identity *Identity) bool {
9999
return true
100100
}
101101

102+
// GetIdentity ...
103+
func (a *Affiliation) GetIdentity(uuid string) *Identity {
104+
if uuid == "" {
105+
log.Println("GetIdentity: uuid is empty")
106+
return nil
107+
}
108+
token, err := a.auth0Client.ValidateToken(a.Environment)
109+
if err != nil {
110+
log.Println(err)
111+
}
112+
headers := make(map[string]string, 0)
113+
headers["Content-type"] = "application/json"
114+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
115+
116+
endpoint := a.AffBaseURL + "/affiliation/get_identity/" + uuid
117+
118+
_, res, err := a.httpClient.Request(strings.TrimSpace(endpoint), "GET", headers, nil, nil)
119+
if err != nil {
120+
log.Println("GetIdentity: Could not get the identity: ", err)
121+
return nil
122+
}
123+
var identity Identity
124+
err = json.Unmarshal(res, &identity)
125+
if err != nil {
126+
log.Println("GetIdentity: failed to unmarshal identity: ", err)
127+
return nil
128+
}
129+
return &identity
130+
}
131+
132+
// GetOrganizations ...
133+
func (a *Affiliation) GetOrganizations(uuid, projectSlug string) *[]Enrollment {
134+
if uuid == "" || projectSlug == "" {
135+
return nil
136+
}
137+
token, err := a.auth0Client.ValidateToken(a.Environment)
138+
if err != nil {
139+
log.Println(err)
140+
}
141+
headers := make(map[string]string, 0)
142+
headers["Content-type"] = "application/json"
143+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
144+
145+
endpoint := a.AffBaseURL + "/affiliation/" + url.PathEscape(projectSlug) + "/enrollments/" + uuid
146+
147+
_, res, err := a.httpClient.Request(strings.TrimSpace(endpoint), "GET", headers, nil, nil)
148+
if err != nil {
149+
log.Println("GetOrganizations: Could not get the organizations: ", err)
150+
return nil
151+
}
152+
153+
var response EnrollmentsResponse
154+
err = json.Unmarshal(res, &response)
155+
if err != nil {
156+
log.Println("GetOrganizations: failed to unmarshal enrollments response: ", err)
157+
return nil
158+
}
159+
return &response.Enrollments
160+
}
161+
162+
// GetProfile ...
163+
func (a *Affiliation) GetProfile(uuid, projectSlug string) *ProfileResponse {
164+
if uuid == "" || projectSlug == "" {
165+
return nil
166+
}
167+
token, err := a.auth0Client.ValidateToken(a.Environment)
168+
if err != nil {
169+
log.Println(err)
170+
}
171+
headers := make(map[string]string, 0)
172+
headers["Content-type"] = "application/json"
173+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
174+
175+
endpoint := a.AffBaseURL + "/affiliation/" + url.PathEscape(projectSlug) + "/get_profile/" + uuid
176+
177+
_, res, err := a.httpClient.Request(strings.TrimSpace(endpoint), "GET", headers, nil, nil)
178+
if err != nil {
179+
log.Println("GetProfile: Could not get the profile: ", err)
180+
return nil
181+
}
182+
183+
var response ProfileResponse
184+
err = json.Unmarshal(res, &response)
185+
if err != nil {
186+
log.Println("GetProfile: failed to unmarshal profile response: ", err)
187+
return nil
188+
}
189+
return &response
190+
}
191+
102192
func buildServices(a *Affiliation) (httpClientProvider *http.ClientProvider, esClientProvider *elastic.ClientProvider, auth0ClientProvider *auth0.ClientProvider, err error) {
103193
esClientProvider, err = elastic.NewClientProvider(&elastic.Params{
104194
URL: a.ESCacheURL,

0 commit comments

Comments
 (0)