Skip to content

Commit f9b6c7d

Browse files
committed
refactor(user): interface-ize UserService and implement with userService
- Define UserService interface for better abstraction and testability- Create userService struct to implement UserService interface - Update client.go to use new UserService interface and NewUserService function - Improve code organization and readability in api_user.go Signed-off-by: Flc <[email protected]>
1 parent 3e1ea66 commit f9b6c7d

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

api_user.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,38 @@ import (
55
"net/http"
66
)
77

8-
type UserRole struct {
9-
ID string `json:"id"`
10-
Name string `json:"name"`
8+
type (
9+
UserRole struct {
10+
ID string `json:"id"`
11+
Name string `json:"name"`
12+
}
13+
14+
// GetRolesRequest represents a request to get roles
15+
GetRolesRequest struct {
16+
WorkspaceID *int `url:"workspace_id,omitempty"` // 项目 ID
17+
}
18+
)
19+
20+
type UserService interface {
21+
// GetRoles 获取角色ID对照关系
22+
//
23+
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/user/get_roles.html
24+
GetRoles(ctx context.Context, request *GetRolesRequest, opts ...RequestOption) ([]*UserRole, *Response, error)
1125
}
1226

13-
// UserService is a service to interact with user related API
14-
type UserService struct {
27+
type userService struct {
1528
client *Client
1629
}
1730

18-
// GetRolesRequest represents a request to get roles
19-
type GetRolesRequest struct {
20-
WorkspaceID *int `url:"workspace_id,omitempty"` // 项目 ID
31+
var _ UserService = (*userService)(nil)
32+
33+
func NewUserService(client *Client) UserService {
34+
return &userService{
35+
client: client,
36+
}
2137
}
2238

23-
// GetRoles 获取角色ID对照关系
24-
//
25-
// https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/user/get_roles.html
26-
func (s *UserService) GetRoles(
39+
func (s *userService) GetRoles(
2740
ctx context.Context, request *GetRolesRequest, opts ...RequestOption,
2841
) ([]*UserRole, *Response, error) {
2942
req, err := s.client.NewRequest(ctx, http.MethodGet, "roles", request, opts)

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Client struct {
4545
WorkspaceService *WorkspaceService
4646
LabelService *LabelService
4747
MeasureService *MeasureService
48-
UserService *UserService
48+
UserService UserService
4949
WorkflowService *WorkflowService
5050
SettingService *SettingService
5151
}
@@ -91,7 +91,7 @@ func newClient(opts ...ClientOption) (*Client, error) {
9191
c.WorkspaceService = &WorkspaceService{client: c}
9292
c.LabelService = &LabelService{client: c}
9393
c.MeasureService = &MeasureService{client: c}
94-
c.UserService = &UserService{client: c}
94+
c.UserService = NewUserService(c)
9595
c.WorkflowService = &WorkflowService{client: c}
9696
c.SettingService = &SettingService{client: c}
9797

0 commit comments

Comments
 (0)