2016-03-23 20:53:09 +01:00
|
|
|
package fastly
|
|
|
|
|
|
|
|
import (
|
2016-04-20 20:43:54 +02:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2016-03-23 20:53:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// RequestOptions is the list of options to pass to the request.
|
|
|
|
type RequestOptions struct {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Params is a map of key-value pairs that will be added to the Request.
|
|
|
|
Params map[string]string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Headers is a map of key-value pairs that will be added to the Request.
|
|
|
|
Headers map[string]string
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Body is an io.Reader object that will be streamed or uploaded with the
|
|
|
|
// Request. BodyLength is the final size of the Body.
|
|
|
|
Body io.Reader
|
|
|
|
BodyLength int64
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RawRequest accepts a verb, URL, and RequestOptions struct and returns the
|
|
|
|
// constructed http.Request and any errors that occurred
|
|
|
|
func (c *Client) RawRequest(verb, p string, ro *RequestOptions) (*http.Request, error) {
|
2016-04-20 20:43:54 +02:00
|
|
|
// Ensure we have request options.
|
|
|
|
if ro == nil {
|
|
|
|
ro = new(RequestOptions)
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Append the path to the URL.
|
|
|
|
u := *c.url
|
|
|
|
u.Path = path.Join(c.url.Path, p)
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Add the token and other params.
|
|
|
|
var params = make(url.Values)
|
|
|
|
for k, v := range ro.Params {
|
|
|
|
params.Add(k, v)
|
|
|
|
}
|
|
|
|
u.RawQuery = params.Encode()
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Create the request object.
|
|
|
|
request, err := http.NewRequest(verb, u.String(), ro.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Set the API key.
|
|
|
|
if len(c.apiKey) > 0 {
|
|
|
|
request.Header.Set(APIKeyHeader, c.apiKey)
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Set the User-Agent.
|
|
|
|
request.Header.Set("User-Agent", UserAgent)
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Add any custom headers.
|
|
|
|
for k, v := range ro.Headers {
|
|
|
|
request.Header.Add(k, v)
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
// Add Content-Length if we have it.
|
|
|
|
if ro.BodyLength > 0 {
|
|
|
|
request.ContentLength = ro.BodyLength
|
|
|
|
}
|
2016-03-23 20:53:09 +01:00
|
|
|
|
2016-04-20 20:43:54 +02:00
|
|
|
return request, nil
|
2016-03-23 20:53:09 +01:00
|
|
|
}
|