38 lines
773 B
Go
38 lines
773 B
Go
package slapshotapi
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
|
|
"github.com/pkg/errors"
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
type SlapAPI struct {
|
|
client *http.Client
|
|
ratelimiter *rate.Limiter
|
|
mu sync.Mutex
|
|
maxTokens int
|
|
key string
|
|
env string
|
|
}
|
|
|
|
func NewSlapAPIClient(cfg *Config) (*SlapAPI, error) {
|
|
if cfg == nil {
|
|
return nil, errors.New("config cannot be nil")
|
|
}
|
|
if cfg.Environment != "api" && cfg.Environment != "staging" {
|
|
return nil, errors.New("invalid env specified, must be 'api' or 'staging'")
|
|
}
|
|
rl := rate.NewLimiter(rate.Inf, 10)
|
|
client := &SlapAPI{
|
|
client: http.DefaultClient,
|
|
ratelimiter: rl,
|
|
mu: sync.Mutex{},
|
|
maxTokens: 10,
|
|
key: cfg.Key,
|
|
env: cfg.Environment,
|
|
}
|
|
return client, nil
|
|
}
|