-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace.go
117 lines (93 loc) · 2.73 KB
/
workspace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package autonomisdk
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/google/uuid"
"github.com/intercloud/autonomi-sdk/models"
)
func (c *Client) CreateWorkspace(ctx context.Context, payload models.CreateWorkspace) (*models.Workspace, error) {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(&payload)
if err != nil {
return nil, err
}
if errV := c.validate.StructCtx(ctx, payload); errV != nil {
return nil, errV
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/accounts/%s/workspaces", c.hostURL, c.accountID), body)
if err != nil {
return nil, err
}
resp, err := c.doRequest(req)
if err != nil {
return nil, err
}
workspace := models.WorkspaceResponse{}
if err := json.Unmarshal(resp, &workspace); err != nil {
return nil, err
}
return &workspace.Data, nil
}
func (c *Client) ListWorkspaces(ctx context.Context, accountID uuid.UUID) ([]models.Workspace, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/accounts/%s/workspaces", c.hostURL, accountID), nil)
if err != nil {
return nil, err
}
resp, err := c.doRequest(req)
if err != nil {
return nil, err
}
workspaces := models.WorkspacesResponse{}
if err = json.Unmarshal(resp, &workspaces); err != nil {
return nil, err
}
return workspaces.Data, nil
}
func (c *Client) GetWorkspace(ctx context.Context, workspaceID string) (*models.Workspace, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/accounts/%s/workspaces/%s", c.hostURL, c.accountID, workspaceID), nil)
if err != nil {
return nil, err
}
resp, err := c.doRequest(req)
if err != nil {
return nil, err
}
workspace := models.WorkspaceResponse{}
if err = json.Unmarshal(resp, &workspace); err != nil {
return nil, err
}
return &workspace.Data, nil
}
func (c *Client) UpdateWorkspace(ctx context.Context, payload models.UpdateWorkspace, workspaceID string) (*models.Workspace, error) {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(&payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/accounts/%s/workspaces/%s", c.hostURL, c.accountID, workspaceID), body)
if err != nil {
return nil, err
}
resp, err := c.doRequest(req)
if err != nil {
return nil, err
}
workspace := models.WorkspaceResponse{}
if err := json.Unmarshal(resp, &workspace); err != nil {
return nil, err
}
return &workspace.Data, nil
}
func (c *Client) DeleteWorkspace(ctx context.Context, workspaceID string) error {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/accounts/%s/workspaces/%s", c.hostURL, c.accountID, workspaceID), nil)
if err != nil {
return err
}
if _, err = c.doRequest(req); err != nil {
return err
}
return nil
}