Skip to content

Commit

Permalink
Add list roles.
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestNeller committed Dec 21, 2023
1 parent d9e1692 commit be4d31b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
38 changes: 22 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Client struct {
instance, token string
logger *slog.Logger
bodyLogger bool
Roles *clientRoles
}

type ClientOption func(client *Client)
Expand All @@ -38,6 +39,7 @@ func NewClient(instance string, token string, opts ...ClientOption) *Client {
token: token,
logger: slog.New(slog.Default().Handler()),
}
client.Roles = &clientRoles{c: client}
for _, opt := range opts {
opt(client)
}
Expand Down Expand Up @@ -165,22 +167,6 @@ func (items *ItemsClient[T]) List(ctx context.Context) ([]*T, error) {
return reply.Data, nil
}

type Role struct {
ID string `json:"id,omitempty"`
AdminAccess bool `json:"admin_access,omitempty"`
}

func (c *Client) ListRoles(ctx context.Context) ([]Role, error) {
reply := struct {
Data []Role `json:"data"`
}{}
if err := c.listdo(ctx, http.MethodGet, c.urlf("/roles"), nil, &reply); err != nil {
return nil, err
}

return reply.Data, nil
}

func (items *ItemsClient[T]) Get(ctx context.Context, id string) (*T, error) {
reply := struct {
Data *T `json:"data"`
Expand Down Expand Up @@ -248,3 +234,23 @@ func (s *SingletonClient[T]) Update(ctx context.Context, item *T) (*T, error) {
}
return reply.Data, nil
}

type clientRoles struct {
c *Client
}

type Role struct {
ID string `json:"id,omitempty"`
AdminAccess bool `json:"admin_access,omitempty"`
}

func (cr clientRoles) List(ctx context.Context) ([]Role, error) {
reply := struct {
Data []Role `json:"data"`
}{}
if err := cr.c.listdo(ctx, http.MethodGet, cr.c.urlf("/roles"), nil, &reply); err != nil {
return nil, err
}

return reply.Data, nil
}
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestItemsList(t *testing.T) {
}

func TestListRoles(t *testing.T) {
roles, err := initClient(t).ListRoles(context.Background())
roles, err := initClient(t).Roles.List(context.Background())
require.NoError(t, err)
require.NotEmpty(t, roles)

Expand Down

0 comments on commit be4d31b

Please sign in to comment.