Skip to content

Commit 3980858

Browse files
authored
Merge pull request #266 from xmidt-org/acquire
Remove Acquire
2 parents 710f1a8 + 44b0ca9 commit 3980858

9 files changed

+133
-298
lines changed

chrysom/acquire.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package chrysom
5+
6+
import (
7+
"net/http"
8+
)
9+
10+
// Acquirer adds an authorization header and value to a given http request.
11+
type Acquirer interface {
12+
AddAuth(*http.Request) error
13+
}

chrysom/acquire_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
2+
// SPDX-License-Identifier: Apache-2.0
3+
package chrysom
4+
5+
import (
6+
"net/http"
7+
8+
"github.com/stretchr/testify/mock"
9+
)
10+
11+
type MockAquirer struct {
12+
mock.Mock
13+
}
14+
15+
func (m *MockAquirer) AddAuth(req *http.Request) error {
16+
args := m.Called(req)
17+
18+
return args.Error(0)
19+
}

chrysom/basicClient.go

+8-28
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"net/http"
1414

1515
"github.com/xmidt-org/ancla/model"
16-
"github.com/xmidt-org/bascule/acquire"
1716
"github.com/xmidt-org/sallust"
1817
"go.uber.org/zap"
1918
)
@@ -25,6 +24,7 @@ var (
2524
ErrItemDataEmpty = errors.New("data field in item is required")
2625
ErrUndefinedIntervalTicker = errors.New("interval ticker is nil. Can't listen for updates")
2726
ErrAuthAcquirerFailure = errors.New("failed acquiring auth token")
27+
ErrAuthAcquirerNil = errors.New("auth aquirer is nil")
2828
ErrBadRequest = errors.New("argus rejected the request as invalid")
2929
)
3030

@@ -58,23 +58,17 @@ type BasicClientConfig struct {
5858

5959
// Auth provides the mechanism to add auth headers to outgoing requests.
6060
// (Optional) If not provided, no auth headers are added.
61-
Auth Auth
61+
Auth Acquirer
6262
}
6363

6464
// BasicClient is the client used to make requests to Argus.
6565
type BasicClient struct {
6666
client *http.Client
67-
auth acquire.Acquirer
67+
auth Acquirer
6868
storeBaseURL string
6969
bucket string
7070
}
7171

72-
// Auth contains authorization data for requests to Argus.
73-
type Auth struct {
74-
JWT acquire.RemoteBearerTokenAcquirerOptions
75-
Basic string
76-
}
77-
7872
type response struct {
7973
Body []byte
8074
ArgusErrorHeader string
@@ -99,13 +93,9 @@ func NewBasicClient(config BasicClientConfig) (*BasicClient, error) {
9993
return nil, err
10094
}
10195

102-
tokenAcquirer, err := buildTokenAcquirer(config.Auth)
103-
if err != nil {
104-
return nil, err
105-
}
10696
clientStore := &BasicClient{
10797
client: config.HTTPClient,
108-
auth: tokenAcquirer,
98+
auth: config.Auth,
10999
bucket: config.Bucket,
110100
storeBaseURL: config.Address + storeAPIPath,
111101
}
@@ -210,7 +200,10 @@ func (c *BasicClient) sendRequest(ctx context.Context, owner, method, url string
210200
if err != nil {
211201
return response{}, fmt.Errorf(errWrappedFmt, errNewRequestFailure, err.Error())
212202
}
213-
err = acquire.AddAuth(r, c.auth)
203+
if c.auth == nil {
204+
return response{}, ErrAuthAcquirerNil
205+
}
206+
err = c.auth.AddAuth(r)
214207
if err != nil {
215208
return response{}, fmt.Errorf(errWrappedFmt, ErrAuthAcquirerFailure, err.Error())
216209
}
@@ -234,10 +227,6 @@ func (c *BasicClient) sendRequest(ctx context.Context, owner, method, url string
234227
return sqResp, nil
235228
}
236229

237-
func isEmpty(options acquire.RemoteBearerTokenAcquirerOptions) bool {
238-
return len(options.AuthURL) < 1 || options.Buffer == 0 || options.Timeout == 0
239-
}
240-
241230
// translateNonSuccessStatusCode returns as specific error
242231
// for known Argus status codes.
243232
func translateNonSuccessStatusCode(code int) error {
@@ -251,15 +240,6 @@ func translateNonSuccessStatusCode(code int) error {
251240
}
252241
}
253242

254-
func buildTokenAcquirer(auth Auth) (acquire.Acquirer, error) {
255-
if !isEmpty(auth.JWT) {
256-
return acquire.NewRemoteBearerTokenAcquirer(auth.JWT)
257-
} else if len(auth.Basic) > 0 {
258-
return acquire.NewFixedAuthAcquirer(auth.Basic)
259-
}
260-
return &acquire.DefaultAcquirer{}, nil
261-
}
262-
263243
func validateBasicConfig(config *BasicClientConfig) error {
264244
if config.Address == "" {
265245
return ErrAddressEmpty

0 commit comments

Comments
 (0)