Skip to content

Commit

Permalink
Change base api URL (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icerzack authored Feb 21, 2024
1 parent 639aed1 commit de8f2ed
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 47 deletions.
2 changes: 1 addition & 1 deletion iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
appName = "iam-go"

// defaultIAMApiURL represents a default Selectel IAM API URL.
defaultIAMApiURL = "https://api.selectel.ru/iam/v1"
defaultIAMApiURL = "https://api.selectel.ru"

// defaultHTTPTimeout represents the default timeout (in seconds) for HTTP requests.
defaultHTTPTimeout = 120
Expand Down
10 changes: 8 additions & 2 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"fmt"
"io"
"net/http"
"net/url"

"github.com/selectel/iam-go/iamerrors"
)

type DoRequestInput struct {
Body io.Reader
Method string
URL string
Path string
}

type BaseClient struct {
Expand All @@ -37,7 +38,12 @@ type BaseClient struct {
//
// X-Auth-Token and other optional headers are added automatically.
func (bc *BaseClient) DoRequest(ctx context.Context, input DoRequestInput) ([]byte, error) {
request, err := http.NewRequestWithContext(ctx, input.Method, input.URL, input.Body)
url, err := url.JoinPath(bc.APIUrl, input.Path)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

request, err := http.NewRequestWithContext(ctx, input.Method, url, input.Body)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestDoRequest(t *testing.T) {
actualBody, err := baseClient.DoRequest(ctx, DoRequestInput{
Body: tt.args.body,
Method: tt.args.method,
URL: baseClient.APIUrl,
Path: "/",
})

require.ErrorIs(err, tt.expectedError)
Expand Down
15 changes: 8 additions & 7 deletions service/ec2/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/selectel/iam-go/internal/client"
)

const apiVersion = "iam/v1"

// EC2 is used to communicate with the EC2 Credentials API.
type EC2 struct {
baseClient *client.BaseClient
Expand All @@ -29,15 +31,15 @@ func (ec2 *EC2) List(ctx context.Context, userID string) ([]Credential, error) {
return nil, iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
}

url, err := url.JoinPath(ec2.baseClient.APIUrl, "service_users", userID, "credentials")
path, err := url.JoinPath(apiVersion, "service_users", userID, "credentials")
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

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

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

url, err := url.JoinPath(ec2.baseClient.APIUrl, "service_users", userID, "credentials", accessKey)
path, err := url.JoinPath(apiVersion, "service_users", userID, "credentials", accessKey)
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

_, err = ec2.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: nil,
Method: http.MethodDelete,
URL: url,
Path: path,
})
if err != nil {
//nolint:wrapcheck // DoRequest already wraps the error.
Expand Down
3 changes: 2 additions & 1 deletion service/ec2/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

const (
credentialsURL = "/service_users/1/credentials"
//nolint:gosec
credentialsURL = "iam/v1/service_users/1/credentials"
)

func TestList(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions service/ec2/testdata/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package testdata

const (
TestToken = "test-token"
TestURL = "http://example.org"
TestURL = "http://example.org/"
)

const TestGetCredentialsResponse = `{
Expand All @@ -13,7 +13,6 @@ const TestGetCredentialsResponse = `{
}]
}`

// nolint gosec complains
const TestCreateCredentialResponse = `{
"name": "12345",
"project_id": "test-project",
Expand Down
26 changes: 14 additions & 12 deletions service/serviceusers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/selectel/iam-go/internal/client"
)

const apiVersion = "iam/v1"

// ServiceUsers is used to communicate with the Service Users API.
type ServiceUsers struct {
baseClient *client.BaseClient
Expand All @@ -25,15 +27,15 @@ func New(baseClient *client.BaseClient) *ServiceUsers {

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

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

url, err := url.JoinPath(su.baseClient.APIUrl, "service_users", userID)
path, err := url.JoinPath(apiVersion, "service_users", userID)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

response, err := su.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: nil,
Method: http.MethodGet,
URL: url,
Path: path,
})
if err != nil {
//nolint:wrapcheck // DoRequest already wraps the error.
Expand Down Expand Up @@ -90,7 +92,7 @@ func (su *ServiceUsers) Create(ctx context.Context, input CreateRequest) (*Servi
}
}

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

url, err := url.JoinPath(su.baseClient.APIUrl, "service_users", userID)
path, err := url.JoinPath(apiVersion, "service_users", userID)
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

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

url, err := url.JoinPath(su.baseClient.APIUrl, "service_users", userID)
path, err := url.JoinPath(apiVersion, "service_users", userID)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}
Expand All @@ -169,7 +171,7 @@ func (su *ServiceUsers) Update(ctx context.Context, userID string, input UpdateR
response, err := su.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: bytes.NewReader(body),
Method: http.MethodPatch,
URL: url,
Path: path,
})
if err != nil {
//nolint:wrapcheck // DoRequest already wraps the error.
Expand Down Expand Up @@ -215,7 +217,7 @@ func (su *ServiceUsers) UnassignRoles(ctx context.Context, userID string, roles
}

func (su *ServiceUsers) manageRoles(ctx context.Context, method string, userID string, roles []Role) error {
url, err := url.JoinPath(su.baseClient.APIUrl, "service_users", userID, "roles")
path, err := url.JoinPath(apiVersion, "service_users", userID, "roles")
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}
Expand All @@ -229,7 +231,7 @@ func (su *ServiceUsers) manageRoles(ctx context.Context, method string, userID s
_, err = su.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: bytes.NewReader(body),
Method: method,
URL: url,
Path: path,
})
if err != nil {
//nolint:wrapcheck // DoRequest already wraps the error.
Expand Down
6 changes: 3 additions & 3 deletions service/serviceusers/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
)

