@@ -8,10 +8,6 @@ import (
88 "net/url"
99 "strings"
1010 "time"
11-
12- "github.com/LF-Engineering/dev-analytics-libraries/auth0"
13- "github.com/LF-Engineering/dev-analytics-libraries/elastic"
14- "github.com/LF-Engineering/dev-analytics-libraries/http"
1511)
1612
1713var unknown string = "Unknown"
@@ -22,50 +18,56 @@ type Affiliations interface {
2218 AddIdentity (identity * Identity ) bool
2319}
2420
21+ // HTTPClientProvider used in connecting to remote http server
22+ type HTTPClientProvider interface {
23+ Request (url string , method string , header map [string ]string , body []byte , params map [string ]string ) (statusCode int , resBody []byte , err error )
24+ }
25+
26+ // ESClientProvider used in connecting to ES server
27+ type ESClientProvider interface {
28+ CreateDocument (index , documentID string , body []byte ) ([]byte , error )
29+ Search (index string , query map [string ]interface {}) ([]byte , error )
30+ CreateIndex (index string , body []byte ) ([]byte , error )
31+ Get (index string , query map [string ]interface {}, result interface {}) error
32+ }
33+
34+ // SlackProvider ...
35+ type SlackProvider interface {
36+ SendText (text string ) error
37+ }
38+
39+ // Auth0ClientProvider ...
40+ type Auth0ClientProvider interface {
41+ GetToken () (string , error )
42+ }
43+
2544// Affiliation struct
2645type Affiliation struct {
27- AffBaseURL string
28- ProjectSlug string
29- ESCacheURL string
30- ESCacheUsername string
31- ESCachePassword string
32- AuthGrantType string
33- AuthClientID string
34- AuthClientSecret string
35- AuthAudience string
36- AuthURL string
37- Environment string
38- httpClient * http.ClientProvider
39- esClient * elastic.ClientProvider
40- auth0Client * auth0.ClientProvider
46+ AffBaseURL string
47+ ProjectSlug string
48+ httpClientProvider HTTPClientProvider
49+ esClientProvider ESClientProvider
50+ auth0ClientProvider Auth0ClientProvider
51+ slackProvider SlackProvider
4152}
4253
4354// NewAffiliationsClient consumes
4455// affBaseURL, projectSlug, esCacheUrl, esCacheUsername, esCachePassword, esCacheIndex, env, authGrantType, authClientID, authClientSecret, authAudience, authURL
45- func NewAffiliationsClient (affBaseURL , projectSlug , esCacheURL , esCacheUsername , esCachePassword , env , authGrantType , authClientID , authClientSecret , authAudience , authURL string ) (* Affiliation , error ) {
56+ func NewAffiliationsClient (affBaseURL string ,
57+ projectSlug string ,
58+ httpClientProvider HTTPClientProvider ,
59+ esClientProvider ESClientProvider ,
60+ auth0ClientPrivder Auth0ClientProvider ,
61+ slackProvider SlackProvider ) (* Affiliation , error ) {
4662 aff := & Affiliation {
47- AffBaseURL : affBaseURL ,
48- ProjectSlug : projectSlug ,
49- ESCacheURL : esCacheURL ,
50- ESCacheUsername : esCacheUsername ,
51- ESCachePassword : esCachePassword ,
52- AuthGrantType : authGrantType ,
53- AuthClientID : authClientID ,
54- AuthClientSecret : authClientSecret ,
55- AuthAudience : authAudience ,
56- AuthURL : authURL ,
57- Environment : env ,
58- }
59-
60- httpClientProvider , esClientProvider , auth0ClientProvider , err := buildServices (aff )
61- if err != nil {
62- return nil , err
63+ AffBaseURL : affBaseURL ,
64+ ProjectSlug : projectSlug ,
65+ httpClientProvider : httpClientProvider ,
66+ esClientProvider : esClientProvider ,
67+ auth0ClientProvider : auth0ClientPrivder ,
68+ slackProvider : slackProvider ,
6369 }
6470
65- aff .esClient = esClientProvider
66- aff .httpClient = httpClientProvider
67- aff .auth0Client = auth0ClientProvider
68-
6971 return aff , nil
7072}
7173
@@ -75,7 +77,7 @@ func (a *Affiliation) AddIdentity(identity *Identity) bool {
7577 log .Println ("AddIdentity: Identity is nil" )
7678 return false
7779 }
78- token , err := a .auth0Client . ValidateToken ( a . Environment )
80+ token , err := a .auth0ClientProvider . GetToken ( )
7981 if err != nil {
8082 log .Println (err )
8183 }
@@ -90,7 +92,7 @@ func (a *Affiliation) AddIdentity(identity *Identity) bool {
9092 queryParams ["id" ] = identity .ID
9193
9294 endpoint := a .AffBaseURL + "/affiliation/" + url .PathEscape (a .ProjectSlug ) + "/add_identity/" + url .PathEscape (identity .Source )
93- _ , res , err := a .httpClient .Request (strings .TrimSpace (endpoint ), "POST" , headers , nil , queryParams )
95+ _ , res , err := a .httpClientProvider .Request (strings .TrimSpace (endpoint ), "POST" , headers , nil , queryParams )
9496 if err != nil {
9597 log .Println ("AddIdentity: Could not insert the identity: " , err )
9698 return false
@@ -110,7 +112,7 @@ func (a *Affiliation) GetIdentity(uuid string) *Identity {
110112 log .Println ("GetIdentity: uuid is empty" )
111113 return nil
112114 }
113- token , err := a .auth0Client . ValidateToken ( a . Environment )
115+ token , err := a .auth0ClientProvider . GetToken ( )
114116 if err != nil {
115117 log .Println (err )
116118 }
@@ -119,7 +121,7 @@ func (a *Affiliation) GetIdentity(uuid string) *Identity {
119121
120122 endpoint := a .AffBaseURL + "/affiliation/get_identity/" + uuid
121123
122- _ , res , err := a .httpClient .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
124+ _ , res , err := a .httpClientProvider .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
123125 if err != nil {
124126 log .Println ("GetIdentity: Could not get the identity: " , err )
125127 return nil
@@ -138,7 +140,7 @@ func (a *Affiliation) GetOrganizations(uuid, projectSlug string) *[]Enrollment {
138140 if uuid == "" || projectSlug == "" {
139141 return nil
140142 }
141- token , err := a .auth0Client . ValidateToken ( a . Environment )
143+ token , err := a .auth0ClientProvider . GetToken ( )
142144 if err != nil {
143145 log .Println (err )
144146 }
@@ -147,7 +149,7 @@ func (a *Affiliation) GetOrganizations(uuid, projectSlug string) *[]Enrollment {
147149
148150 endpoint := a .AffBaseURL + "/affiliation/" + url .PathEscape (projectSlug ) + "/enrollments/" + uuid
149151
150- _ , res , err := a .httpClient .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
152+ _ , res , err := a .httpClientProvider .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
151153 if err != nil {
152154 log .Println ("GetOrganizations: Could not get the organizations: " , err )
153155 return nil
@@ -167,7 +169,7 @@ func (a *Affiliation) GetProfile(uuid, projectSlug string) *ProfileResponse {
167169 if uuid == "" || projectSlug == "" {
168170 return nil
169171 }
170- token , err := a .auth0Client . ValidateToken ( a . Environment )
172+ token , err := a .auth0ClientProvider . GetToken ( )
171173 if err != nil {
172174 log .Println (err )
173175 }
@@ -176,7 +178,7 @@ func (a *Affiliation) GetProfile(uuid, projectSlug string) *ProfileResponse {
176178
177179 endpoint := a .AffBaseURL + "/affiliation/" + url .PathEscape (projectSlug ) + "/get_profile/" + uuid
178180
179- _ , res , err := a .httpClient .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
181+ _ , res , err := a .httpClientProvider .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
180182 if err != nil {
181183 log .Println ("GetProfile: Could not get the profile: " , err )
182184 return nil
@@ -198,7 +200,7 @@ func (a *Affiliation) GetIdentityByUser(key string, value string) (*AffIdentity,
198200 log .Println (nilKeyOrValueErr )
199201 return nil , fmt .Errorf (nilKeyOrValueErr )
200202 }
201- token , err := a .auth0Client . ValidateToken ( a . Environment )
203+ token , err := a .auth0ClientProvider . GetToken ( )
202204 if err != nil {
203205 log .Println (err )
204206 return nil , err
@@ -207,7 +209,7 @@ func (a *Affiliation) GetIdentityByUser(key string, value string) (*AffIdentity,
207209 headers := make (map [string ]string , 0 )
208210 headers ["Authorization" ] = fmt .Sprintf ("%s %s" , "Bearer" , token )
209211 endpoint := a .AffBaseURL + "/affiliation/" + "identity/" + key + "/" + value
210- _ , res , err := a .httpClient .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
212+ _ , res , err := a .httpClientProvider .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
211213 if err != nil {
212214 log .Println ("GetIdentityByUser: Could not get the identity: " , err )
213215 return nil , err
@@ -227,7 +229,7 @@ func (a *Affiliation) GetIdentityByUser(key string, value string) (*AffIdentity,
227229 }
228230
229231 profileEndpoint := a .AffBaseURL + "/affiliation/" + url .PathEscape (a .ProjectSlug ) + "/get_profile/" + * ident .UUID
230- _ , profileRes , err := a .httpClient .Request (strings .TrimSpace (profileEndpoint ), "GET" , headers , nil , nil )
232+ _ , profileRes , err := a .httpClientProvider .Request (strings .TrimSpace (profileEndpoint ), "GET" , headers , nil , nil )
231233 if err != nil {
232234 log .Println ("GetIdentityByUser: Could not get the identity: " , err )
233235 return nil , err
@@ -286,7 +288,7 @@ func (a *Affiliation) GetProfileByUsername(username string, projectSlug string)
286288 return nil , fmt .Errorf (nilKeyOrValueErr )
287289 }
288290
289- token , err := a .auth0Client . ValidateToken ( a . Environment )
291+ token , err := a .auth0ClientProvider . GetToken ( )
290292 if err != nil {
291293 log .Println (err )
292294 return nil , err
@@ -295,7 +297,7 @@ func (a *Affiliation) GetProfileByUsername(username string, projectSlug string)
295297 headers := make (map [string ]string , 0 )
296298 headers ["Authorization" ] = fmt .Sprintf ("%s %s" , "Bearer" , token )
297299 endpoint := a .AffBaseURL + "/affiliation/" + url .PathEscape (projectSlug ) + "/get_profile_by_username/" + url .PathEscape (username )
298- statusCode , res , err := a .httpClient .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
300+ statusCode , res , err := a .httpClientProvider .Request (strings .TrimSpace (endpoint ), "GET" , headers , nil , nil )
299301 if err != nil {
300302 log .Println ("GetProfileByUsername: Could not get the profile: " , err )
301303 return nil , err
@@ -377,23 +379,3 @@ func (a *Affiliation) getUserOrg(enrollments []*Enrollments) *string {
377379
378380 return & result
379381}
380-
381- func buildServices (a * Affiliation ) (httpClientProvider * http.ClientProvider , esClientProvider * elastic.ClientProvider , auth0ClientProvider * auth0.ClientProvider , err error ) {
382- esClientProvider , err = elastic .NewClientProvider (& elastic.Params {
383- URL : a .ESCacheURL ,
384- Username : a .ESCacheUsername ,
385- Password : a .ESCachePassword ,
386- })
387- if err != nil {
388- return
389- }
390-
391- httpClientProvider = http .NewClientProvider (time .Minute )
392-
393- auth0ClientProvider , err = auth0 .NewAuth0Client (a .ESCacheURL , a .ESCacheUsername , a .ESCachePassword , a .Environment , a .AuthGrantType , a .AuthClientID , a .AuthClientSecret , a .AuthAudience , a .AuthURL )
394- if err != nil {
395- return
396- }
397-
398- return
399- }
0 commit comments