Skip to content

Commit de8f2ed

Browse files
authored
Change base api URL (#2)
1 parent 639aed1 commit de8f2ed

File tree

12 files changed

+58
-47
lines changed

12 files changed

+58
-47
lines changed

iam.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
appName = "iam-go"
1818

1919
// defaultIAMApiURL represents a default Selectel IAM API URL.
20-
defaultIAMApiURL = "https://api.selectel.ru/iam/v1"
20+
defaultIAMApiURL = "https://api.selectel.ru"
2121

2222
// defaultHTTPTimeout represents the default timeout (in seconds) for HTTP requests.
2323
defaultHTTPTimeout = 120

internal/client/client.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"net/url"
910

1011
"github.com/selectel/iam-go/iamerrors"
1112
)
1213

1314
type DoRequestInput struct {
1415
Body io.Reader
1516
Method string
16-
URL string
17+
Path string
1718
}
1819

1920
type BaseClient struct {
@@ -37,7 +38,12 @@ type BaseClient struct {
3738
//
3839
// X-Auth-Token and other optional headers are added automatically.
3940
func (bc *BaseClient) DoRequest(ctx context.Context, input DoRequestInput) ([]byte, error) {
40-
request, err := http.NewRequestWithContext(ctx, input.Method, input.URL, input.Body)
41+
url, err := url.JoinPath(bc.APIUrl, input.Path)
42+
if err != nil {
43+
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
44+
}
45+
46+
request, err := http.NewRequestWithContext(ctx, input.Method, url, input.Body)
4147
if err != nil {
4248
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
4349
}

internal/client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestDoRequest(t *testing.T) {
102102
actualBody, err := baseClient.DoRequest(ctx, DoRequestInput{
103103
Body: tt.args.body,
104104
Method: tt.args.method,
105-
URL: baseClient.APIUrl,
105+
Path: "/",
106106
})
107107

108108
require.ErrorIs(err, tt.expectedError)

service/ec2/requests.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/selectel/iam-go/internal/client"
1212
)
1313

14+
const apiVersion = "iam/v1"
15+
1416
// EC2 is used to communicate with the EC2 Credentials API.
1517
type EC2 struct {
1618
baseClient *client.BaseClient
@@ -29,15 +31,15 @@ func (ec2 *EC2) List(ctx context.Context, userID string) ([]Credential, error) {
2931
return nil, iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
3032
}
3133

32-
url, err := url.JoinPath(ec2.baseClient.APIUrl, "service_users", userID, "credentials")
34+
path, err := url.JoinPath(apiVersion, "service_users", userID, "credentials")
3335
if err != nil {
3436
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
3537
}
3638

3739
response, err := ec2.baseClient.DoRequest(ctx, client.DoRequestInput{
3840
Body: nil,
3941
Method: http.MethodGet,
40-
URL: url,
42+
Path: path,
4143
})
4244
if err != nil {
4345
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -64,7 +66,7 @@ func (ec2 *EC2) Create(ctx context.Context, userID, name, projectID string) (*Cr
6466
return nil, iamerrors.Error{Err: iamerrors.ErrProjectIDRequired, Desc: "No projectID was provided."}
6567
}
6668

67-
url, err := url.JoinPath(ec2.baseClient.APIUrl, "service_users", userID, "credentials")
69+
path, err := url.JoinPath(apiVersion, "service_users", userID, "credentials")
6870
if err != nil {
6971
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
7072
}
@@ -77,7 +79,7 @@ func (ec2 *EC2) Create(ctx context.Context, userID, name, projectID string) (*Cr
7779
response, err := ec2.baseClient.DoRequest(ctx, client.DoRequestInput{
7880
Body: bytes.NewReader(body),
7981
Method: http.MethodPost,
80-
URL: url,
82+
Path: path,
8183
})
8284
if err != nil {
8385
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -101,15 +103,14 @@ func (ec2 *EC2) Delete(ctx context.Context, userID, accessKey string) error {
101103
return iamerrors.Error{Err: iamerrors.ErrCredentialAccessKeyRequired, Desc: "No accessKey was provided."}
102104
}
103105

104-
url, err := url.JoinPath(ec2.baseClient.APIUrl, "service_users", userID, "credentials", accessKey)
106+
path, err := url.JoinPath(apiVersion, "service_users", userID, "credentials", accessKey)
105107
if err != nil {
106108
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
107109
}
108-
109110
_, err = ec2.baseClient.DoRequest(ctx, client.DoRequestInput{
110111
Body: nil,
111112
Method: http.MethodDelete,
112-
URL: url,
113+
Path: path,
113114
})
114115
if err != nil {
115116
//nolint:wrapcheck // DoRequest already wraps the error.

service/ec2/requests_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import (
1515
)
1616

1717
const (
18-
credentialsURL = "/service_users/1/credentials"
18+
//nolint:gosec
19+
credentialsURL = "iam/v1/service_users/1/credentials"
1920
)
2021

2122
func TestList(t *testing.T) {

service/ec2/testdata/fixtures.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package testdata
22

33
const (
44
TestToken = "test-token"
5-
TestURL = "http://example.org"
5+
TestURL = "http://example.org/"
66
)
77

88
const TestGetCredentialsResponse = `{
@@ -13,7 +13,6 @@ const TestGetCredentialsResponse = `{
1313
}]
1414
}`
1515

16-
// nolint gosec complains
1716
const TestCreateCredentialResponse = `{
1817
"name": "12345",
1918
"project_id": "test-project",

service/serviceusers/requests.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/selectel/iam-go/internal/client"
1212
)
1313

14+
const apiVersion = "iam/v1"
15+
1416
// ServiceUsers is used to communicate with the Service Users API.
1517
type ServiceUsers struct {
1618
baseClient *client.BaseClient
@@ -25,15 +27,15 @@ func New(baseClient *client.BaseClient) *ServiceUsers {
2527

2628
// List returns a list of Service Users for the account.
2729
func (su *ServiceUsers) List(ctx context.Context) ([]ServiceUser, error) {
28-
url, err := url.JoinPath(su.baseClient.APIUrl, "service_users")
30+
path, err := url.JoinPath(apiVersion, "service_users")
2931
if err != nil {
3032
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
3133
}
3234

3335
response, err := su.baseClient.DoRequest(ctx, client.DoRequestInput{
3436
Body: nil,
3537
Method: http.MethodGet,
36-
URL: url,
38+
Path: path,
3739
})
3840
if err != nil {
3941
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -54,15 +56,15 @@ func (su *ServiceUsers) Get(ctx context.Context, userID string) (*ServiceUser, e
5456
return nil, iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
5557
}
5658

57-
url, err := url.JoinPath(su.baseClient.APIUrl, "service_users", userID)
59+
path, err := url.JoinPath(apiVersion, "service_users", userID)
5860
if err != nil {
5961
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
6062
}
6163

6264
response, err := su.baseClient.DoRequest(ctx, client.DoRequestInput{
6365
Body: nil,
6466
Method: http.MethodGet,
65-
URL: url,
67+
Path: path,
6668
})
6769
if err != nil {
6870
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -90,7 +92,7 @@ func (su *ServiceUsers) Create(ctx context.Context, input CreateRequest) (*Servi
9092
}
9193
}
9294

93-
url, err := url.JoinPath(su.baseClient.APIUrl, "service_users")
95+
path, err := url.JoinPath(apiVersion, "service_users")
9496
if err != nil {
9597
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
9698
}
@@ -107,7 +109,7 @@ func (su *ServiceUsers) Create(ctx context.Context, input CreateRequest) (*Servi
107109
response, err := su.baseClient.DoRequest(ctx, client.DoRequestInput{
108110
Body: bytes.NewReader(body),
109111
Method: http.MethodPost,
110-
URL: url,
112+
Path: path,
111113
})
112114
if err != nil {
113115
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -128,15 +130,15 @@ func (su *ServiceUsers) Delete(ctx context.Context, userID string) error {
128130
return iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
129131
}
130132

131-
url, err := url.JoinPath(su.baseClient.APIUrl, "service_users", userID)
133+
path, err := url.JoinPath(apiVersion, "service_users", userID)
132134
if err != nil {
133135
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
134136
}
135137

136138
_, err = su.baseClient.DoRequest(ctx, client.DoRequestInput{
137139
Body: nil,
138140
Method: http.MethodDelete,
139-
URL: url,
141+
Path: path,
140142
})
141143
if err != nil {
142144
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -152,7 +154,7 @@ func (su *ServiceUsers) Update(ctx context.Context, userID string, input UpdateR
152154
return nil, iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
153155
}
154156

155-
url, err := url.JoinPath(su.baseClient.APIUrl, "service_users", userID)
157+
path, err := url.JoinPath(apiVersion, "service_users", userID)
156158
if err != nil {
157159
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
158160
}
@@ -169,7 +171,7 @@ func (su *ServiceUsers) Update(ctx context.Context, userID string, input UpdateR
169171
response, err := su.baseClient.DoRequest(ctx, client.DoRequestInput{
170172
Body: bytes.NewReader(body),
171173
Method: http.MethodPatch,
172-
URL: url,
174+
Path: path,
173175
})
174176
if err != nil {
175177
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -215,7 +217,7 @@ func (su *ServiceUsers) UnassignRoles(ctx context.Context, userID string, roles
215217
}
216218

217219
func (su *ServiceUsers) manageRoles(ctx context.Context, method string, userID string, roles []Role) error {
218-
url, err := url.JoinPath(su.baseClient.APIUrl, "service_users", userID, "roles")
220+
path, err := url.JoinPath(apiVersion, "service_users", userID, "roles")
219221
if err != nil {
220222
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
221223
}
@@ -229,7 +231,7 @@ func (su *ServiceUsers) manageRoles(ctx context.Context, method string, userID s
229231
_, err = su.baseClient.DoRequest(ctx, client.DoRequestInput{
230232
Body: bytes.NewReader(body),
231233
Method: method,
232-
URL: url,
234+
Path: path,
233235
})
234236
if err != nil {
235237
//nolint:wrapcheck // DoRequest already wraps the error.

service/serviceusers/requests_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
)
1616

1717
const (
18-
serviceUsersURL = "/service_users"
19-
serviceUsersIDURL = "/service_users/123"
20-
serviceUsersRolesURL = "/service_users/123/roles"
18+
serviceUsersURL = "iam/v1/service_users"
19+
serviceUsersIDURL = "iam/v1/service_users/123"
20+
serviceUsersRolesURL = "iam/v1/service_users/123/roles"
2121
)
2222

2323
func TestList(t *testing.T) {

service/serviceusers/testdata/fixtures.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package testdata
22

33
const (
44
TestToken = "test-token"
5-
TestURL = "http://example.org"
5+
TestURL = "http://example.org/"
66
)
77

88
const TestGetUsersResponse = `{

service/users/requests.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/selectel/iam-go/internal/client"
1212
)
1313

14+
const apiVersion = "iam/v1"
15+
1416
// Users is used to communicate with the Users API.
1517
type Users struct {
1618
baseClient *client.BaseClient
@@ -25,15 +27,15 @@ func New(baseClient *client.BaseClient) *Users {
2527

2628
// List returns a list of Users for the account.
2729
func (u *Users) List(ctx context.Context) ([]User, error) {
28-
url, err := url.JoinPath(u.baseClient.APIUrl, "users")
30+
path, err := url.JoinPath(apiVersion, "users")
2931
if err != nil {
3032
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
3133
}
3234

3335
response, err := u.baseClient.DoRequest(ctx, client.DoRequestInput{
3436
Body: nil,
3537
Method: http.MethodGet,
36-
URL: url,
38+
Path: path,
3739
})
3840
if err != nil {
3941
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -54,15 +56,15 @@ func (u *Users) Get(ctx context.Context, userID string) (*User, error) {
5456
return nil, iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
5557
}
5658

57-
url, err := url.JoinPath(u.baseClient.APIUrl, "users", userID)
59+
path, err := url.JoinPath(apiVersion, "users", userID)
5860
if err != nil {
5961
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
6062
}
6163

6264
response, err := u.baseClient.DoRequest(ctx, client.DoRequestInput{
6365
Body: nil,
6466
Method: http.MethodGet,
65-
URL: url,
67+
Path: path,
6668
})
6769
if err != nil {
6870
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -83,7 +85,7 @@ func (u *Users) Create(ctx context.Context, input CreateRequest) (*User, error)
8385
return nil, iamerrors.Error{Err: iamerrors.ErrUserEmailRequired, Desc: "No email for User was provided."}
8486
}
8587

86-
url, err := url.JoinPath(u.baseClient.APIUrl, "users")
88+
path, err := url.JoinPath(apiVersion, "users")
8789
if err != nil {
8890
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
8991
}
@@ -103,7 +105,7 @@ func (u *Users) Create(ctx context.Context, input CreateRequest) (*User, error)
103105
response, err := u.baseClient.DoRequest(ctx, client.DoRequestInput{
104106
Body: bytes.NewReader(body),
105107
Method: http.MethodPost,
106-
URL: url,
108+
Path: path,
107109
})
108110
if err != nil {
109111
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -124,15 +126,15 @@ func (u *Users) Delete(ctx context.Context, userID string) error {
124126
return iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
125127
}
126128

127-
url, err := url.JoinPath(u.baseClient.APIUrl, "users", userID)
129+
path, err := url.JoinPath(apiVersion, "users", userID)
128130
if err != nil {
129131
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
130132
}
131133

132134
_, err = u.baseClient.DoRequest(ctx, client.DoRequestInput{
133135
Body: nil,
134136
Method: http.MethodDelete,
135-
URL: url,
137+
Path: path,
136138
})
137139
if err != nil {
138140
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -148,15 +150,15 @@ func (u *Users) ResendInvite(ctx context.Context, userID string) error {
148150
return iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
149151
}
150152

151-
url, err := url.JoinPath(u.baseClient.APIUrl, "users", userID, "resend_invite")
153+
path, err := url.JoinPath(apiVersion, "users", userID, "resend_invite")
152154
if err != nil {
153155
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
154156
}
155157

156158
_, err = u.baseClient.DoRequest(ctx, client.DoRequestInput{
157159
Body: nil,
158160
Method: http.MethodPatch,
159-
URL: url,
161+
Path: path,
160162
})
161163
if err != nil {
162164
//nolint:wrapcheck // DoRequest already wraps the error.
@@ -192,7 +194,7 @@ func (u *Users) UnassignRoles(ctx context.Context, userID string, roles []Role)
192194
}
193195

194196
func (u *Users) manageRoles(ctx context.Context, method string, userID string, roles []Role) error {
195-
url, err := url.JoinPath(u.baseClient.APIUrl, "users", userID, "roles")
197+
path, err := url.JoinPath(apiVersion, "users", userID, "roles")
196198
if err != nil {
197199
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
198200
}
@@ -206,7 +208,7 @@ func (u *Users) manageRoles(ctx context.Context, method string, userID string, r
206208
_, err = u.baseClient.DoRequest(ctx, client.DoRequestInput{
207209
Body: bytes.NewReader(body),
208210
Method: method,
209-
URL: url,
211+
Path: path,
210212
})
211213
if err != nil {
212214
//nolint:wrapcheck // DoRequest already wraps the error.

0 commit comments

Comments
 (0)