const (
serviceUsersURL = "/service_users"
serviceUsersIDURL = "/service_users/123"
serviceUsersRolesURL = "/service_users/123/roles"
serviceUsersURL = "iam/v1/service_users"
serviceUsersIDURL = "iam/v1/service_users/123"
serviceUsersRolesURL = "iam/v1/service_users/123/roles"
)

func TestList(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion service/serviceusers/testdata/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package testdata

const (
TestToken = "test-token"
TestURL = "http://example.org"
TestURL = "http://example.org/"
)

const TestGetUsersResponse = `{
Expand Down
26 changes: 14 additions & 12 deletions service/users/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/selectel/iam-go/internal/client"
)

const apiVersion = "iam/v1"

// Users is used to communicate with the Users API.
type Users struct {
baseClient *client.BaseClient
Expand All @@ -25,15 +27,15 @@ func New(baseClient *client.BaseClient) *Users {

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

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

url, err := url.JoinPath(u.baseClient.APIUrl, "users", userID)
path, err := url.JoinPath(apiVersion, "users", userID)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

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

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

url, err := url.JoinPath(u.baseClient.APIUrl, "users", userID)
path, err := url.JoinPath(apiVersion, "users", userID)
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

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

url, err := url.JoinPath(u.baseClient.APIUrl, "users", userID, "resend_invite")
path, err := url.JoinPath(apiVersion, "users", userID, "resend_invite")
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

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

func (u *Users) manageRoles(ctx context.Context, method string, userID string, roles []Role) error {
url, err := url.JoinPath(u.baseClient.APIUrl, "users", userID, "roles")
path, err := url.JoinPath(apiVersion, "users", userID, "roles")
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}
Expand All @@ -206,7 +208,7 @@ func (u *Users) manageRoles(ctx context.Context, method string, userID string, r
_, err = u.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: bytes.NewReader(body),
Method: method,
URL: url,
Path: path,
})
if err != nil {
//nolint:wrapcheck // DoRequest already wraps the error.
Expand Down
Loading

0 comments on commit de8f2ed

Please sign in to comment.