-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
config.go
66 lines (54 loc) · 1.63 KB
/
config.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
// Copyright 2020 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package http
import (
"fmt"
"net/http"
"time"
"github.com/open-policy-agent/opa/internal/wasm/sdk/opa/errors"
)
// WithURL configures the URL to download the bundle from.
func (l *Loader) WithURL(url string) *Loader {
l.url = url
return l
}
// WithClient configures the HTTP client to use. If not configured,
// http.DefaultClient is used.
func (l *Loader) WithClient(client *http.Client) *Loader {
if client == nil {
l.configErr = fmt.Errorf("client: %w", errors.ErrInvalidConfig)
return l
}
l.client = client
return l
}
// WithInterval configures the minimum and maximum delay between bundle downloads.
func (l *Loader) WithInterval(min, max time.Duration) *Loader {
if min > max {
l.configErr = fmt.Errorf("interval: %w", errors.ErrInvalidConfig)
return l
}
l.minDelay = min
l.maxDelay = max
return l
}
// WithPrepareRequest configures a handler to customize the HTTP requests before their sending. The
// HTTP request is not modified after the handle invocation.
func (l *Loader) WithPrepareRequest(prepare func(*http.Request) error) *Loader {
if prepare == nil {
l.configErr = fmt.Errorf("prepare request: %w", errors.ErrInvalidConfig)
return l
}
l.prepareRequest = prepare
return l
}
// WithErrorLogger configures an error logger invoked with all the errors.
func (l *Loader) WithErrorLogger(logger func(error)) *Loader {
if logger == nil {
l.configErr = fmt.Errorf("logger: %w", errors.ErrInvalidConfig)
return l
}
l.logError = logger
return l
}