2016-03-23 20:53:09 +01:00
|
|
|
package fastly
|
|
|
|
|
|
|
|
import (
|
2016-07-21 21:10:33 +02:00
|
|
|
"bytes"
|
2016-04-20 20:43:54 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ajg/form"
|
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2016-03-23 20:53:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// APIKeyEnvVar is the name of the environment variable where the Fastly API
|
|
|
|
// key should be read from.
|
|
|
|
const APIKeyEnvVar = "FASTLY_API_KEY"
|
|
|
|
|
|
|
|
// APIKeyHeader is the name of the header that contains the Fastly API key.
|
|
|
|
const APIKeyHeader = "Fastly-Key"
|
|
|
|
|
|
|
|
// DefaultEndpoint is the default endpoint for Fastly. Since Fastly does not
|
|
|
|
// support an on-premise solution, this is likely to always be the default.
|
|
|
|
const DefaultEndpoint = "https://api.fastly.com"
|
|
|
|
|
|
|
|
// ProjectURL is the url for this library.
|
|
|
|
var ProjectURL = "github.com/sethvargo/go-fastly"
|
|
|
|
|
|
|
|
// ProjectVersion is the version of this library.
|
2016-04-20 20:43:54 +02:00
|
|
|
var ProjectVersion = "0.2"
|
2016-03-23 20:53:09 +01:00
|
|
|
|
|
|
|
// UserAgent is the user agent for this particular client.
|
|
|
|
var UserAgent = fmt.Sprintf("FastlyGo/%s (+%s; %s)",
|
2016-04-20 20:43:54 +02:00
|
|
|
ProjectVersion, ProjectURL, runtime.Version())
|
2016-03-23 20:53:09 +01:00
|
|
|
|
|
|
|
// Client is the main entrypoint to the Fastly golang API library.
|
|
|
|
type Client struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Address is the address of Fastly's API endpoint.
|
|
|
|
Address string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// HTTPClient is the HTTP client to use. If one is not provided, a default
|
|
|
|
// client will be used.
|
|
|
|
HTTPClient *http.Client
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// apiKey is the Fastly API key to authenticate requests.
|
|
|
|
apiKey string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// url is the parsed URL from Address
|
|
|
|
url *url.URL
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultClient instantiates a new Fastly API client. This function requires
|
|
|
|
// the environment variable `FASTLY_API_KEY` is set and contains a valid API key
|
|
|
|
// to authenticate with Fastly.
|
|
|
|
func DefaultClient() *Client {
|
2016-04-20 20:43:54 +02:00
|
|
|
client, err := NewClient(os.Getenv(APIKeyEnvVar))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return client
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 20:30:54 +02:00
|
|
|
// NewClient creates a new API client with the given key and the default API
|
|
|
|
// endpoint. Because Fastly allows some requests without an API key, this
|
|
|
|
// function will not error if the API token is not supplied. Attempts to make a
|
|
|
|
// request that requires an API key will return a 403 response.
|
2016-03-23 20:53:09 +01:00
|
|
|
func NewClient(key string) (*Client, error) {
|
2017-04-18 20:30:54 +02:00
|
|
|
return NewClientForEndpoint(key, DefaultEndpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientForEndpoint creates a new API client with the given key and API
|
|
|
|
// endpoint. Because Fastly allows some requests without an API key, this
|
|
|
|
// function will not error if the API token is not supplied. Attempts to make a
|
|
|
|
// request that requires an API key will return a 403 response.
|
|
|
|
func NewClientForEndpoint(key string, endpoint string) (*Client, error) {
|
|
|
|
client := &Client{apiKey: key, Address: endpoint}
|
2016-04-20 20:43:54 +02:00
|
|
|
return client.init()
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) init() (*Client, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
u, err := url.Parse(c.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.url = u
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
if c.HTTPClient == nil {
|
|
|
|
c.HTTPClient = cleanhttp.DefaultClient()
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
return c, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get issues an HTTP GET request.
|
|
|
|
func (c *Client) Get(p string, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
return c.Request("GET", p, ro)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Head issues an HTTP HEAD request.
|
|
|
|
func (c *Client) Head(p string, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
return c.Request("HEAD", p, ro)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post issues an HTTP POST request.
|
|
|
|
func (c *Client) Post(p string, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
return c.Request("POST", p, ro)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostForm issues an HTTP POST request with the given interface form-encoded.
|
|
|
|
func (c *Client) PostForm(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
return c.RequestForm("POST", p, i, ro)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put issues an HTTP PUT request.
|
|
|
|
func (c *Client) Put(p string, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
return c.Request("PUT", p, ro)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutForm issues an HTTP PUT request with the given interface form-encoded.
|
|
|
|
func (c *Client) PutForm(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
return c.RequestForm("PUT", p, i, ro)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete issues an HTTP DELETE request.
|
|
|
|
func (c *Client) Delete(p string, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
return c.Request("DELETE", p, ro)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request makes an HTTP request against the HTTPClient using the given verb,
|
|
|
|
// Path, and request options.
|
|
|
|
func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
req, err := c.RawRequest(verb, p, ro)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
resp, err := checkResp(c.HTTPClient.Do(req))
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
return resp, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RequestForm makes an HTTP request with the given interface being encoded as
|
|
|
|
// form data.
|
|
|
|
func (c *Client) RequestForm(verb, p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
if ro == nil {
|
|
|
|
ro = new(RequestOptions)
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
if ro.Headers == nil {
|
|
|
|
ro.Headers = make(map[string]string)
|
|
|
|
}
|
|
|
|
ro.Headers["Content-Type"] = "application/x-www-form-urlencoded"
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-07-21 21:10:33 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2017-04-18 20:30:54 +02:00
|
|
|
if err := form.NewEncoder(buf).KeepZeros(true).DelimitWith('|').Encode(i); err != nil {
|
2016-07-21 21:10:33 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
body := buf.String()
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
ro.Body = strings.NewReader(body)
|
|
|
|
ro.BodyLength = int64(len(body))
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
return c.Request(verb, p, ro)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkResp wraps an HTTP request from the default client and verifies that the
|
|
|
|
// request was successful. A non-200 request returns an error formatted to
|
|
|
|
// included any validation problems or otherwise.
|
|
|
|
func checkResp(resp *http.Response, err error) (*http.Response, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
// If the err is already there, there was an error higher up the chain, so
|
|
|
|
// just return that.
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case 200, 201, 202, 204, 205, 206:
|
|
|
|
return resp, nil
|
|
|
|
default:
|
|
|
|
return resp, NewHTTPError(resp)
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// decodeJSON is used to decode an HTTP response body into an interface as JSON.
|
|
|
|
func decodeJSON(out interface{}, body io.ReadCloser) error {
|
2016-04-20 20:43:54 +02:00
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
var parsed interface{}
|
|
|
|
dec := json.NewDecoder(body)
|
|
|
|
if err := dec.Decode(&parsed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
|
|
|
mapToHTTPHeaderHookFunc(),
|
|
|
|
stringToTimeHookFunc(),
|
|
|
|
),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: out,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return decoder.Decode(parsed)
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|