Skip to content

Commit b5c12d1

Browse files
authored
CLOUDP-333245: Use core HTTPClient (#4176)
1 parent 90a6c8b commit b5c12d1

File tree

6 files changed

+28
-52
lines changed

6 files changed

+28
-52
lines changed

build/package/purls.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pkg:golang/github.com/mholt/[email protected]
7575
pkg:golang/github.com/mikelolasagasti/[email protected]
7676
pkg:golang/github.com/minio/[email protected]
7777
pkg:golang/github.com/mongodb-forks/[email protected]
78-
pkg:golang/github.com/mongodb/[email protected]20250822132614-230a610dbe0f
78+
pkg:golang/github.com/mongodb/[email protected]20250901162552-2e62c5010f93
7979
pkg:golang/github.com/montanaflynn/[email protected]
8080
pkg:golang/github.com/nwaples/rardecode/[email protected]
8181
pkg:golang/github.com/pelletier/go-toml/[email protected]

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
github.com/mattn/go-isatty v0.0.20
3131
github.com/mholt/archives v0.1.3
3232
github.com/mongodb-labs/cobra2snooty v1.19.1
33-
github.com/mongodb/atlas-cli-core v0.0.0-20250822132614-230a610dbe0f
33+
github.com/mongodb/atlas-cli-core v0.0.0-20250901162552-2e62c5010f93
3434
github.com/pelletier/go-toml v1.9.5
3535
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
3636
github.com/shirou/gopsutil/v4 v4.25.7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ github.com/mongodb-forks/digest v1.1.0 h1:7eUdsR1BtqLv0mdNm4OXs6ddWvR4X2/OsLwdKk
320320
github.com/mongodb-forks/digest v1.1.0/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg=
321321
github.com/mongodb-labs/cobra2snooty v1.19.1 h1:GDEQZWy8f/DeJlImNgVvStu6sgNi8nuSOR1Oskcw8BI=
322322
github.com/mongodb-labs/cobra2snooty v1.19.1/go.mod h1:Hyq4YadN8dwdOiz56MXwTuVN63p0WlkQwxdLxOSGdX8=
323-
github.com/mongodb/atlas-cli-core v0.0.0-20250822132614-230a610dbe0f h1:oSNF4zFkefc61U5K/C/rT6A0tsbu7cG/Tdo1ZVKbUGA=
324-
github.com/mongodb/atlas-cli-core v0.0.0-20250822132614-230a610dbe0f/go.mod h1:QDfVGpdfxXM1httLNXCKsfWTKv6slzCqBZxkkPIktlQ=
323+
github.com/mongodb/atlas-cli-core v0.0.0-20250901162552-2e62c5010f93 h1:arSkz/kAB+5jgo0efH24UByHsYe7QZ9cHgnAXZPRPeg=
324+
github.com/mongodb/atlas-cli-core v0.0.0-20250901162552-2e62c5010f93/go.mod h1:QDfVGpdfxXM1httLNXCKsfWTKv6slzCqBZxkkPIktlQ=
325325
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
326326
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
327327
github.com/nwaples/rardecode/v2 v2.1.0 h1:JQl9ZoBPDy+nIZGb1mx8+anfHp/LV3NE2MjMiv0ct/U=

internal/api/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/mongodb/atlas-cli-core/config"
2424
"github.com/mongodb/atlas-cli-core/transport"
2525
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/log"
26-
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
26+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/version"
2727
)
2828

