forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp.go
231 lines (191 loc) · 4.56 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
package provider
import (
"encoding/base64"
"fmt"
"io"
"math"
"strconv"
"strings"
"time"
"github.com/andig/evcc/util"
"github.com/andig/evcc/util/jq"
"github.com/andig/evcc/util/request"
"github.com/itchyny/gojq"
)
// HTTP implements HTTP request provider
type HTTP struct {
*request.Helper
url, method string
headers map[string]string
body string
scale float64
jq *gojq.Query
}
func init() {
registry.Add("http", NewHTTPProviderFromConfig)
}
// Auth is the authorization config
type Auth struct {
Type, User, Password string
}
// AuthHeaders creates authorization headers from config
func AuthHeaders(log *util.Logger, auth Auth, headers map[string]string) error {
if strings.ToLower(auth.Type) != "basic" {
return fmt.Errorf("unsupported auth type: %s", auth.Type)
}
basicAuth := auth.User + ":" + auth.Password
headers["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(basicAuth))
return nil
}
// NewHTTPProviderFromConfig creates a HTTP provider
func NewHTTPProviderFromConfig(other map[string]interface{}) (IntProvider, error) {
cc := struct {
URI, Method string
Headers map[string]string
Body string
Jq string
Scale float64
Insecure bool
Auth Auth
Timeout time.Duration
}{
Headers: make(map[string]string),
Timeout: request.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
log := util.NewLogger("http")
// handle basic auth
if cc.Auth.Type != "" {
if err := AuthHeaders(log, cc.Auth, cc.Headers); err != nil {
return nil, fmt.Errorf("http auth: %w", err)
}
}
http, err := NewHTTP(log,
cc.Method,
cc.URI,
cc.Headers,
cc.Body,
cc.Insecure,
cc.Jq,
cc.Scale,
)
if err != nil {
return nil, err
}
if err == nil {
http.Client.Timeout = cc.Timeout
}
return http, err
}
// NewHTTP create HTTP provider
func NewHTTP(log *util.Logger, method, uri string, headers map[string]string, body string, insecure bool, jq string, scale float64) (*HTTP, error) {
url := util.DefaultScheme(uri, "http")
if url != uri {
log.WARN.Printf("missing scheme for %s, assuming http", uri)
}
p := &HTTP{
Helper: request.NewHelper(log),
url: url,
method: method,
headers: headers,
body: body,
scale: scale,
}
// ignore the self signed certificate
if insecure {
p.Client.Transport = request.NewTripper(log, request.InsecureTransport())
}
if jq != "" {
op, err := gojq.Parse(jq)
if err != nil {
return nil, fmt.Errorf("invalid jq query '%s': %w", jq, err)
}
p.jq = op
}
return p, nil
}
// request executed the configured request
func (p *HTTP) request(body ...string) ([]byte, error) {
var b io.Reader
if len(body) == 1 {
b = strings.NewReader(body[0])
}
// empty method becomes GET
req, err := request.New(strings.ToUpper(p.method), p.url, b, p.headers)
if err != nil {
return []byte{}, err
}
return p.DoBody(req)
}
// FloatGetter parses float from request
func (p *HTTP) FloatGetter() func() (float64, error) {
g := p.StringGetter()
return func() (float64, error) {
s, err := g()
if err != nil {
return 0, err
}
f, err := strconv.ParseFloat(s, 64)
if err == nil && p.scale != 0 {
f *= p.scale
}
return f, err
}
}
// IntGetter parses int64 from request
func (p *HTTP) IntGetter() func() (int64, error) {
g := p.FloatGetter()
return func() (int64, error) {
f, err := g()
return int64(math.Round(f)), err
}
}
// StringGetter sends string request
func (p *HTTP) StringGetter() func() (string, error) {
return func() (string, error) {
b, err := p.request()
if err != nil {
return string(b), err
}
if p.jq != nil {
v, err := jq.Query(p.jq, b)
return fmt.Sprintf("%v", v), err
}
return string(b), err
}
}
// BoolGetter parses bool from request
func (p *HTTP) BoolGetter() func() (bool, error) {
g := p.StringGetter()
return func() (bool, error) {
s, err := g()
return util.Truish(s), err
}
}
func (p *HTTP) set(param string, val interface{}) error {
body, err := setFormattedValue(p.body, param, val)
if err == nil {
_, err = p.request(body)
}
return err
}
// IntSetter sends int request
func (p *HTTP) IntSetter(param string) func(int64) error {
return func(val int64) error {
return p.set(param, val)
}
}
// StringSetter sends string request
func (p *HTTP) StringSetter(param string) func(string) error {
return func(val string) error {
return p.set(param, val)
}
}
// BoolSetter sends bool request
func (p *HTTP) BoolSetter(param string) func(bool) error {
return func(val bool) error {
return p.set(param, val)
}
}