aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-sdk-for-go/storage/util.go
blob: d71c6ce55d8e1461a5673cc5a7940579c8cba474 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package storage

import (
    "bytes"
    "crypto/hmac"
    "crypto/sha256"
    "encoding/base64"
    "encoding/xml"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "net/url"
    "reflect"
    "time"
)

func (c Client) computeHmac256(message string) string {
    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 {
        s += fmt.Sprintf("<%s>%s</%s>", v.Status, v.ID, v.Status)
    }
    s += `</BlockList>`
    return s
}

func xmlUnmarshal(body io.Reader, v interface{}) error {
    data, err := ioutil.ReadAll(body)
    if err != nil {
        return err
    }
    return xml.Unmarshal(data, v)
}

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
}

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
}