2020-08-13 20:14:25 +00:00
|
|
|
package serverapi
|
|
|
|
|
|
|
|
import (
|
2020-10-06 03:21:20 +00:00
|
|
|
"bytes"
|
2020-08-13 20:14:25 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a client aimed at a specific ServerAPI endpoint
|
|
|
|
type Client struct {
|
2020-10-06 03:21:20 +00:00
|
|
|
endpoint string
|
|
|
|
http *http.Client
|
|
|
|
token string
|
2020-08-13 20:14:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 03:21:20 +00:00
|
|
|
// ClientOption is options for a Client
|
|
|
|
type ClientOption func(c *Client)
|
|
|
|
|
|
|
|
var defaultHeader = http.Header{
|
|
|
|
"Content-Type": []string{"application/json; charset=utf-8"},
|
|
|
|
"Accept": []string{"application/json; charset=utf-8"},
|
2020-08-13 20:14:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 03:21:20 +00:00
|
|
|
// NewClient returns a new Client for making ServerAPI requests
|
|
|
|
func NewClient(endpoint string, opts ...ClientOption) *Client {
|
|
|
|
endpoint = strings.TrimSuffix(endpoint, "/")
|
|
|
|
c := &Client{
|
|
|
|
endpoint: endpoint,
|
|
|
|
http: http.DefaultClient,
|
|
|
|
token: "",
|
2020-08-13 20:14:25 +00:00
|
|
|
}
|
2020-10-06 03:21:20 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(c)
|
2020-08-13 20:14:25 +00:00
|
|
|
}
|
2020-10-06 03:21:20 +00:00
|
|
|
return c
|
|
|
|
}
|
2020-08-13 20:14:25 +00:00
|
|
|
|
2020-10-06 03:21:20 +00:00
|
|
|
// WithHTTP is a ClientOption for setting the http.Client of a Client
|
|
|
|
func WithHTTP(http *http.Client) ClientOption {
|
|
|
|
return func(c *Client) {
|
|
|
|
c.http = http
|
2020-08-13 20:14:25 +00:00
|
|
|
}
|
2020-10-06 03:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithToken is a ClientOption for setting the token of a Client
|
|
|
|
func WithToken(token string) ClientOption {
|
|
|
|
return func(c *Client) {
|
|
|
|
c.token = token
|
2020-08-13 20:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 03:21:20 +00:00
|
|
|
func (c *Client) jsonGET(endpoint string, obj interface{}) error {
|
|
|
|
req, err := c.newRequest(endpoint, http.MethodGet, nil)
|
2020-08-13 20:14:25 +00:00
|
|
|
if err != nil {
|
2020-10-06 03:21:20 +00:00
|
|
|
return err
|
2020-08-13 20:14:25 +00:00
|
|
|
}
|
2020-10-06 03:21:20 +00:00
|
|
|
res, err := c.http.Do(req)
|
2020-08-13 20:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("sending request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading body: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Unmarshal(body, obj)
|
|
|
|
}
|
2020-10-06 03:21:20 +00:00
|
|
|
|
|
|
|
func (c *Client) jsonPOST(endpoint string, obj interface{}) (int, error) {
|
|
|
|
body, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := c.newRequest(endpoint, http.MethodPost, body)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("sending request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.StatusCode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) newRequest(endpoint, method string, body []byte) (*http.Request, error) {
|
|
|
|
req, err := http.NewRequest(method, endpoint, bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("new JSON request: %v", err)
|
|
|
|
}
|
|
|
|
req.Header = defaultHeader
|
|
|
|
req.Header.Add("X-ServerAPI-Token", c.token)
|
|
|
|
return req, nil
|
|
|
|
}
|