Skip to content

Commit

Permalink
Housekeeping; organize Client methods
Browse files Browse the repository at this point in the history
  • Loading branch information
momer committed May 1, 2024
1 parent 9ff9475 commit a4f0471
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions bonsai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ func WithProvisionRateLimit(l *rate.Limiter) ClientOption {
}
}

type PaginatedResponse struct {
PageNumber int `json:"page_number"`
PageSize int `json:"page_size"`
TotalRecords int `json:"total_records"`
}

type Response struct {
*http.Response

PaginatedResponse
}

type limiter = *rate.Limiter
type ClientLimiter struct {
// limiter is an embedded default rate limiter, but not exposed.
Expand All @@ -182,6 +194,23 @@ type Client struct {
userAgent string
}

func NewClient(options ...ClientOption) *Client {
client := &Client{
endpoint: BaseEndpoint,
httpClient: &http.Client{},
rateLimiter: &ClientLimiter{
limiter: rate.NewLimiter(rate.Every(DefaultClientBurstDuration), DefaultClientBurstAllowance),
provisionLimiter: rate.NewLimiter(rate.Every(ProvisionClientBurstDuration), ProvisionClientBurstAllowance),
},
}

for _, option := range options {
option(client)
}

return client
}

func (c *Client) UserAgent() string {
return c.userAgent
}
Expand Down Expand Up @@ -209,18 +238,6 @@ func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Re
return req, nil
}

type PaginatedResponse struct {
PageNumber int `json:"page_number"`
PageSize int `json:"page_size"`
TotalRecords int `json:"total_records"`
}

type Response struct {
*http.Response

PaginatedResponse
}

// Do performs an HTTP request against the API.
func (c *Client) Do(ctx context.Context, r *http.Request) (*Response, error) {
reqBuf := new(bytes.Buffer)
Expand Down Expand Up @@ -299,23 +316,6 @@ func (c *Client) Do(ctx context.Context, r *http.Request) (*Response, error) {
}
}

func NewClient(options ...ClientOption) *Client {
client := &Client{
endpoint: BaseEndpoint,
httpClient: &http.Client{},
rateLimiter: &ClientLimiter{
limiter: rate.NewLimiter(rate.Every(DefaultClientBurstDuration), DefaultClientBurstAllowance),
provisionLimiter: rate.NewLimiter(rate.Every(ProvisionClientBurstDuration), ProvisionClientBurstAllowance),
},
}

for _, option := range options {
option(client)
}

return client
}

// all loops through the next page pagination results until empty
// it allows the caller to pass a func (typically a closure) to collect
// results.
Expand Down

0 comments on commit a4f0471

Please sign in to comment.