-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkspaces.go
124 lines (114 loc) · 3.16 KB
/
workspaces.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
118
119
120
121
122
123
124
package gotypeform
import (
"bytes"
"encoding/json"
"github.com/google/go-querystring/query"
)
type Workspaces struct {
Workspaces []Workspace `json:"items"`
PageCount int `json:"page_count"`
TotalItems int `json:"total_items"`
}
type Workspace struct {
AccountID string `json:"account_id"`
Default bool `json:"default"`
Forms *WorkspaceForms `json:"forms,omitempty"`
ID string `json:"id"`
Members *[]Members `json:"members,omitempty"`
Name string `json:"name"`
Self Self `json:"self"`
Shared bool `json:"shared"`
}
type WorkspaceForms struct {
Count int `json:"count"`
Href string `json:"href"`
}
type Members struct {
Email string `json:"email"`
Name string `json:"name"`
Role string `json:"role"`
AccountMemberID string `json:"account_member_id"`
}
type Self struct {
Href string `json:"href"`
}
type WorkspaceName struct {
Name string `json:"name"`
}
type WorkspaceQueryParams struct {
Search string `url:"search,omitempty"`
Page int `url:"page,omitempty"`
PageSize int `url:"page_size,omitempty"`
}
func (t *Typeform) GetAllWorkspaces(params WorkspaceQueryParams) (*Workspaces, error) {
queryParams, _ := query.Values(params)
contents, err := t.buildAndExecRequest("GET", workspacesUrl+"?"+queryParams.Encode(), nil)
if err != nil {
return nil, err
}
var workspaces *Workspaces
err = json.Unmarshal(contents, &workspaces)
if err != nil {
return nil, err
}
return workspaces, err
}
func (t *Typeform) GetAllWorkspacesInAccount(id string, params WorkspaceQueryParams) (*Workspaces, error) {
queryParams, _ := query.Values(params)
contents, err := t.buildAndExecRequest("GET", accountsUrl+"/"+id+workspacesUrl+"?"+queryParams.Encode(), nil)
if err != nil {
return nil, err
}
var workspaces *Workspaces
err = json.Unmarshal(contents, &workspaces)
if err != nil {
return nil, err
}
return workspaces, err
}
func (t *Typeform) GetWorkspace(id string) (*Workspace, error) {
contents, err := t.buildAndExecRequest("GET", workspacesUrl+"/"+id, nil)
if err != nil {
return nil, err
}
var workspace *Workspace
err = json.Unmarshal(contents, &workspace)
if err != nil {
return nil, err
}
return workspace, err
}
func (t *Typeform) UpdateWorkspace(id string, updates []UpdateRequestBody) error {
json_data, err := json.Marshal(updates)
if err != nil {
return err
}
_, err = t.buildAndExecRequest("PATCH", workspacesUrl+"/"+id, bytes.NewBuffer(json_data))
if err != nil {
return err
}
return err
}
func (t *Typeform) CreateWorkspace(name string) (*Workspace, error) {
requestBody := &WorkspaceName{
Name: name,
}
json_data, err := json.Marshal(requestBody)
if err != nil {
return nil, err
}
contents, err := t.buildAndExecRequest("POST", workspacesUrl, bytes.NewBuffer(json_data))
if err != nil {
return nil, err
}
var workspace *Workspace
err = json.Unmarshal(contents, &workspace)
if err != nil {
return nil, err
}
return workspace, nil
}
func (t *Typeform) DeleteWorkspace(id string) error {
_, err := t.buildAndExecRequest("DELETE", workspacesUrl+"/"+id, nil)
return err
}