2015-11-27 20:46:13 +08:00
|
|
|
// Package rest implements a simple REST wrapper
|
|
|
|
package rest
|
2015-10-05 05:08:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-11-28 02:25:52 +08:00
|
|
|
"io/ioutil"
|
2015-10-05 05:08:31 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client contains the info to sustain the API
|
|
|
|
type Client struct {
|
2015-11-27 20:46:13 +08:00
|
|
|
c *http.Client
|
|
|
|
rootURL string
|
|
|
|
errorHandler func(resp *http.Response) error
|
2015-11-28 02:25:52 +08:00
|
|
|
headers map[string]string
|
2015-10-05 05:08:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient takes an oauth http.Client and makes a new api instance
|
2015-11-28 02:25:52 +08:00
|
|
|
func NewClient(c *http.Client) *Client {
|
|
|
|
api := &Client{
|
2015-11-27 20:46:13 +08:00
|
|
|
c: c,
|
|
|
|
errorHandler: defaultErrorHandler,
|
2015-11-28 02:25:52 +08:00
|
|
|
headers: make(map[string]string),
|
2015-10-05 05:08:31 +08:00
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
api.SetHeader("User-Agent", fs.UserAgent)
|
|
|
|
return api
|
2015-10-05 05:08:31 +08:00
|
|
|
}
|
|
|
|
|
2015-11-28 02:25:52 +08:00
|
|
|
// defaultErrorHandler doesn't attempt to parse the http body, just
|
|
|
|
// returns it in the error message
|
2015-11-27 20:46:13 +08:00
|
|
|
func defaultErrorHandler(resp *http.Response) (err error) {
|
2015-11-29 02:13:08 +08:00
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
2015-11-28 02:25:52 +08:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("HTTP error %v (%v) returned body: %q", resp.StatusCode, resp.Status, body)
|
2015-11-27 20:46:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetErrorHandler sets the handler to decode an error response when
|
|
|
|
// the HTTP status code is not 2xx. The handler should close resp.Body.
|
2015-11-28 02:25:52 +08:00
|
|
|
func (api *Client) SetErrorHandler(fn func(resp *http.Response) error) *Client {
|
2015-11-27 20:46:13 +08:00
|
|
|
api.errorHandler = fn
|
2015-11-28 02:25:52 +08:00
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRoot sets the default root URL
|
|
|
|
func (api *Client) SetRoot(RootURL string) *Client {
|
|
|
|
api.rootURL = RootURL
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeader sets a header for all requests
|
|
|
|
func (api *Client) SetHeader(key, value string) *Client {
|
|
|
|
api.headers[key] = value
|
|
|
|
return api
|
2015-11-27 20:46:13 +08:00
|
|
|
}
|
|
|
|
|
2015-10-05 05:08:31 +08:00
|
|
|
// Opts contains parameters for Call, CallJSON etc
|
|
|
|
type Opts struct {
|
|
|
|
Method string
|
|
|
|
Path string
|
|
|
|
Absolute bool // Path is absolute
|
|
|
|
Body io.Reader
|
|
|
|
NoResponse bool // set to close Body
|
|
|
|
ContentType string
|
|
|
|
ContentLength *int64
|
|
|
|
ContentRange string
|
2015-10-30 16:40:14 +08:00
|
|
|
ExtraHeaders map[string]string
|
2015-11-28 02:25:52 +08:00
|
|
|
UserName string // username for Basic Auth
|
|
|
|
Password string // password for Basic Auth
|
2015-10-05 05:08:31 +08:00
|
|
|
}
|
|
|
|
|
2015-10-30 16:40:14 +08:00
|
|
|
// DecodeJSON decodes resp.Body into result
|
|
|
|
func DecodeJSON(resp *http.Response, result interface{}) (err error) {
|
2015-11-29 02:13:08 +08:00
|
|
|
defer fs.CheckClose(resp.Body, &err)
|
2015-10-05 05:08:31 +08:00
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
return decoder.Decode(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call makes the call and returns the http.Response
|
|
|
|
//
|
|
|
|
// if err != nil then resp.Body will need to be closed
|
|
|
|
//
|
|
|
|
// it will return resp if at all possible, even if err is set
|
|
|
|
func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {
|
|
|
|
if opts == nil {
|
|
|
|
return nil, fmt.Errorf("call() called with nil opts")
|
|
|
|
}
|
|
|
|
var url string
|
|
|
|
if opts.Absolute {
|
|
|
|
url = opts.Path
|
|
|
|
} else {
|
2015-11-28 02:25:52 +08:00
|
|
|
if api.rootURL == "" {
|
|
|
|
return nil, fmt.Errorf("RootURL not set")
|
|
|
|
}
|
2015-11-27 20:46:13 +08:00
|
|
|
url = api.rootURL + opts.Path
|
2015-10-05 05:08:31 +08:00
|
|
|
}
|
|
|
|
req, err := http.NewRequest(opts.Method, url, opts.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
headers := make(map[string]string)
|
|
|
|
// Set default headers
|
|
|
|
for k, v := range api.headers {
|
|
|
|
headers[k] = v
|
|
|
|
}
|
2015-10-05 05:08:31 +08:00
|
|
|
if opts.ContentType != "" {
|
2015-11-28 02:25:52 +08:00
|
|
|
headers["Content-Type"] = opts.ContentType
|
2015-10-05 05:08:31 +08:00
|
|
|
}
|
|
|
|
if opts.ContentLength != nil {
|
|
|
|
req.ContentLength = *opts.ContentLength
|
|
|
|
}
|
|
|
|
if opts.ContentRange != "" {
|
2015-11-28 02:25:52 +08:00
|
|
|
headers["Content-Range"] = opts.ContentRange
|
2015-10-05 05:08:31 +08:00
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
// Set any extra headers
|
2015-10-30 16:40:14 +08:00
|
|
|
if opts.ExtraHeaders != nil {
|
|
|
|
for k, v := range opts.ExtraHeaders {
|
2015-11-28 02:25:52 +08:00
|
|
|
headers[k] = v
|
2015-10-30 16:40:14 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
// Now set the headers
|
|
|
|
for k, v := range headers {
|
|
|
|
req.Header.Add(k, v)
|
|
|
|
}
|
|
|
|
if opts.UserName != "" || opts.Password != "" {
|
|
|
|
req.SetBasicAuth(opts.UserName, opts.Password)
|
|
|
|
}
|
2015-10-05 05:08:31 +08:00
|
|
|
resp, err = api.c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
2015-11-27 20:46:13 +08:00
|
|
|
return resp, api.errorHandler(resp)
|
2015-10-05 05:08:31 +08:00
|
|
|
}
|
|
|
|
if opts.NoResponse {
|
|
|
|
return resp, resp.Body.Close()
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:40:14 +08:00
|
|
|
// CallJSON runs Call and decodes the body as a JSON object into response (if not nil)
|
2015-10-05 05:08:31 +08:00
|
|
|
//
|
|
|
|
// If request is not nil then it will be JSON encoded as the body of the request
|
|
|
|
//
|
|
|
|
// It will return resp if at all possible, even if err is set
|
|
|
|
func (api *Client) CallJSON(opts *Opts, request interface{}, response interface{}) (resp *http.Response, err error) {
|
|
|
|
// Set the body up as a JSON object if required
|
|
|
|
if opts.Body == nil && request != nil {
|
|
|
|
body, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var newOpts = *opts
|
|
|
|
newOpts.Body = bytes.NewBuffer(body)
|
|
|
|
newOpts.ContentType = "application/json"
|
|
|
|
opts = &newOpts
|
|
|
|
}
|
|
|
|
resp, err = api.Call(opts)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
2015-11-28 02:25:52 +08:00
|
|
|
if response == nil || opts.NoResponse {
|
2015-10-30 16:40:14 +08:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
err = DecodeJSON(resp, response)
|
2015-10-05 05:08:31 +08:00
|
|
|
return resp, err
|
|
|
|
}
|