Skip to content

Commit

Permalink
Merge pull request #4389 from nickmango/bug/user-service-search
Browse files Browse the repository at this point in the history
[#4373] Bug/User Service update
  • Loading branch information
nickmango committed Jul 17, 2024
2 parents a766ca4 + 1972fd6 commit 5bc09f5
Showing 1 changed file with 116 additions and 27 deletions.
143 changes: 116 additions & 27 deletions cla-backend-go/v2/user-service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"strings"

"github.com/communitybridge/easycla/cla-backend-go/utils"
"github.com/sirupsen/logrus"

log "github.com/communitybridge/easycla/cla-backend-go/logging"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/communitybridge/easycla/cla-backend-go/token"
"github.com/communitybridge/easycla/cla-backend-go/v2/user-service/client"
"github.com/communitybridge/easycla/cla-backend-go/v2/user-service/client/bulk"
"github.com/communitybridge/easycla/cla-backend-go/v2/user-service/client/user"
"github.com/communitybridge/easycla/cla-backend-go/v2/user-service/models"
runtimeClient "github.com/go-openapi/runtime/client"
Expand Down Expand Up @@ -76,19 +74,57 @@ func (usc *Client) GetUsersByUsernames(lfUsernames []string) ([]*models.User, er
return nil, err
}

params := bulk.NewSearchBulkParams()
params.SearchBulk = &models.SearchBulk{
List: lfUsernames,
url := fmt.Sprintf("https://%s/user-service/v1/bulk", usc.apiGwURL)
var requestBody = models.SearchBulk{
Type: aws.String("username"),
List: lfUsernames,
}
clientAuth := runtimeClient.BearerToken(tok)
result, err := usc.cl.Bulk.SearchBulk(params, clientAuth)

requestBodyBytes, err := json.Marshal(requestBody)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem marshalling the request body")
return nil, err
}

request, err := http.NewRequest("POST", url, strings.NewReader(string(requestBodyBytes)))

if err != nil {
log.WithFields(f).WithError(err).Warn("problem building new request")
return nil, err
}

request.Header.Set("X-API-KEY", usc.apiKey)
request.Header.Set("Authorization", "Bearer "+tok)
request.Header.Set("Content-Type", "application/json")

response, err := http.DefaultClient.Do(request)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem searching user")
return nil, err
}

defer func() {
closeErr := response.Body.Close()
if closeErr != nil {
log.WithFields(f).WithError(closeErr).Warn("error closing body")
}

}()

data, err := io.ReadAll(response.Body)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem with the bulk search")
log.WithFields(f).WithError(err).Warn("problem decoding the user response")
return nil, err
}

return result.Payload.Data, nil
//return as []*models.User
userList, err := getUsers(data)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem processing the user response")
return nil, err
}

return userList, nil
}

// GetUserByUsername returns user by lfUsername
Expand Down Expand Up @@ -189,25 +225,51 @@ func (usc *Client) ListUsersByUsername(lfUsername string) (*models.User, error)
log.WithFields(f).WithError(err).Warn("problem obtaining token")
return nil, err
}
clientAuth := runtimeClient.BearerToken(tok)

params := &user.FindUsersParams{
Username: &lfUsername,
Context: utils.NewContext(),
url := fmt.Sprintf("https://%s/user-service/v1/users?username=%s", usc.apiGwURL, lfUsername)
request, err := http.NewRequest("GET", url, nil)

if err != nil {
log.WithFields(f).WithError(err).Warn("problem building new request")
return nil, err
}
result, err := usc.cl.User.FindUsers(params, clientAuth)

request.Header.Set("X-API-KEY", usc.apiKey)
request.Header.Set("Authorization", "Bearer "+tok)
request.Header.Set("Content-Type", "application/json")

response, err := http.DefaultClient.Do(request)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem finding user by lfUsername")
log.WithFields(f).WithError(err).Warn("problem searching user")
return nil, err
}
users := result.Payload.Data

if len(users) == 0 {
defer func() {
closeErr := response.Body.Close()
if closeErr != nil {
log.WithFields(f).WithError(closeErr).Warn("error closing body")
}
}()

data, err := io.ReadAll(response.Body)

if err != nil {
log.WithFields(f).WithError(err).Warn("problem decoding the user response")
return nil, err
}

userList, err := getUsers(data)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem processing the user response")
return nil, err
}

if len(userList) == 0 {
log.WithFields(f).Debug("get by lfUsername returned no results")
return nil, ErrUserNotFound
}

return users[0], nil
return userList[0], nil
}

// SearchUsersByEmail returns a single user based on the email parameter
Expand All @@ -222,24 +284,51 @@ func (usc *Client) SearchUsersByEmail(email string) (*models.User, error) {
log.WithFields(f).WithError(err).Warn("problem obtaining token")
return nil, err
}
clientAuth := runtimeClient.BearerToken(tok)

params := &user.FindUsersParams{
Email: &email,
Context: context.Background(),
url := fmt.Sprintf("https://%s/user-service/v1/users?email=%s", usc.apiGwURL, email)
request, err := http.NewRequest("GET", url, nil)

if err != nil {
log.WithFields(f).WithError(err).Warn("problem building new request")
return nil, err
}

request.Header.Set("X-API-KEY", usc.apiKey)
request.Header.Set("Authorization", "Bearer "+tok)
request.Header.Set("Content-Type", "application/json")

response, err := http.DefaultClient.Do(request)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem searching user")
return nil, err
}
result, err := usc.cl.User.FindUsers(params, clientAuth)

defer func() {
closeErr := response.Body.Close()
if closeErr != nil {
log.WithFields(f).WithError(closeErr).Warn("error closing body")
}
}()

data, err := io.ReadAll(response.Body)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem finding user by email")
log.WithFields(f).WithError(err).Warn("problem decoding the user response")
return nil, err
}
users := result.Payload.Data

if len(users) == 0 {
userList, err := getUsers(data)
if err != nil {
log.WithFields(f).WithError(err).Warn("problem processing the user response")
return nil, err
}

if len(userList) == 0 {
log.WithFields(f).Debug("get by lfUsername returned no results")
return nil, ErrUserNotFound
}
return users[0], nil

return userList[0], nil

}

func getUsers(body []byte) ([]*models.User, error) {
Expand Down

0 comments on commit 5bc09f5

Please sign in to comment.