forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
295 lines (240 loc) · 6.36 KB
/
http.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package provider
import (
"context"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/provider/pipeline"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/gregjones/httpcache"
"github.com/jpfielding/go-http-digest/pkg/digest"
)
// HTTP implements HTTP request provider
type HTTP struct {
*request.Helper
url, method string
headers map[string]string
body string
scale float64
cache time.Duration
updated time.Time
pipeline *pipeline.Pipeline
val []byte // Cached http response value
err error // Cached http response error
}
func init() {
registry.AddCtx("http", NewHTTPProviderFromConfig)
}
// Auth is the authorization config
type Auth struct {
Type, User, Password string
}
// NewHTTPProviderFromConfig creates a HTTP provider
func NewHTTPProviderFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) {
cc := struct {
URI, Method string
Headers map[string]string
Body string
pipeline.Settings `mapstructure:",squash"`
Scale float64
Insecure bool
Auth Auth
Timeout time.Duration
Cache time.Duration
}{
Headers: make(map[string]string),
Method: http.MethodGet,
Scale: 1,
Timeout: request.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
log := contextLogger(ctx, util.NewLogger("http"))
http := NewHTTP(
log,
strings.ToUpper(cc.Method),
cc.URI,
cc.Insecure,
cc.Scale,
cc.Cache,
).
WithHeaders(cc.Headers).
WithBody(cc.Body)
http.Client.Timeout = cc.Timeout
var err error
if cc.Auth.Type != "" {
_, err = http.WithAuth(cc.Auth.Type, cc.Auth.User, cc.Auth.Password)
}
if err == nil {
var pipe *pipeline.Pipeline
pipe, err = pipeline.New(log, cc.Settings)
http = http.WithPipeline(pipe)
}
return http, err
}
// NewHTTP create HTTP provider
func NewHTTP(log *util.Logger, method, uri string, insecure bool, scale float64, cache time.Duration) *HTTP {
p := &HTTP{
Helper: request.NewHelper(log),
url: uri,
method: method,
scale: scale,
cache: cache,
}
// http cache
cacheTransport := httpcache.NewMemoryCacheTransport()
cacheTransport.Transport = p.Client.Transport
p.Client.Transport = cacheTransport
// ignore the self signed certificate
if insecure {
p.Client.Transport = request.NewTripper(log, transport.Insecure())
}
return p
}
// WithBody adds request body
func (p *HTTP) WithBody(body string) *HTTP {
p.body = body
return p
}
// WithHeaders adds request headers
func (p *HTTP) WithHeaders(headers map[string]string) *HTTP {
p.headers = headers
return p
}
// WithPipeline adds a processing pipeline
func (p *HTTP) WithPipeline(pipeline *pipeline.Pipeline) *HTTP {
p.pipeline = pipeline
return p
}
// WithAuth adds authorized transport
func (p *HTTP) WithAuth(typ, user, password string) (*HTTP, error) {
switch strings.ToLower(typ) {
case "basic":
basicAuth := transport.BasicAuthHeader(user, password)
log.Redact(basicAuth)
p.Client.Transport = transport.BasicAuth(user, password, p.Client.Transport)
case "bearer":
p.Client.Transport = transport.BearerAuth(password, p.Client.Transport)
case "digest":
p.Client.Transport = digest.NewTransport(user, password, p.Client.Transport)
default:
return nil, fmt.Errorf("unknown auth type '%s'", typ)
}
return p, nil
}
// request executes the configured request or returns the cached value
func (p *HTTP) request(url string, body string) ([]byte, error) {
if time.Since(p.updated) >= p.cache {
var b io.Reader
if p.method != http.MethodGet {
b = strings.NewReader(body)
}
url := util.DefaultScheme(url, "http")
// empty method becomes GET
req, err := request.New(p.method, url, b, p.headers)
if err != nil {
return []byte{}, err
}
p.val, p.err = p.DoBody(req)
if p.err != nil {
if err := knownErrors(p.val); err != nil {
p.err = err
}
}
p.updated = time.Now()
}
return p.val, p.err
}
var _ StringProvider = (*HTTP)(nil)
// StringGetter sends string request
func (p *HTTP) StringGetter() (func() (string, error), error) {
return func() (string, error) {
url, err := setFormattedValue(p.url, "", "")
if err != nil {
return "", err
}
b, err := p.request(url, p.body)
if err == nil && p.pipeline != nil {
b, err = p.pipeline.Process(b)
}
return string(b), err
}, nil
}
var _ FloatProvider = (*HTTP)(nil)
// FloatGetter parses float from request
func (p *HTTP) FloatGetter() (func() (float64, error), error) {
g, err := p.StringGetter()
return func() (float64, error) {
s, err := g()
if err != nil {
return 0, err
}
f, err := strconv.ParseFloat(s, 64)
return f * p.scale, err
}, err
}
var _ IntProvider = (*HTTP)(nil)
// IntGetter parses int64 from request
func (p *HTTP) IntGetter() (func() (int64, error), error) {
g, err := p.FloatGetter()
return func() (int64, error) {
f, err := g()
return int64(math.Round(f)), err
}, err
}
var _ BoolProvider = (*HTTP)(nil)
// BoolGetter parses bool from request
func (p *HTTP) BoolGetter() (func() (bool, error), error) {
g, err := p.StringGetter()
return func() (bool, error) {
s, err := g()
return util.Truish(s), err
}, err
}
func (p *HTTP) set(param string, val interface{}) error {
url, err := setFormattedValue(p.url, param, val)
if err != nil {
return err
}
body, err := setFormattedValue(p.body, param, val)
if err != nil {
return err
}
_, err = p.request(url, body)
return err
}
var _ SetIntProvider = (*HTTP)(nil)
// IntSetter sends int request
func (p *HTTP) IntSetter(param string) (func(int64) error, error) {
return func(val int64) error {
return p.set(param, val)
}, nil
}
var _ SetFloatProvider = (*HTTP)(nil)
// FloatSetter sends int request
func (p *HTTP) FloatSetter(param string) (func(float64) error, error) {
return func(val float64) error {
return p.set(param, val)
}, nil
}
var _ SetStringProvider = (*HTTP)(nil)
// StringSetter sends string request
func (p *HTTP) StringSetter(param string) (func(string) error, error) {
return func(val string) error {
return p.set(param, val)
}, nil
}
var _ SetBoolProvider = (*HTTP)(nil)
// BoolSetter sends bool request
func (p *HTTP) BoolSetter(param string) (func(bool) error, error) {
return func(val bool) error {
return p.set(param, val)
}, nil
}