Skip to content

Commit

Permalink
feat!: add context argument to methods (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored and Serpentiel committed Aug 7, 2023
1 parent d0f75e0 commit ed04302
Show file tree
Hide file tree
Showing 74 changed files with 858 additions and 632 deletions.
21 changes: 11 additions & 10 deletions account_authentications.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aiven

import (
"context"
"errors"
"time"
)
Expand Down Expand Up @@ -94,13 +95,13 @@ type (
)

// List returns a list of all available account authentication methods
func (h AccountAuthenticationsHandler) List(accountId string) (*AccountAuthenticationListResponse, error) {
func (h AccountAuthenticationsHandler) List(ctx context.Context, accountId string) (*AccountAuthenticationListResponse, error) {
if accountId == "" {
return nil, errors.New("cannot get a list of account authentication methods when account id is empty")
}

path := buildPath("account", accountId, "authentication")
bts, err := h.client.doGetRequest(path, nil)
bts, err := h.client.doGetRequest(ctx, path, nil)
if err != nil {
return nil, err
}
Expand All @@ -114,13 +115,13 @@ func (h AccountAuthenticationsHandler) List(accountId string) (*AccountAuthentic
}

// Get returns a list of all available account authentication methods
func (h AccountAuthenticationsHandler) Get(accountId, authId string) (*AccountAuthenticationResponse, error) {
func (h AccountAuthenticationsHandler) Get(ctx context.Context, accountId, authId string) (*AccountAuthenticationResponse, error) {
if accountId == "" || authId == "" {
return nil, errors.New("cannot get an account authentication method when account id or auth id is empty")
}

path := buildPath("account", accountId, "authentication", authId)
bts, err := h.client.doGetRequest(path, nil)
bts, err := h.client.doGetRequest(ctx, path, nil)
if err != nil {
return nil, err
}
Expand All @@ -134,13 +135,13 @@ func (h AccountAuthenticationsHandler) Get(accountId, authId string) (*AccountAu
}

// Create creates an account authentication method
func (h AccountAuthenticationsHandler) Create(accountId string, a AccountAuthenticationMethodCreate) (*AccountAuthenticationResponse, error) {
func (h AccountAuthenticationsHandler) Create(ctx context.Context, accountId string, a AccountAuthenticationMethodCreate) (*AccountAuthenticationResponse, error) {
if accountId == "" {
return nil, errors.New("cannot create an account authentication method when account id is empty")
}

path := buildPath("account", accountId, "authentication")
bts, err := h.client.doPostRequest(path, a)
bts, err := h.client.doPostRequest(ctx, path, a)
if err != nil {
return nil, err
}
Expand All @@ -154,13 +155,13 @@ func (h AccountAuthenticationsHandler) Create(accountId string, a AccountAuthent
}

// Update updates an account authentication method empty fields are omitted, acts like PATCH
func (h AccountAuthenticationsHandler) Update(accountId, accountAuthMethID string, a AccountAuthenticationMethodUpdate) (*AccountAuthenticationResponse, error) {
func (h AccountAuthenticationsHandler) Update(ctx context.Context, accountId, accountAuthMethID string, a AccountAuthenticationMethodUpdate) (*AccountAuthenticationResponse, error) {
if accountId == "" || accountAuthMethID == "" {
return nil, errors.New("cannot update an account authentication method when account id or auth id is empty")
}

path := buildPath("account", accountId, "authentication", accountAuthMethID)
bts, err := h.client.doPutRequest(path, a)
bts, err := h.client.doPutRequest(ctx, path, a)
if err != nil {
return nil, err
}
Expand All @@ -174,13 +175,13 @@ func (h AccountAuthenticationsHandler) Update(accountId, accountAuthMethID strin
}

// Delete deletes an account authentication method
func (h AccountAuthenticationsHandler) Delete(accountId, authId string) error {
func (h AccountAuthenticationsHandler) Delete(ctx context.Context, accountId, authId string) error {
if accountId == "" || authId == "" {
return errors.New("cannot delete an account authentication method when account id or auth id is empty")
}

path := buildPath("account", accountId, "authentication", authId)
bts, err := h.client.doDeleteRequest(path, nil)
bts, err := h.client.doDeleteRequest(ctx, path, nil)
if err != nil {
return err
}
Expand Down
22 changes: 16 additions & 6 deletions account_authentications_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aiven

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -122,7 +123,6 @@ func setupAccountAuthenticationsTestCase(t *testing.T) (*Client, func(t *testing
}))

apiUrl = ts.URL

c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version())
if err != nil {
t.Fatalf("user authentication error: %s", err)
Expand All @@ -138,6 +138,8 @@ func TestAccountAuthenticationsHandler_List(t *testing.T) {
c, tearDown := setupAccountAuthenticationsTestCase(t)
defer tearDown(t)

ctx := context.Background()

type fields struct {
client *Client
}
Expand Down Expand Up @@ -186,7 +188,7 @@ func TestAccountAuthenticationsHandler_List(t *testing.T) {
h := AccountAuthenticationsHandler{
client: tt.fields.client,
}
got, err := h.List(tt.args.accountId)
got, err := h.List(ctx, tt.args.accountId)
if (err != nil) != tt.wantErr {
t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -202,6 +204,8 @@ func TestAccountAuthenticationsHandler_Create(t *testing.T) {
c, tearDown := setupAccountAuthenticationsTestCase(t)
defer tearDown(t)

ctx := context.Background()

type fields struct {
client *Client
}
Expand Down Expand Up @@ -262,7 +266,7 @@ func TestAccountAuthenticationsHandler_Create(t *testing.T) {
h := AccountAuthenticationsHandler{
client: tt.fields.client,
}
got, err := h.Create(tt.args.accountId, tt.args.a)
got, err := h.Create(ctx, tt.args.accountId, tt.args.a)
if (err != nil) != tt.wantErr {
t.Errorf("Create() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -278,6 +282,8 @@ func TestAccountAuthenticationsHandler_Update(t *testing.T) {
c, tearDown := setupAccountAuthenticationsTestCase(t)
defer tearDown(t)

ctx := context.Background()

type fields struct {
client *Client
}
Expand Down Expand Up @@ -350,7 +356,7 @@ func TestAccountAuthenticationsHandler_Update(t *testing.T) {
h := AccountAuthenticationsHandler{
client: tt.fields.client,
}
got, err := h.Update(tt.args.accountId, tt.args.accountAuthMethId, tt.args.a)
got, err := h.Update(ctx, tt.args.accountId, tt.args.accountAuthMethId, tt.args.a)
if (err != nil) != tt.wantErr {
t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -366,6 +372,8 @@ func TestAccountAuthenticationsHandler_Delete(t *testing.T) {
c, tearDown := setupAccountAuthenticationsTestCase(t)
defer tearDown(t)

ctx := context.Background()

type fields struct {
client *Client
}
Expand Down Expand Up @@ -411,7 +419,7 @@ func TestAccountAuthenticationsHandler_Delete(t *testing.T) {
h := AccountAuthenticationsHandler{
client: tt.fields.client,
}
if err := h.Delete(tt.args.accountId, tt.args.authId); (err != nil) != tt.wantErr {
if err := h.Delete(ctx, tt.args.accountId, tt.args.authId); (err != nil) != tt.wantErr {
t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand All @@ -422,6 +430,8 @@ func TestAccountAuthenticationsHandler_Get(t *testing.T) {
c, tearDown := setupAccountAuthenticationsTestCase(t)
defer tearDown(t)

ctx := context.Background()

type fields struct {
client *Client
}
Expand Down Expand Up @@ -483,7 +493,7 @@ func TestAccountAuthenticationsHandler_Get(t *testing.T) {
h := AccountAuthenticationsHandler{
client: tt.fields.client,
}
got, err := h.Get(tt.args.accountId, tt.args.authId)
got, err := h.Get(ctx, tt.args.accountId, tt.args.authId)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
9 changes: 5 additions & 4 deletions account_team_invites.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aiven

import (
"context"
"errors"
"time"
)
Expand Down Expand Up @@ -30,13 +31,13 @@ type (
)

// List returns a list of all available account invitations
func (h AccountTeamInvitesHandler) List(accountId, teamId string) (*AccountTeamInvitesResponse, error) {
func (h AccountTeamInvitesHandler) List(ctx context.Context, accountId, teamId string) (*AccountTeamInvitesResponse, error) {
if accountId == "" || teamId == "" {
return nil, errors.New("cannot get a list of account team invites when account id or team id is empty")
}

path := buildPath("account", accountId, "team", teamId, "invites")
bts, err := h.client.doGetRequest(path, nil)
bts, err := h.client.doGetRequest(ctx, path, nil)
if err != nil {
return nil, err
}
Expand All @@ -50,13 +51,13 @@ func (h AccountTeamInvitesHandler) List(accountId, teamId string) (*AccountTeamI
}

// Delete deletes a list of all available account invitations
func (h AccountTeamInvitesHandler) Delete(accountId, teamId, userEmail string) error {
func (h AccountTeamInvitesHandler) Delete(ctx context.Context, accountId, teamId, userEmail string) error {
if accountId == "" || teamId == "" || userEmail == "" {
return errors.New("cannot delete an account team invite when account id or team id or user email is empty")
}

path := buildPath("account", accountId, "team", teamId, "invites", userEmail)
bts, err := h.client.doDeleteRequest(path, nil)
bts, err := h.client.doDeleteRequest(ctx, path, nil)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions account_team_invites_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aiven

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -71,7 +72,6 @@ func setupAccountTeamInvitesTestCase(t *testing.T) (*Client, func(t *testing.T))
}))

apiUrl = ts.URL

c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version())
if err != nil {
t.Fatalf("user authentication error: %s", err)
Expand Down Expand Up @@ -145,12 +145,13 @@ func TestAccountTeamInvitesHandler_List(t *testing.T) {
true,
},
}
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := AccountTeamInvitesHandler{
client: tt.fields.client,
}
got, err := h.List(tt.args.accountId, tt.args.teamId)
got, err := h.List(ctx, tt.args.accountId, tt.args.teamId)
if (err != nil) != tt.wantErr {
t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -221,12 +222,13 @@ func TestAccountTeamInvitesHandler_Delete(t *testing.T) {
true,
},
}
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := AccountTeamInvitesHandler{
client: tt.fields.client,
}
if err := h.Delete(tt.args.accountId, tt.args.teamId, tt.args.userEmail); (err != nil) != tt.wantErr {
if err := h.Delete(ctx, tt.args.accountId, tt.args.teamId, tt.args.userEmail); (err != nil) != tt.wantErr {
t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
13 changes: 7 additions & 6 deletions account_team_members.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aiven

import (
"context"
"errors"
"time"
)
Expand Down Expand Up @@ -36,13 +37,13 @@ type (
)

// List returns a list of all existing account team members
func (h AccountTeamMembersHandler) List(accountId, teamId string) (*AccountTeamMembersResponse, error) {
func (h AccountTeamMembersHandler) List(ctx context.Context, accountId, teamId string) (*AccountTeamMembersResponse, error) {
if accountId == "" || teamId == "" {
return nil, errors.New("cannot get a list of team members when account id or team id is empty")
}

path := buildPath("account", accountId, "team", teamId, "members")
bts, err := h.client.doGetRequest(path, nil)
bts, err := h.client.doGetRequest(ctx, path, nil)
if err != nil {
return nil, err
}
Expand All @@ -55,7 +56,7 @@ func (h AccountTeamMembersHandler) List(accountId, teamId string) (*AccountTeamM
}

// Invite invites a team member
func (h AccountTeamMembersHandler) Invite(accountId, teamId, email string) error {
func (h AccountTeamMembersHandler) Invite(ctx context.Context, accountId, teamId, email string) error {
if accountId == "" || teamId == "" {
return errors.New("cannot invite a team members when account id or team id is empty")
}
Expand All @@ -65,7 +66,7 @@ func (h AccountTeamMembersHandler) Invite(accountId, teamId, email string) error
}

path := buildPath("account", accountId, "team", teamId, "members")
bts, err := h.client.doPostRequest(path, struct {
bts, err := h.client.doPostRequest(ctx, path, struct {
Email string `json:"email"`
}{Email: email})
if err != nil {
Expand All @@ -76,13 +77,13 @@ func (h AccountTeamMembersHandler) Invite(accountId, teamId, email string) error
}

// Delete deletes an existing account team member
func (h AccountTeamMembersHandler) Delete(accountId, teamId, userId string) error {
func (h AccountTeamMembersHandler) Delete(ctx context.Context, accountId, teamId, userId string) error {
if accountId == "" || teamId == "" || userId == "" {
return errors.New("cannot delete a team member when account id or team id or user id is empty")
}

path := buildPath("account", accountId, "team", teamId, "member", userId)
bts, err := h.client.doDeleteRequest(path, nil)
bts, err := h.client.doDeleteRequest(ctx, path, nil)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit ed04302

Please sign in to comment.