added slapapi
This commit is contained in:
37
pkg/slapshotapi/client.go
Normal file
37
pkg/slapshotapi/client.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user