-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_api.go
145 lines (122 loc) · 3.05 KB
/
client_api.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package ovh
import (
"bytes"
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"sync"
"time"
"golang.org/x/net/context"
)
var baseURL = "https://eu.api.ovh.com/1.0"
func apiURL(format string, a ...interface{}) string {
return fmt.Sprintf("%s%s", baseURL, fmt.Sprintf(format, a...))
}
type httpResponse struct {
resp *http.Response
err error
}
type ovhRequest struct {
opts *Options
method string
query string
body []byte
timestamp string
}
var plus = []byte("+")
// requestSignature - Calculates a request signature and returns in following pattern
// "$1$" + SHA1_HEX(AS+"+"+CK+"+"+METHOD+"+"+QUERY+"+"+BODY+"+"+TSTAMP)
func (req *ovhRequest) signature() string {
hash := sha1.New()
hash.Write([]byte(req.opts.AppSecret))
hash.Write(plus)
hash.Write([]byte(req.opts.ConsumerKey))
hash.Write(plus)
hash.Write([]byte(req.method))
hash.Write(plus)
hash.Write([]byte(req.query))
hash.Write(plus)
hash.Write(req.body)
hash.Write(plus)
hash.Write([]byte(req.timestamp))
return fmt.Sprintf("$1$%x", hash.Sum(nil))
}
func httpDo(ctx context.Context, opts *Options, method, url string, buffer io.Reader) (response *http.Response, err error) {
var body []byte
if opts.ConsumerKey != "" && buffer != nil {
body, err = ioutil.ReadAll(buffer)
if err != nil {
return nil, err
}
buffer = bytes.NewBuffer(body)
}
req, err := http.NewRequest(method, url, buffer)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Ovh-Application", opts.AppKey)
if opts.ConsumerKey != "" {
r := &ovhRequest{
opts: opts,
method: method,
query: req.URL.String(),
body: body,
timestamp: ovhTimestamp(),
}
req.Header.Set("X-Ovh-Timestamp", r.timestamp)
req.Header.Set("X-Ovh-Consumer", opts.ConsumerKey)
req.Header.Set("X-Ovh-Signature", r.signature())
}
transport := &http.Transport{}
client := &http.Client{Transport: transport}
respchan := make(chan *httpResponse, 1)
go func() {
resp, err := client.Do(req)
respchan <- &httpResponse{resp: resp, err: err}
}()
select {
case <-ctx.Done():
transport.CancelRequest(req)
<-respchan
return nil, ctx.Err()
case r := <-respchan:
return r.resp, r.err
}
}
var (
onceTime sync.Once
ovhTime = new(struct {
LocalUnixTime int64
OvhTimestamp int64
})
)
func ovhTimestamp() string {
onceTime.Do(func() {
resp, err := http.Get("https://eu.api.ovh.com/1.0/auth/time")
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Set response time
ovhTime.LocalUnixTime = time.Now().Unix()
t, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Set time on ovh server
timestamp, err := strconv.Atoi(string(t))
if err != nil {
panic(err)
}
ovhTime.OvhTimestamp = int64(timestamp)
})
return fmt.Sprintf("%d", ovhTime.OvhTimestamp+(time.Now().Unix()-ovhTime.LocalUnixTime))
}
func unexpectedStatusError(resp *http.Response) error {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("Unexpected status code: %v (body=%s)", resp.StatusCode, body)
}