@@ -11,7 +11,6 @@ import (
11
11
"fmt"
12
12
"io"
13
13
"net/http"
14
- "time"
15
14
16
15
"github.com/xmidt-org/ancla/auth"
17
16
"github.com/xmidt-org/ancla/model"
@@ -25,53 +24,28 @@ const (
25
24
)
26
25
27
26
var (
28
- ErrNilMeasures = errors .New ("measures cannot be nil" )
29
- ErrAddressEmpty = errors .New ("argus address is required" )
30
- ErrBucketEmpty = errors .New ("bucket name is required" )
31
- ErrItemIDEmpty = errors .New ("item ID is required" )
32
- ErrItemDataEmpty = errors .New ("data field in item is required" )
33
- ErrUndefinedIntervalTicker = errors .New ("interval ticker is nil. Can't listen for updates" )
34
- ErrAuthDecoratorFailure = errors .New ("failed decorating auth header" )
35
- ErrBadRequest = errors .New ("argus rejected the request as invalid" )
27
+ ErrItemIDEmpty = errors .New ("item ID is required" )
28
+ ErrItemDataEmpty = errors .New ("data field in item is required" )
29
+ ErrAuthDecoratorFailure = errors .New ("failed decorating auth header" )
30
+ ErrBadRequest = errors .New ("argus rejected the request as invalid" )
36
31
)
37
32
38
33
var (
39
- errNonSuccessResponse = errors .New ("argus responded with a non-success status code " )
40
- errNewRequestFailure = errors .New ("failed creating an HTTP request " )
41
- errDoRequestFailure = errors .New ("http client failed while sending request" )
42
- errReadingBodyFailure = errors .New ("failed while reading http response body " )
43
- errJSONUnmarshal = errors .New ("failed unmarshaling JSON response payload " )
44
- errJSONMarshal = errors .New ("failed marshaling item as JSON payload" )
45
- errFailedConfig = errors .New ("ancla configuration error " )
34
+ ErrFailedAuthentication = errors .New ("failed to authentication with argus " )
35
+ errNonSuccessResponse = errors .New ("argus responded with a non-success status code " )
36
+ errNewRequestFailure = errors .New ("failed creating an HTTP request" )
37
+ errDoRequestFailure = errors .New ("http client failed while sending request " )
38
+ errReadingBodyFailure = errors .New ("failed while reading http response body " )
39
+ errJSONUnmarshal = errors .New ("failed unmarshaling JSON response payload" )
40
+ errJSONMarshal = errors .New ("failed marshaling item as JSON payload " )
46
41
)
47
42
48
- // BasicClientConfig contains config data for the client that will be used to
49
- // make requests to the Argus client.
50
- type BasicClientConfig struct {
51
- // Address is the Argus URL (i.e. https://example-argus.io:8090)
52
- Address string
53
-
54
- // Bucket partition to be used by this client.
55
- Bucket string
56
-
57
- // HTTPClient refers to the client that will be used to send requests.
58
- // (Optional) Defaults to http.DefaultClient.
59
- HTTPClient * http.Client
60
-
61
- // Auth provides the mechanism to add auth headers to outgoing requests.
62
- // (Optional) If not provided, no auth headers are added.
63
- Auth auth.Decorator
64
-
65
- // PullInterval is how often listeners should get updates.
66
- // (Optional). Defaults to 5 seconds.
67
- PullInterval time.Duration
68
- }
69
-
70
43
// BasicClient is the client used to make requests to Argus.
71
44
type BasicClient struct {
72
45
client * http.Client
73
46
auth auth.Decorator
74
47
storeBaseURL string
48
+ storeAPIPath string
75
49
bucket string
76
50
getLogger func (context.Context ) * zap.Logger
77
51
}
@@ -83,31 +57,32 @@ type response struct {
83
57
}
84
58
85
59
const (
86
- storeAPIPath = "/api/v1/store"
60
+ storeV1APIPath = "/api/v1/store"
87
61
errWrappedFmt = "%w: %s"
88
62
errStatusCodeFmt = "%w: received status %v"
89
63
errorHeaderKey = "errorHeader"
90
64
)
91
65
92
- // Items is a slice of model.Item(s) .
93
- type Items []model.Item
94
-
95
66
// NewBasicClient creates a new BasicClient that can be used to
96
67
// make requests to Argus.
97
- func NewBasicClient (config BasicClientConfig ,
98
- getLogger func (context.Context ) * zap.Logger ) (* BasicClient , error ) {
99
- err := validateBasicConfig (& config )
100
- if err != nil {
101
- return nil , err
102
- }
68
+ func NewBasicClient (opts ... ClientOption ) (* BasicClient , error ) {
69
+ var (
70
+ client BasicClient
71
+ defaultClientOptions = ClientOptions {
72
+ // localhost defaults
73
+ StoreBaseURL ("" ),
74
+ StoreAPIPath ("" ),
75
+ // Nop defaults
76
+ HTTPClient (nil ),
77
+ GetClientLogger (nil ),
78
+ Auth (nil ),
79
+ }
80
+ )
103
81
104
- return & BasicClient {
105
- client : config .HTTPClient ,
106
- auth : config .Auth ,
107
- bucket : config .Bucket ,
108
- storeBaseURL : config .Address + storeAPIPath ,
109
- getLogger : getLogger ,
110
- }, nil
82
+ opts = append (defaultClientOptions , opts ... )
83
+ opts = append (opts , clientValidator ())
84
+
85
+ return & client , ClientOptions (opts ).apply (& client )
111
86
}
112
87
113
88
// GetItems fetches all items that belong to a given owner.
@@ -251,19 +226,3 @@ func translateNonSuccessStatusCode(code int) error {
251
226
return errNonSuccessResponse
252
227
}
253
228
}
254
-
255
- func validateBasicConfig (config * BasicClientConfig ) error {
256
- if config .Address == "" {
257
- return ErrAddressEmpty
258
- }
259
-
260
- if config .Bucket == "" {
261
- return ErrBucketEmpty
262
- }
263
-
264
- if config .HTTPClient == nil {
265
- config .HTTPClient = http .DefaultClient
266
- }
267
-
268
- return nil
269
- }
0 commit comments