Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/labd/contentstack-go-sdk

go 1.20
go 1.24.0

toolchain go1.24.3

require golang.org/x/time v0.13.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI=
golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
44 changes: 37 additions & 7 deletions management/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io/ioutil"
"net/http"
"net/url"

"golang.org/x/time/rate"
)

type Auth struct {
Expand All @@ -19,6 +21,8 @@ type ClientConfig struct {
HTTPClient *http.Client
AuthToken string
OrganizationUID string
RateLimit float64
RateBurst int
}

type UserCredentials struct {
Expand All @@ -27,9 +31,10 @@ type UserCredentials struct {
}

type Client struct {
authToken string
baseURL *url.URL
httpClient *http.Client
authToken string
baseURL *url.URL
httpClient *http.Client
rateLimiter *rate.Limiter
}

type ErrorMessage struct {
Expand Down Expand Up @@ -60,17 +65,36 @@ func NewClient(cfg ClientConfig) (*Client, error) {
httpClient = &http.Client{}
}

rateLimit := cfg.RateLimit
if rateLimit <= 0 {
rateLimit = 10.0
}

rateBurst := cfg.RateBurst
if rateBurst <= 0 {
rateBurst = 10
}

rateLimiter := rate.NewLimiter(rate.Limit(rateLimit), rateBurst)

client := &Client{
baseURL: url,
authToken: cfg.AuthToken,
httpClient: httpClient,
baseURL: url,
authToken: cfg.AuthToken,
httpClient: httpClient,
rateLimiter: rateLimiter,
}

return client, nil
}

func NewClientWithToken(auth *Auth) *Client {
return &Client{}
rateLimiter := rate.NewLimiter(rate.Limit(10.0), 10)

return &Client{
authToken: auth.AuthToken,
httpClient: &http.Client{},
rateLimiter: rateLimiter,
}
}

func (c *Client) head(ctx context.Context, path string, queryParams url.Values, headers http.Header) (*http.Response, error) {
Expand Down Expand Up @@ -102,6 +126,12 @@ func (c *Client) createEndpoint(p string) (*url.URL, error) {
}

func (c *Client) execute(ctx context.Context, method string, path string, params url.Values, headers http.Header, body io.Reader) (*http.Response, error) {
if c.rateLimiter != nil {
if err := c.rateLimiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("rate limiting wait failed: %w", err)
}
}

endpoint, err := c.createEndpoint(path)
if err != nil {
return nil, err
Expand Down