generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: define client interface; feat: implement rate limit decorator;
- Loading branch information
Andrei-Alexandru Dobre
committed
Jan 7, 2025
1 parent
ce11382
commit ce3c127
Showing
7 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package slackExt | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
type Client interface { | ||
GetUserInfo(user string) (*slack.User, error) | ||
GetUserByEmail(email string) (*slack.User, error) | ||
GetUsersContext(ctx context.Context) ([]slack.User, error) | ||
GetUserGroups(options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error) | ||
} | ||
|
||
func New(base *slack.Client) Client { | ||
return &clientRateLimit{&clientImpl{base}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package slackExt | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
type clientImpl struct { | ||
base *slack.Client | ||
} | ||
|
||
func (c *clientImpl) GetUserInfo(user string) (*slack.User, error) { | ||
return c.base.GetUserInfo(user) | ||
} | ||
|
||
func (c *clientImpl) GetUserByEmail(email string) (*slack.User, error) { | ||
return c.base.GetUserByEmail(email) | ||
} | ||
|
||
func (c *clientImpl) GetUsersContext(ctx context.Context) ([]slack.User, error) { | ||
return c.base.GetUsersContext(ctx) | ||
} | ||
|
||
func (c *clientImpl) GetUserGroups(options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error) { | ||
return c.base.GetUserGroups(options...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package slackExt | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
type clientRateLimit struct { | ||
base Client | ||
} | ||
|
||
func rateLimit[R any](f func() (R, error)) (R, error) { | ||
for { | ||
result, err := f() | ||
|
||
if err == nil { | ||
return result, nil | ||
} | ||
|
||
if rateLimitedError, ok := err.(*slack.RateLimitedError); ok { | ||
<-time.After(rateLimitedError.RetryAfter) | ||
err = nil | ||
} | ||
} | ||
} | ||
|
||
func (c *clientRateLimit) GetUserInfo(user string) (result *slack.User, err error) { | ||
return rateLimit(func() (*slack.User, error) { | ||
return c.base.GetUserInfo(user) | ||
}) | ||
} | ||
|
||
func (c *clientRateLimit) GetUserByEmail(email string) (*slack.User, error) { | ||
return c.base.GetUserByEmail(email) | ||
} | ||
|
||
func (c *clientRateLimit) GetUsersContext(ctx context.Context) ([]slack.User, error) { | ||
return rateLimit(func() ([]slack.User, error) { | ||
return c.base.GetUsersContext(ctx) | ||
}) | ||
} | ||
|
||
func (c *clientRateLimit) GetUserGroups(options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error) { | ||
return rateLimit(func() ([]slack.UserGroup, error) { | ||
return c.base.GetUserGroups(options...) | ||
}) | ||
} |