2929
var (
@@ -72,7 +72,7 @@ func NewExecutor(commandConverter CommandConverter, httpClient Doer, formatter R
7272
func NewDefaultExecutor(formatter ResponseFormatter) (*Executor, error) {
7373
profile := config.Default()
7474

75-
client, err := store.HTTPClient(profile, transport.Default())
75+
client, err := transport.HTTPClient(version.Version, transport.Default())
7676
if err != nil {
7777
return nil, err
7878
}

internal/store/store.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,6 @@ type Store struct {
5353
ctx context.Context
5454
}
5555

56-
func HTTPClient(c CredentialsGetter, httpTransport http.RoundTripper) (*http.Client, error) {
57-
switch c.AuthType() {
58-
case config.APIKeys:
59-
t := transport.NewDigestTransport(c.PublicAPIKey(), c.PrivateAPIKey(), httpTransport)
60-
return t.Client()
61-
case config.UserAccount:
62-
token, err := c.Token()
63-
if err != nil {
64-
return nil, err
65-
}
66-
tr, err := transport.NewAccessTokenTransport(token, httpTransport, version.Version, func(t *atlasauth.Token) error {
67-
config.SetAccessToken(t.AccessToken)
68-
config.SetRefreshToken(t.RefreshToken)
69-
return config.Save()
70-
})
71-
if err != nil {
72-
return nil, err
73-
}
74-
return &http.Client{Transport: tr}, nil
75-
case config.ServiceAccount:
76-
return transport.NewServiceAccountClientWithHost(c.ClientID(), c.ClientSecret(), config.OpsManagerURL()), nil
77-
case config.NoAuth:
78-
fallthrough
79-
default:
80-
return &http.Client{Transport: httpTransport}, nil
81-
}
82-
}
83-
8456
func (s *Store) transport() *http.Transport {
8557
switch {
8658
case s.telemetry:
@@ -144,9 +116,9 @@ type CredentialsGetter interface {
144116
}
145117

146118
// WithAuthentication sets the store credentials.
147-
func WithAuthentication(c CredentialsGetter) Option {
119+
func WithAuthentication() Option {
148120
return func(s *Store) error {
149-
client, err := HTTPClient(c, s.transport())
121+
client, err := transport.HTTPClient(version.Version, s.transport())
150122
if err != nil {
151123
return err
152124
}
@@ -256,7 +228,7 @@ type AuthenticatedConfig interface {
256228

257229
// AuthenticatedPreset is the default Option when connecting to the public API with authentication.
258230
func AuthenticatedPreset(c AuthenticatedConfig) Option {
259-
options := []Option{Service(c.Service()), WithAuthentication(c)}
231+
options := []Option{Service(c.Service()), WithAuthentication()}
260232
if baseURLOpt := baseURLOption(c); baseURLOpt != nil {
261233
options = append(options, baseURLOpt)
262234
}

internal/store/store_test.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,42 @@ var _ AuthenticatedConfig = &testConfig{}
118118

119119
func TestWithAuthentication(t *testing.T) {
120120
tests := []struct {
121-
name string
122-
a auth
121+
name string
122+
setTestProfile func(p *config.Profile)
123123
}{
124124
{
125125
name: "api keys",
126-
a: auth{
127-
username: "username",
128-
password: "password",
126+
setTestProfile: func(p *config.Profile) {
127+
p.SetAuthType(config.APIKeys)
128+
p.SetPublicAPIKey("test-key")
129+
p.SetPrivateAPIKey("test-secret")
129130
},
130131
},
131132
{
132133
name: "service account",
133-
a: auth{
134-
clientID: "id",
135-
clientSecret: "secret",
134+
setTestProfile: func(p *config.Profile) {
135+
p.SetAuthType(config.ServiceAccount)
136+
p.SetClientID("id")
137+
p.SetClientSecret("secret")
136138
},
137139
},
138140
{
139141
name: "user account",
140-
a: auth{
141-
refreshToken: "token",
142-
accessToken: &atlasauth.Token{
143-
AccessToken: "access",
144-
RefreshToken: "refresh",
145-
},
142+
setTestProfile: func(p *config.Profile) {
143+
p.SetAuthType(config.UserAccount)
144+
p.SetAccessToken("token")
146145
},
147146
},
148147
}
149148

150149
for _, tt := range tests {
151150
t.Run(tt.name, func(t *testing.T) {
152-
c, err := New(Service("cloud"), WithAuthentication(tt.a))
151+
// Set up profile for testing
152+
profile := config.NewProfile("test", config.NewInMemoryStore())
153+
tt.setTestProfile(profile)
154+
config.SetDefaultProfile(profile)
155+
156+
c, err := New(Service("cloud"), WithAuthentication())
153157
require.NoError(t, err)
154158
require.NotNil(t, c.httpClient)
155159
require.NotNil(t, c.httpClient.Transport)

0 commit comments

Comments
 (0)