| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"os" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"github.com/ajg/form" |
| 15 |
"github.com/google/jsonapi" |
| 16 |
"github.com/hashicorp/go-cleanhttp" |
| 17 |
"github.com/mitchellh/mapstructure" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
const APIKeyEnvVar = "FASTLY_API_KEY" |
| 23 |
|
| 24 |
|
| 25 |
const APIKeyHeader = "Fastly-Key" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
const DefaultEndpoint = "https://api.fastly.com" |
| 30 |
|
| 31 |
|
| 32 |
const RealtimeStatsEndpoint = "https://rt.fastly.com" |
| 33 |
|
| 34 |
|
| 35 |
var ProjectURL = "github.com/fastly/go-fastly" |
| 36 |
|
| 37 |
|
| 38 |
var ProjectVersion = "1.2.1" |
| 39 |
|
| 40 |
|
| 41 |
var UserAgent = fmt.Sprintf("FastlyGo/%s (+%s; %s)", |
| 42 |
ProjectVersion, ProjectURL, runtime.Version()) |
| 43 |
|
| 44 |
|
| 45 |
type Client struct { |
| 46 |
|
| 47 |
Address string |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
HTTPClient *http.Client |
| 52 |
|
| 53 |
|
| 54 |
apiKey string |
| 55 |
|
| 56 |
|
| 57 |
url *url.URL |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
type RTSClient struct { |
| 62 |
client *Client |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func DefaultClient() *Client { |
| 69 |
client, err := NewClient(os.Getenv(APIKeyEnvVar)) |
| 70 |
if err != nil { |
| 71 |
panic(err) |
| 72 |
} |
| 73 |
return client |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
func NewClient(key string) (*Client, error) { |
| 81 |
return NewClientForEndpoint(key, DefaultEndpoint) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
func NewClientForEndpoint(key string, endpoint string) (*Client, error) { |
| 89 |
client := &Client{apiKey: key, Address: endpoint} |
| 90 |
return client.init() |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
func NewRealtimeStatsClient() *RTSClient { |
| 97 |
c, err := NewClientForEndpoint(os.Getenv(APIKeyEnvVar), RealtimeStatsEndpoint) |
| 98 |
if err != nil { |
| 99 |
panic(err) |
| 100 |
} |
| 101 |
return &RTSClient{client: c} |
| 102 |
} |
| 103 |
|
| 104 |
func (c *Client) init() (*Client, error) { |
| 105 |
u, err := url.Parse(c.Address) |
| 106 |
if err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
c.url = u |
| 110 |
|
| 111 |
if c.HTTPClient == nil { |
| 112 |
c.HTTPClient = cleanhttp.DefaultClient() |
| 113 |
} |
| 114 |
|
| 115 |
return c, nil |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (c *Client) Get(p string, ro *RequestOptions) (*http.Response, error) { |
| 120 |
return c.Request("GET", p, ro) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (c *Client) Head(p string, ro *RequestOptions) (*http.Response, error) { |
| 125 |
return c.Request("HEAD", p, ro) |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (c *Client) Patch(p string, ro *RequestOptions) (*http.Response, error) { |
| 130 |
return c.Request("PATCH", p, ro) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func (c *Client) PatchForm(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 135 |
return c.RequestForm("PATCH", p, i, ro) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func (c *Client) PatchJSON(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 140 |
return c.RequestJSON("PATCH", p, i, ro) |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func (c *Client) PatchJSONAPI(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 145 |
return c.RequestJSONAPI("PATCH", p, i, ro) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
func (c *Client) Post(p string, ro *RequestOptions) (*http.Response, error) { |
| 150 |
return c.Request("POST", p, ro) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func (c *Client) PostForm(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 155 |
return c.RequestForm("POST", p, i, ro) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func (c *Client) PostJSON(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 160 |
return c.RequestJSON("POST", p, i, ro) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (c *Client) PostJSONAPI(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 165 |
return c.RequestJSONAPI("POST", p, i, ro) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
func (c *Client) Put(p string, ro *RequestOptions) (*http.Response, error) { |
| 170 |
return c.Request("PUT", p, ro) |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
func (c *Client) PutForm(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 175 |
return c.RequestForm("PUT", p, i, ro) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
func (c *Client) PutJSON(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 180 |
return c.RequestJSON("PUT", p, i, ro) |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
func (c *Client) PutJSONAPI(p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 185 |
return c.RequestJSONAPI("PUT", p, i, ro) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
func (c *Client) Delete(p string, ro *RequestOptions) (*http.Response, error) { |
| 190 |
return c.Request("DELETE", p, ro) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, error) { |
| 196 |
req, err := c.RawRequest(verb, p, ro) |
| 197 |
if err != nil { |
| 198 |
return nil, err |
| 199 |
} |
| 200 |
|
| 201 |
resp, err := checkResp(c.HTTPClient.Do(req)) |
| 202 |
if err != nil { |
| 203 |
return resp, err |
| 204 |
} |
| 205 |
|
| 206 |
return resp, nil |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
func (c *Client) RequestForm(verb, p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 212 |
if ro == nil { |
| 213 |
ro = new(RequestOptions) |
| 214 |
} |
| 215 |
|
| 216 |
if ro.Headers == nil { |
| 217 |
ro.Headers = make(map[string]string) |
| 218 |
} |
| 219 |
ro.Headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 220 |
|
| 221 |
buf := new(bytes.Buffer) |
| 222 |
if err := form.NewEncoder(buf).KeepZeros(true).DelimitWith('|').Encode(i); err != nil { |
| 223 |
return nil, err |
| 224 |
} |
| 225 |
body := buf.String() |
| 226 |
|
| 227 |
ro.Body = strings.NewReader(body) |
| 228 |
ro.BodyLength = int64(len(body)) |
| 229 |
|
| 230 |
return c.Request(verb, p, ro) |
| 231 |
} |
| 232 |
|
| 233 |
func (c *Client) RequestJSON(verb, p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 234 |
if ro == nil { |
| 235 |
ro = new(RequestOptions) |
| 236 |
} |
| 237 |
|
| 238 |
if ro.Headers == nil { |
| 239 |
ro.Headers = make(map[string]string) |
| 240 |
} |
| 241 |
ro.Headers["Content-Type"] = "application/json" |
| 242 |
ro.Headers["Accept"] = "application/json" |
| 243 |
|
| 244 |
body, err := json.Marshal(i) |
| 245 |
if err != nil { |
| 246 |
return nil, err |
| 247 |
} |
| 248 |
|
| 249 |
ro.Body = bytes.NewReader(body) |
| 250 |
ro.BodyLength = int64(len(body)) |
| 251 |
|
| 252 |
return c.Request(verb, p, ro) |
| 253 |
} |
| 254 |
|
| 255 |
func (c *Client) RequestJSONAPI(verb, p string, i interface{}, ro *RequestOptions) (*http.Response, error) { |
| 256 |
if ro == nil { |
| 257 |
ro = new(RequestOptions) |
| 258 |
} |
| 259 |
|
| 260 |
if ro.Headers == nil { |
| 261 |
ro.Headers = make(map[string]string) |
| 262 |
} |
| 263 |
ro.Headers["Content-Type"] = jsonapi.MediaType |
| 264 |
ro.Headers["Accept"] = jsonapi.MediaType |
| 265 |
|
| 266 |
var buf bytes.Buffer |
| 267 |
if err := jsonapi.MarshalPayload(&buf, i); err != nil { |
| 268 |
return nil, err |
| 269 |
} |
| 270 |
|
| 271 |
ro.Body = &buf |
| 272 |
ro.BodyLength = int64(buf.Len()) |
| 273 |
|
| 274 |
return c.Request(verb, p, ro) |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
func checkResp(resp *http.Response, err error) (*http.Response, error) { |
| 281 |
|
| 282 |
|
| 283 |
if err != nil { |
| 284 |
return resp, err |
| 285 |
} |
| 286 |
|
| 287 |
switch resp.StatusCode { |
| 288 |
case 200, 201, 202, 204, 205, 206: |
| 289 |
return resp, nil |
| 290 |
default: |
| 291 |
return resp, NewHTTPError(resp) |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
func decodeJSON(out interface{}, body io.ReadCloser) error { |
| 297 |
defer body.Close() |
| 298 |
|
| 299 |
var parsed interface{} |
| 300 |
dec := json.NewDecoder(body) |
| 301 |
if err := dec.Decode(&parsed); err != nil { |
| 302 |
return err |
| 303 |
} |
| 304 |
|
| 305 |
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ |
| 306 |
DecodeHook: mapstructure.ComposeDecodeHookFunc( |
| 307 |
mapToHTTPHeaderHookFunc(), |
| 308 |
stringToTimeHookFunc(), |
| 309 |
), |
| 310 |
WeaklyTypedInput: true, |
| 311 |
Result: out, |
| 312 |
}) |
| 313 |
if err != nil { |
| 314 |
return err |
| 315 |
} |
| 316 |
return decoder.Decode(parsed) |
| 317 |
} |