|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + |
| 9 | + v1pb "github.com/bytebase/bytebase/proto/generated-go/v1" |
| 10 | + "google.golang.org/protobuf/encoding/protojson" |
| 11 | +) |
| 12 | + |
| 13 | +// ListGroup list all groups. |
| 14 | +func (c *client) ListGroup(ctx context.Context) (*v1pb.ListGroupsResponse, error) { |
| 15 | + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/groups", c.url, c.version), nil) |
| 16 | + if err != nil { |
| 17 | + return nil, err |
| 18 | + } |
| 19 | + |
| 20 | + body, err := c.doRequest(req) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + |
| 25 | + var res v1pb.ListGroupsResponse |
| 26 | + if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + |
| 30 | + return &res, nil |
| 31 | +} |
| 32 | + |
| 33 | +// CreateGroup creates the group. |
| 34 | +func (c *client) CreateGroup(ctx context.Context, email string, group *v1pb.Group) (*v1pb.Group, error) { |
| 35 | + payload, err := protojson.Marshal(group) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/%s/groups?groupEmail=%s", c.url, c.version, email), strings.NewReader(string(payload))) |
| 41 | + |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + body, err := c.doRequest(req) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + var res v1pb.Group |
| 52 | + if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + return &res, nil |
| 57 | +} |
| 58 | + |
| 59 | +// GetGroup gets the group by name. |
| 60 | +func (c *client) GetGroup(ctx context.Context, name string) (*v1pb.Group, error) { |
| 61 | + body, err := c.getResource(ctx, name) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + var res v1pb.Group |
| 67 | + if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + return &res, nil |
| 72 | +} |
| 73 | + |
| 74 | +// UpdateGroup updates the group. |
| 75 | +func (c *client) UpdateGroup(ctx context.Context, patch *v1pb.Group, updateMasks []string) (*v1pb.Group, error) { |
| 76 | + body, err := c.updateResource(ctx, patch.Name, patch, updateMasks, false /* allow missing = false*/) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + var res v1pb.Group |
| 82 | + if err := ProtojsonUnmarshaler.Unmarshal(body, &res); err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + return &res, nil |
| 87 | +} |
| 88 | + |
| 89 | +// DeleteGroup deletes the group by name. |
| 90 | +func (c *client) DeleteGroup(ctx context.Context, name string) error { |
| 91 | + return c.deleteResource(ctx, name) |
| 92 | +} |
0 commit comments