45 lines
978 B
Go
45 lines
978 B
Go
package slapshotapi
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type endpoint interface {
|
|
path() string
|
|
method() string
|
|
}
|
|
|
|
func (c *SlapAPI) request(
|
|
ctx context.Context,
|
|
ep endpoint,
|
|
) ([]byte, error) {
|
|
baseurl := fmt.Sprintf("https://%s.slapshot.gg%s", c.env, ep.path())
|
|
req, err := http.NewRequest(ep.method(), baseurl, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "http.NewRequest")
|
|
}
|
|
req.Header.Add("accept", "application/json")
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.key))
|
|
res, err := c.do(ctx, req)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "http.DefaultClient.Do")
|
|
}
|
|
if res.StatusCode != 200 {
|
|
return nil, errors.New(fmt.Sprintf("Error making request: %v", res.StatusCode))
|
|
}
|
|
body, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "io.ReadAll")
|
|
}
|
|
err = res.Body.Close()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "resp.Body.Close")
|
|
}
|
|
return body, nil
|
|
}
|