diff --git a/categories_gen.go b/categories_gen.go index d82bb34..ef0598a 100644 --- a/categories_gen.go +++ b/categories_gen.go @@ -8,19 +8,19 @@ package hop // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Channels.Tokens. type ClientCategoryChannelsTokens struct { - c *Client + c clientDoer } // ClientCategoryChannels is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Channels. type ClientCategoryChannels struct { - c *Client + c clientDoer Tokens *ClientCategoryChannelsTokens } -func newChannels(c *Client) *ClientCategoryChannels { +func newChannels(c clientDoer) *ClientCategoryChannels { return &ClientCategoryChannels{ c: c, Tokens: &ClientCategoryChannelsTokens{c}, @@ -31,35 +31,35 @@ func newChannels(c *Client) *ClientCategoryChannels { // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Ignite.Gateways. type ClientCategoryIgniteGateways struct { - c *Client + c clientDoer } // ClientCategoryIgniteDeployments is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Ignite.Deployments. type ClientCategoryIgniteDeployments struct { - c *Client + c clientDoer } // ClientCategoryIgniteContainers is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Ignite.Containers. type ClientCategoryIgniteContainers struct { - c *Client + c clientDoer } // ClientCategoryIgnite is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Ignite. type ClientCategoryIgnite struct { - c *Client + c clientDoer Gateways *ClientCategoryIgniteGateways Deployments *ClientCategoryIgniteDeployments Containers *ClientCategoryIgniteContainers } -func newIgnite(c *Client) *ClientCategoryIgnite { +func newIgnite(c clientDoer) *ClientCategoryIgnite { return &ClientCategoryIgnite{ c: c, Gateways: &ClientCategoryIgniteGateways{c}, @@ -72,19 +72,19 @@ func newIgnite(c *Client) *ClientCategoryIgnite { // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Pipe.Rooms. type ClientCategoryPipeRooms struct { - c *Client + c clientDoer } // ClientCategoryPipe is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Pipe. type ClientCategoryPipe struct { - c *Client + c clientDoer Rooms *ClientCategoryPipeRooms } -func newPipe(c *Client) *ClientCategoryPipe { +func newPipe(c clientDoer) *ClientCategoryPipe { return &ClientCategoryPipe{ c: c, Rooms: &ClientCategoryPipeRooms{c}, @@ -95,27 +95,27 @@ func newPipe(c *Client) *ClientCategoryPipe { // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Projects.Tokens. type ClientCategoryProjectsTokens struct { - c *Client + c clientDoer } // ClientCategoryProjectsSecrets is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Projects.Secrets. type ClientCategoryProjectsSecrets struct { - c *Client + c clientDoer } // ClientCategoryProjects is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Projects. type ClientCategoryProjects struct { - c *Client + c clientDoer Tokens *ClientCategoryProjectsTokens Secrets *ClientCategoryProjectsSecrets } -func newProjects(c *Client) *ClientCategoryProjects { +func newProjects(c clientDoer) *ClientCategoryProjects { return &ClientCategoryProjects{ c: c, Tokens: &ClientCategoryProjectsTokens{c}, @@ -127,19 +127,19 @@ func newProjects(c *Client) *ClientCategoryProjects { // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Registry.Images. type ClientCategoryRegistryImages struct { - c *Client + c clientDoer } // ClientCategoryRegistry is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Registry. type ClientCategoryRegistry struct { - c *Client + c clientDoer Images *ClientCategoryRegistryImages } -func newRegistry(c *Client) *ClientCategoryRegistry { +func newRegistry(c clientDoer) *ClientCategoryRegistry { return &ClientCategoryRegistry{ c: c, Images: &ClientCategoryRegistryImages{c}, @@ -150,19 +150,19 @@ func newRegistry(c *Client) *ClientCategoryRegistry { // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Users.Me. type ClientCategoryUsersMe struct { - c *Client + c clientDoer } // ClientCategoryUsers is an auto-generated struct which is used to allow for simple categorisation of the APIs. // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field Users. type ClientCategoryUsers struct { - c *Client + c clientDoer Me *ClientCategoryUsersMe } -func newUsers(c *Client) *ClientCategoryUsers { +func newUsers(c clientDoer) *ClientCategoryUsers { return &ClientCategoryUsers{ c: c, Me: &ClientCategoryUsersMe{c}, diff --git a/channels.go b/channels.go index 798e5ef..8a0a16a 100644 --- a/channels.go +++ b/channels.go @@ -179,7 +179,7 @@ func (t ClientCategoryChannelsTokens) Delete(ctx context.Context, projectId, id // Create is used to create a new channel token. State is the map of the state of the token (this can be nil), and // projectId is the project ID to associate the token with (this can be empty unless it is bearer or PAT auth). func (t ClientCategoryChannelsTokens) Create(ctx context.Context, projectId string, state map[string]any) (*types.ChannelToken, error) { - if projectId == "" && t.c.tokenType != "ptk" { + if projectId == "" && t.c.getTokenType() != "ptk" { return nil, types.InvalidToken("project ID must be specified when creating a channel token with bearer or PAT auth") } if state == nil { diff --git a/client.go b/client.go index d0c63f1..3b0df97 100644 --- a/client.go +++ b/client.go @@ -111,6 +111,8 @@ type responseBody struct { Data json.RawMessage `json:"data"` } +func (c *Client) getTokenType() string { return c.tokenType } + // Does the specified HTTP request. func (c *Client) do(ctx context.Context, a clientArgs) error { // Handle getting the body bytes. @@ -238,9 +240,14 @@ func (c *Client) do(ctx context.Context, a clientArgs) error { return nil } +type clientDoer interface { + do(ctx context.Context, a clientArgs) error + getTokenType() string +} + // Paginator is used to create a way to access paginated API routes. type Paginator[T any] struct { - c *Client + c clientDoer pageIndex int count int total int // should be set to -1 on init. diff --git a/client_test.go b/client_test.go index d5dc7cf..0ca784c 100644 --- a/client_test.go +++ b/client_test.go @@ -6,6 +6,7 @@ import ( "errors" "io" "net/http" + "reflect" "strings" "testing" @@ -188,6 +189,18 @@ func TestClient_do(t *testing.T) { method: "GET", path: "/test", }, + { + name: "custom base url", + wantHeaders: http.Header{ + "Accept": {"application/json"}, + "Authorization": {"testing"}, + }, + baseUrl: "https://example.com/v9999", + wantUrl: "https://example.com/v9999/test", + returnsStatus: 204, + method: "GET", + path: "/test", + }, { name: "get ignore 404", wantHeaders: http.Header{ @@ -334,7 +347,7 @@ func TestClient_do(t *testing.T) { // blank responses should be a nil pointer ptr = nil } - err := c.do(context.TODO(), clientArgs{ + err := c.do(context.Background(), clientArgs{ method: tt.method, path: tt.path, resultKey: tt.resultKey, @@ -359,3 +372,366 @@ func TestClient_do(t *testing.T) { }) } } + +type mockClientDoer struct { + t *testing.T + + wantMethod string + wantPath string + wantQuery map[string]string + wantBody any + wantResultKey string + wantIgnore404 bool + tokenType string + + returnsResult any + returnsErr error +} + +func (c *mockClientDoer) getTokenType() string { return c.tokenType } + +func (c *mockClientDoer) do(ctx context.Context, a clientArgs) error { + c.t.Helper() + assert.NotNil(c.t, ctx) + assert.Equal(c.t, c.wantMethod, a.method) + assert.Equal(c.t, c.wantPath, a.path) + assert.Equal(c.t, c.wantQuery, a.query) + assert.Equal(c.t, c.wantBody, a.body) + assert.Equal(c.t, c.wantResultKey, a.resultKey) + assert.Equal(c.t, c.wantIgnore404, a.ignore404) + if c.returnsErr != nil { + return c.returnsErr + } + if c.returnsResult == nil { + // Ensure the result is nil. + assert.Nil(c.t, a.result) + } else { + // Ensure that the result is a pointer to the type of the return result. + if assert.Equal(c.t, reflect.PtrTo(reflect.TypeOf(c.returnsResult)), reflect.TypeOf(a.result)) { + // Set the value of the result pointer to the return result. + reflect.ValueOf(a.result).Elem().Set(reflect.ValueOf(c.returnsResult)) + } + } + return nil +} + +func dup[T any](x T, l int) []T { + r := make([]T, l) + for i := range r { + r[i] = x + } + return r +} + +func rawify(m map[string]any) map[string]json.RawMessage { + r := make(map[string]json.RawMessage, len(m)) + for k, v := range m { + r[k], _ = json.Marshal(v) + } + return r +} + +func TestPaginator_Next(t *testing.T) { + tests := []struct { + name string + + queries []map[string]string + clientResults []map[string]json.RawMessage + clientErrors []error + returnedResults [][]string + returnedErrors []error + + offsetStrat bool + initialQuery map[string]string + }{ + { + name: "first client error", + queries: dup(map[string]string{"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, 1), + clientResults: dup((map[string]json.RawMessage)(nil), 1), + clientErrors: []error{errors.New("fail")}, + returnedResults: dup([]string(nil), 1), + returnedErrors: []error{errors.New("fail")}, + offsetStrat: false, + initialQuery: nil, + }, + { + name: "second client error", + queries: []map[string]string{ + {"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, + {"orderBy": "asc", "page": "2", "sortBy": "test_sort"}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "total_count": 4, + "items": []string{"a", "b"}, + }), nil}, + clientErrors: []error{nil, errors.New("fail")}, + returnedResults: [][]string{{"a", "b"}, nil}, + returnedErrors: []error{nil, errors.New("fail")}, + offsetStrat: false, + initialQuery: nil, + }, + { + name: "query param passthrough", + queries: []map[string]string{ + {"orderBy": "asc", "page": "1", "sortBy": "test_sort", "x": "y"}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "total_count": 2, + "items": []string{"a", "b"}, + }), nil}, + clientErrors: []error{nil}, + returnedResults: [][]string{{"a", "b"}}, + returnedErrors: []error{nil}, + offsetStrat: false, + initialQuery: map[string]string{"x": "y"}, + }, + { + name: "page mode single success", + queries: []map[string]string{ + {"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "total_count": 2, + "items": []string{"a", "b"}, + }), nil}, + clientErrors: []error{nil}, + returnedResults: [][]string{{"a", "b"}}, + returnedErrors: []error{nil}, + offsetStrat: false, + }, + { + name: "page mode multiple success", + queries: []map[string]string{ + {"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, + {"orderBy": "asc", "page": "2", "sortBy": "test_sort"}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "total_count": 4, + "items": []string{"a", "b"}, + }), rawify(map[string]any{ + "total_count": 4, + "items": []string{"c", "d"}, + })}, + clientErrors: []error{nil, nil}, + returnedResults: [][]string{{"a", "b"}, {"c", "d"}}, + returnedErrors: []error{nil, nil}, + offsetStrat: false, + }, + { + name: "page mode stop iteration client hit", + queries: []map[string]string{ + {"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, + {}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "total_count": 2, + "items": []string{"a", "b"}, + }), nil}, + clientErrors: []error{nil, nil}, + returnedResults: [][]string{{"a", "b"}, {}}, + returnedErrors: []error{nil, types.StopIteration}, + offsetStrat: false, + }, + { + name: "page mode stop iteration api hit", + queries: []map[string]string{ + {"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, + {"orderBy": "asc", "page": "2", "sortBy": "test_sort"}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "total_count": 4, + "items": []string{"a", "b"}, + }), rawify(map[string]any{ + "total_count": 4, + "items": []string{}, + })}, + clientErrors: []error{nil, nil}, + returnedResults: [][]string{{"a", "b"}, {}}, + returnedErrors: []error{nil, types.StopIteration}, + offsetStrat: false, + }, + { + name: "offset mode single success", + queries: []map[string]string{ + {"limit": "2", "offset": "0", "orderBy": "asc", "sortBy": "test_sort"}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "items": []string{"a", "b"}, + }), nil}, + clientErrors: []error{nil}, + returnedResults: [][]string{{"a", "b"}}, + returnedErrors: []error{nil}, + offsetStrat: true, + }, + { + name: "offset mode multiple success", + queries: []map[string]string{ + {"limit": "2", "offset": "0", "orderBy": "asc", "sortBy": "test_sort"}, + {"limit": "2", "offset": "2", "orderBy": "asc", "sortBy": "test_sort"}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "items": []string{"a", "b"}, + }), rawify(map[string]any{ + "items": []string{"c", "d"}, + })}, + clientErrors: []error{nil, nil}, + returnedResults: [][]string{{"a", "b"}, {"c", "d"}}, + returnedErrors: []error{nil, nil}, + offsetStrat: true, + }, + { + name: "offset mode stop iteration", + queries: []map[string]string{ + {"limit": "2", "offset": "0", "orderBy": "asc", "sortBy": "test_sort"}, + {"limit": "2", "offset": "2", "orderBy": "asc", "sortBy": "test_sort"}, + }, + clientResults: []map[string]json.RawMessage{rawify(map[string]any{ + "items": []string{"a", "b"}, + }), rawify(map[string]any{ + "items": []string{}, + })}, + clientErrors: []error{nil, nil}, + returnedResults: [][]string{{"a", "b"}, {"c", "d"}}, + returnedErrors: []error{nil, types.StopIteration}, + offsetStrat: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + d := &mockClientDoer{ + t: t, + wantMethod: "GET", + wantPath: "/test", + wantQuery: tt.queries[0], + returnsResult: tt.clientResults[0], + returnsErr: tt.clientErrors[0], + } + p := &Paginator[string]{ + c: d, + total: -1, + path: "/test", + resultKey: "items", + sortBy: "test_sort", + query: tt.initialQuery, + limit: 2, + offsetStrat: tt.offsetStrat, + } + for i := 0; i < len(tt.queries); i++ { + d.wantQuery = tt.queries[i] + d.returnsResult = tt.clientResults[i] + d.returnsErr = tt.clientErrors[i] + res, err := p.Next(context.Background()) + if tt.returnedErrors[i] == nil { + // Make sure error is nil. + assert.NoError(t, err) + + // Make sure the result is what we expect. + assert.Equal(t, tt.returnedResults[i], res) + } else { + assert.Equal(t, tt.returnedErrors[i], err) + } + } + }) + } +} + +func TestPaginator_ForChunk(t *testing.T) { + t.Run("run until stop", func(t *testing.T) { + clientDoers := []*mockClientDoer{ + { + t: t, + wantMethod: "GET", + wantPath: "/test", + wantQuery: map[string]string{"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, + returnsResult: rawify(map[string]any{"total_count": 4, "items": []string{"a", "b"}}), + returnsErr: nil, + }, + { + t: t, + wantMethod: "GET", + wantPath: "/test", + wantQuery: map[string]string{"orderBy": "asc", "page": "2", "sortBy": "test_sort"}, + returnsResult: rawify(map[string]any{"total_count": 4, "items": []string{"c"}}), + returnsErr: nil, + }, + { + t: t, + wantMethod: "GET", + wantPath: "/test", + wantQuery: map[string]string{"orderBy": "asc", "page": "3", "sortBy": "test_sort"}, + returnsResult: rawify(map[string]any{"total_count": 4, "items": []string{"d"}}), + returnsErr: nil, + }, + { + t: t, + wantMethod: "GET", + wantPath: "/test", + wantQuery: map[string]string{"orderBy": "asc", "page": "4", "sortBy": "test_sort"}, + returnsResult: rawify(map[string]any{"total_count": 4, "items": []string{}}), + returnsErr: types.StopIteration, + }, + } + s := []string{} + p := &Paginator[string]{ + total: -1, + path: "/test", + resultKey: "items", + sortBy: "test_sort", + } + p.c = clientDoers[0] + i := 1 + err := p.ForChunk(context.Background(), func(res []string) error { + s = append(s, res...) + p.c = clientDoers[i] + i++ + return nil + }) + assert.NoError(t, err) + assert.Equal(t, []string{"a", "b", "c", "d"}, s) + }) + + t.Run("loop error", func(t *testing.T) { + p := &Paginator[string]{ + c: &mockClientDoer{ + t: t, + wantMethod: "GET", + wantPath: "/test", + wantQuery: map[string]string{"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, + returnsResult: rawify(map[string]any{"total_count": 4, "items": []string{"a", "b"}}), + returnsErr: nil, + }, + total: -1, + path: "/test", + resultKey: "items", + sortBy: "test_sort", + } + err := p.ForChunk(context.Background(), func(res []string) error { + return errors.New("test error") + }) + assert.EqualError(t, err, "test error") + }) + + t.Run("client error", func(t *testing.T) { + clientDoers := []*mockClientDoer{ + { + t: t, + wantMethod: "GET", + wantPath: "/test", + wantQuery: map[string]string{"orderBy": "asc", "page": "1", "sortBy": "test_sort"}, + returnsResult: rawify(map[string]any{"total_count": 4, "items": []string{"a", "b"}}), + returnsErr: errors.New("test error"), + }, + } + p := &Paginator[string]{ + c: clientDoers[0], + total: -1, + path: "/test", + resultKey: "items", + sortBy: "test_sort", + } + err := p.ForChunk(context.Background(), func(res []string) error { + return nil + }) + assert.EqualError(t, err, "test error") + }) +} diff --git a/generate_categories.go b/generate_categories.go index 310855e..d3dbd2b 100644 --- a/generate_categories.go +++ b/generate_categories.go @@ -17,7 +17,7 @@ const categoryTemplate = `// {{ .StructName }} is an auto-generated struct which // It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this // directly. Instead, call NewClient and then go to the field {{ .FieldPath }}. type {{ .StructName }} struct { - c *Client{{ .AdditionalFields }} + c clientDoer{{ .AdditionalFields }} }` func buildStruct(name, fieldPath, fields string) string { @@ -112,7 +112,7 @@ func main() { goCode = append(goCode, rootStruct) // Build the root initializer. - initFunc := "func new" + rootCat + "(c *Client) *ClientCategory" + rootCat + " {" + initFunc := "func new" + rootCat + "(c clientDoer) *ClientCategory" + rootCat + " {" if len(fields) == 0 { // Do a simple inline init here. initFunc += "\n\treturn &ClientCategory" + rootCat + "{c}\n}" diff --git a/ignite.go b/ignite.go index 37f9924..2c8ed59 100644 --- a/ignite.go +++ b/ignite.go @@ -35,11 +35,11 @@ func (c ClientCategoryIgniteGateways) Get(ctx context.Context, id string) (*type // Create is used to create a deployment. func (c ClientCategoryIgniteDeployments) Create(ctx context.Context, projectId string, deployment *types.DeploymentConfig) (*types.Deployment, error) { if projectId == "" { - if c.c.tokenType != "ptk" { + if c.c.getTokenType() != "ptk" { return nil, types.InvalidToken("project ID must be specified when using bearer authentication to make deployments") } } else { - if c.c.tokenType != "bearer" && c.c.tokenType != "pat" { + if c.c.getTokenType() != "bearer" && c.c.getTokenType() != "pat" { return nil, types.InvalidToken("project ID must not be specified if it is implied") } } diff --git a/pipe.go b/pipe.go index 357b6d5..0ae5811 100644 --- a/pipe.go +++ b/pipe.go @@ -9,7 +9,7 @@ import ( // GetAll is used to get all rooms associated with a pipe. func (c ClientCategoryPipeRooms) GetAll(ctx context.Context, projectId string) ([]*types.Room, error) { - if projectId == "" && c.c.tokenType != "ptk" { + if projectId == "" && c.c.getTokenType() != "ptk" { return nil, types.InvalidToken("project ID must be specified when using bearer authentication to get rooms") } var rooms []*types.Room diff --git a/projects.go b/projects.go index f539b65..dc21afa 100644 --- a/projects.go +++ b/projects.go @@ -9,7 +9,7 @@ import ( // Delete is used to delete a token. func (c ClientCategoryProjectsTokens) Delete(ctx context.Context, projectId, id string) error { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return types.InvalidToken("project tokens cannot be retrieved with a project token") } return c.c.do(ctx, clientArgs{ @@ -20,7 +20,7 @@ func (c ClientCategoryProjectsTokens) Delete(ctx context.Context, projectId, id // GetAll is used to get all tokens associated with a project. func (c ClientCategoryProjectsTokens) GetAll(ctx context.Context, projectId string) ([]*types.ProjectToken, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return nil, types.InvalidToken("project tokens cannot be retrieved with a project token") } var tokens []*types.ProjectToken @@ -38,7 +38,7 @@ func (c ClientCategoryProjectsTokens) GetAll(ctx context.Context, projectId stri // Create is used to create a token. func (c ClientCategoryProjectsTokens) Create(ctx context.Context, projectId string, permissions []types.ProjectPermission) (*types.ProjectToken, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return nil, types.InvalidToken("project tokens cannot be created with a project token") } if permissions == nil { @@ -60,7 +60,7 @@ func (c ClientCategoryProjectsTokens) Create(ctx context.Context, projectId stri // GetAllMembers is used to get all members associated with a project. func (c ClientCategoryProjects) GetAllMembers(ctx context.Context, projectId string) ([]*types.ProjectMember, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { projectId = "@this" } else { if projectId == "" { @@ -83,7 +83,7 @@ func (c ClientCategoryProjects) GetAllMembers(ctx context.Context, projectId str // GetCurrentMember is used to get the current member associated with a project. You cannot use this method with a project token. func (c ClientCategoryProjects) GetCurrentMember(ctx context.Context, projectId string) (*types.ProjectMember, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return nil, types.InvalidToken("current member cannot be retrieved with a project token") } if projectId == "" { @@ -105,7 +105,7 @@ func (c ClientCategoryProjects) GetCurrentMember(ctx context.Context, projectId // GetAll is used to get all project secrets. func (c ClientCategoryProjectsSecrets) GetAll(ctx context.Context, projectId string) ([]*types.ProjectSecret, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return nil, types.InvalidToken("project secrets cannot be retrieved with a project token") } var secrets []*types.ProjectSecret @@ -123,7 +123,7 @@ func (c ClientCategoryProjectsSecrets) GetAll(ctx context.Context, projectId str // Create is used to create a project secret. func (c ClientCategoryProjectsSecrets) Create(ctx context.Context, projectId string, key, value string) (*types.ProjectSecret, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return nil, types.InvalidToken("project secrets cannot be created with a project token") } var secret types.ProjectSecret @@ -142,7 +142,7 @@ func (c ClientCategoryProjectsSecrets) Create(ctx context.Context, projectId str // Delete is used to delete a project secret. func (c ClientCategoryProjectsSecrets) Delete(ctx context.Context, projectId, id string) error { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return types.InvalidToken("project secrets cannot be deleted with a project token") } return c.c.do(ctx, clientArgs{ diff --git a/users.go b/users.go index d9433a6..8586441 100644 --- a/users.go +++ b/users.go @@ -8,7 +8,7 @@ import ( // Get is used to get the current user. func (c ClientCategoryUsersMe) Get(ctx context.Context) (*types.UserMeInfo, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return nil, types.InvalidToken("cannot get user with project token") } var u types.UserMeInfo @@ -25,7 +25,7 @@ func (c ClientCategoryUsersMe) Get(ctx context.Context) (*types.UserMeInfo, erro // CreatePat is used to create a personal access token for the current user. func (c ClientCategoryUsersMe) CreatePat(ctx context.Context, name string) (*types.UserPat, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return nil, types.InvalidToken("cannot create users tokens with project token") } var pat types.UserPat @@ -44,7 +44,7 @@ func (c ClientCategoryUsersMe) CreatePat(ctx context.Context, name string) (*typ // GetAllPats is used to get all personal access tokens for the current user. func (c ClientCategoryUsersMe) GetAllPats(ctx context.Context) ([]*types.UserPat, error) { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return nil, types.InvalidToken("cannot get users tokens with project token") } var pats []*types.UserPat @@ -62,7 +62,7 @@ func (c ClientCategoryUsersMe) GetAllPats(ctx context.Context) ([]*types.UserPat // DeletePat is used to delete a personal access token for the current user. func (c ClientCategoryUsersMe) DeletePat(ctx context.Context, id string) error { - if c.c.tokenType == "ptk" { + if c.c.getTokenType() == "ptk" { return types.InvalidToken("cannot delete users tokens with project token") } return c.c.do(ctx, clientArgs{