2015-02-05 01:37:43 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2015-06-12 00:30:18 +02:00
|
|
|
"bytes"
|
2015-02-05 01:37:43 +01:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-10-29 00:46:05 +02:00
|
|
|
"reflect"
|
2015-02-05 01:37:43 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-06-12 00:30:18 +02:00
|
|
|
func (c Client) computeHmac256(message string) string {
|
2015-02-05 01:37:43 +01:00
|
|
|
h := hmac.New(sha256.New, c.accountKey)
|
|
|
|
h.Write([]byte(message))
|
|
|
|
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func currentTimeRfc1123Formatted() string {
|
|
|
|
return timeRfc1123Formatted(time.Now().UTC())
|
|
|
|
}
|
|
|
|
|
|
|
|
func timeRfc1123Formatted(t time.Time) string {
|
|
|
|
return t.Format(http.TimeFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeParams(v1, v2 url.Values) url.Values {
|
|
|
|
out := url.Values{}
|
|
|
|
for k, v := range v1 {
|
|
|
|
out[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range v2 {
|
|
|
|
vals, ok := out[k]
|
|
|
|
if ok {
|
|
|
|
vals = append(vals, v...)
|
|
|
|
out[k] = vals
|
|
|
|
} else {
|
|
|
|
out[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareBlockListRequest(blocks []Block) string {
|
|
|
|
s := `<?xml version="1.0" encoding="utf-8"?><BlockList>`
|
|
|
|
for _, v := range blocks {
|
2015-06-12 00:30:18 +02:00
|
|
|
s += fmt.Sprintf("<%s>%s</%s>", v.Status, v.ID, v.Status)
|
2015-02-05 01:37:43 +01:00
|
|
|
}
|
|
|
|
s += `</BlockList>`
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-06-12 00:30:18 +02:00
|
|
|
func xmlUnmarshal(body io.Reader, v interface{}) error {
|
2015-02-05 01:37:43 +01:00
|
|
|
data, err := ioutil.ReadAll(body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return xml.Unmarshal(data, v)
|
|
|
|
}
|
2015-06-12 00:30:18 +02:00
|
|
|
|
|
|
|
func xmlMarshal(v interface{}) (io.Reader, int, error) {
|
|
|
|
b, err := xml.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return bytes.NewReader(b), len(b), nil
|
|
|
|
}
|
2016-10-29 00:46:05 +02:00
|
|
|
|
|
|
|
func headersFromStruct(v interface{}) map[string]string {
|
|
|
|
headers := make(map[string]string)
|
|
|
|
value := reflect.ValueOf(v)
|
|
|
|
for i := 0; i < value.NumField(); i++ {
|
|
|
|
key := value.Type().Field(i).Tag.Get("header")
|
|
|
|
val := value.Field(i).String()
|
|
|
|
if val != "" {
|
|
|
|
headers[key] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return headers
|
|
|
|
}
|