Skip to content

Commit

Permalink
Adding pagination to the client library (#147)
Browse files Browse the repository at this point in the history
* Adding iterators to the list requests to MSTeams

* Fixing linter error
  • Loading branch information
jespino authored May 25, 2023
1 parent 4174d93 commit b738098
Showing 1 changed file with 55 additions and 16 deletions.
71 changes: 55 additions & 16 deletions server/msteams/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/microsoftgraph/msgraph-beta-sdk-go/sites"
"github.com/microsoftgraph/msgraph-beta-sdk-go/teams"
"github.com/microsoftgraph/msgraph-beta-sdk-go/users"
msgraphcore "github.com/microsoftgraph/msgraph-sdk-go-core"
a "github.com/microsoftgraph/msgraph-sdk-go-core/authentication"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -521,13 +522,21 @@ func (tc *ClientImpl) subscribe(baseURL, webhookSecret, resource, changeType str
return nil, NormalizeGraphAPIError(err)
}

pageIterator, err := msgraphcore.NewPageIterator[models.Subscriptionable](subscriptionsRes, tc.client.GetAdapter(), models.CreateSubscriptionCollectionResponseFromDiscriminatorValue)
if err != nil {
return nil, NormalizeGraphAPIError(err)
}

var existingSubscription models.Subscriptionable
for _, s := range subscriptionsRes.GetValue() {
subscription := s
err = pageIterator.Iterate(context.Background(), func(subscription models.Subscriptionable) bool {
if subscription.GetResource() != nil && (*subscription.GetResource() == resource || *subscription.GetResource()+"?model=B" == resource) {
existingSubscription = subscription
break
return false
}
return true
})
if err != nil {
return nil, NormalizeGraphAPIError(err)
}

lifecycleNotificationURL := baseURL + "lifecycle"
Expand Down Expand Up @@ -1178,24 +1187,35 @@ func (tc *ClientImpl) ListUsers() ([]User, error) {
return nil, NormalizeGraphAPIError(err)
}

users := make([]User, len(r.GetValue()))
for i, u := range r.GetValue() {
pageIterator, err := msgraphcore.NewPageIterator[models.Userable](r, tc.client.GetAdapter(), models.CreateUserCollectionResponseFromDiscriminatorValue)
if err != nil {
return nil, NormalizeGraphAPIError(err)
}

users := []User{}
err = pageIterator.Iterate(context.Background(), func(u models.Userable) bool {
displayName := ""
if u.GetDisplayName() != nil {
displayName = *u.GetDisplayName()
}

users[i] = User{
user := User{
DisplayName: displayName,
ID: *u.GetId(),
}

if u.GetMail() != nil {
users[i].Mail = strings.ToLower(*u.GetMail())
user.Mail = strings.ToLower(*u.GetMail())
} else if u.GetUserPrincipalName() != nil {
users[i].Mail = strings.ToLower(*u.GetUserPrincipalName())
user.Mail = strings.ToLower(*u.GetUserPrincipalName())
}
users = append(users, user)
return true
})
if err != nil {
return nil, NormalizeGraphAPIError(err)
}

return users, nil
}

Expand All @@ -1210,9 +1230,14 @@ func (tc *ClientImpl) ListTeams() ([]Team, error) {
if err != nil {
return nil, NormalizeGraphAPIError(err)
}
teams := make([]Team, len(r.GetValue()))

for i, t := range r.GetValue() {
pageIterator, err := msgraphcore.NewPageIterator[models.Teamable](r, tc.client.GetAdapter(), models.CreateTeamCollectionResponseFromDiscriminatorValue)
if err != nil {
return nil, NormalizeGraphAPIError(err)
}

teams := []Team{}
err = pageIterator.Iterate(context.Background(), func(t models.Teamable) bool {
description := ""
if t.GetDescription() != nil {
description = *t.GetDescription()
Expand All @@ -1223,11 +1248,15 @@ func (tc *ClientImpl) ListTeams() ([]Team, error) {
displayName = *t.GetDisplayName()
}

teams[i] = Team{
teams = append(teams, Team{
DisplayName: displayName,
Description: description,
ID: *t.GetId(),
}
})
return true
})
if err != nil {
return nil, NormalizeGraphAPIError(err)
}
return teams, nil
}
Expand All @@ -1243,8 +1272,14 @@ func (tc *ClientImpl) ListChannels(teamID string) ([]Channel, error) {
if err != nil {
return nil, NormalizeGraphAPIError(err)
}
channels := make([]Channel, len(r.GetValue()))
for i, c := range r.GetValue() {

pageIterator, err := msgraphcore.NewPageIterator[models.Channelable](r, tc.client.GetAdapter(), models.CreateChannelCollectionResponseFromDiscriminatorValue)
if err != nil {
return nil, NormalizeGraphAPIError(err)
}

channels := []Channel{}
err = pageIterator.Iterate(context.Background(), func(c models.Channelable) bool {
description := ""
if c.GetDescription() != nil {
description = *c.GetDescription()
Expand All @@ -1255,11 +1290,15 @@ func (tc *ClientImpl) ListChannels(teamID string) ([]Channel, error) {
displayName = *c.GetDisplayName()
}

channels[i] = Channel{
channels = append(channels, Channel{
DisplayName: displayName,
Description: description,
ID: *c.GetId(),
}
})
return true
})
if err != nil {
return nil, NormalizeGraphAPIError(err)
}
return channels, nil
}
Expand Down

0 comments on commit b738098

Please sign in to comment.