-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.go
More file actions
135 lines (118 loc) · 4.13 KB
/
Copy pathmodules.go
File metadata and controls
135 lines (118 loc) · 4.13 KB
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
125
126
127
128
129
130
131
132
133
134
135
package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
)
type Modules struct {
Client *Client
}
func (m Modules) basePath(orgName string) string {
return fmt.Sprintf("orgs/%s/modules", orgName)
}
func (m Modules) path(orgName, moduleName string) string {
return fmt.Sprintf("orgs/%s/modules/%s", orgName, moduleName)
}
func (m Modules) List(ctx context.Context, orgName string) ([]types.Module, error) {
res, err := m.Client.Do(ctx, http.MethodGet, m.basePath(orgName), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.Module](res)
}
type FindModulesInput struct {
Category *string `json:"category,omitempty"`
Subcategory *string `json:"subcategory,omitempty"`
Provider *string `json:"provider,omitempty"`
Platform *string `json:"platform,omitempty"`
Subplatform *string `json:"subplatform,omitempty"`
Name *string `json:"name,omitempty"`
Contributor []types.Contributor `json:"contributors,omitempty"`
}
// Find - GET /orgs/:orgName/modules/find
// Searches the entire module registry visible to the caller.
// When Contributor is empty, defaults to [nullstone-official, my-org].
func (m Modules) Find(ctx context.Context, orgName string, input FindModulesInput) ([]types.Module, error) {
contributors := input.Contributor
if len(contributors) == 0 {
contributors = []types.Contributor{types.ContributorNullstoneOfficial, types.ContributorMyOrg}
}
q := url.Values{}
if input.Category != nil {
q.Set("category", *input.Category)
}
if input.Subcategory != nil {
q.Set("subcategory", *input.Subcategory)
}
if input.Provider != nil {
q.Set("provider", *input.Provider)
}
if input.Platform != nil {
q.Set("platform", *input.Platform)
}
if input.Subplatform != nil {
q.Set("subplatform", *input.Subplatform)
}
if input.Name != nil {
q.Set("name", *input.Name)
}
parts := make([]string, 0, len(contributors))
for _, c := range contributors {
parts = append(parts, string(c))
}
q.Set("contributors", strings.Join(parts, ","))
relativePath := fmt.Sprintf("orgs/%s/modules/find", orgName)
res, err := m.Client.Do(ctx, http.MethodGet, relativePath, q, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.Module](res)
}
func (m Modules) Get(ctx context.Context, orgName, moduleName string) (*types.Module, error) {
res, err := m.Client.Do(ctx, http.MethodGet, m.path(orgName, moduleName), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Module](res)
}
type CreateModuleInput struct {
Name string `json:"name"`
FriendlyName string `json:"friendlyName"`
Description string `json:"description"`
IsPublic bool `json:"isPublic"`
Category string `json:"category"`
Subcategory string `json:"subcategory"`
ProviderTypes []string `json:"providerTypes"`
Platform string `json:"platform"`
Subplatform string `json:"subplatform"`
Type string `json:"type"`
AppCategories []string `json:"appCategories"`
SourceUrl string `json:"sourceUrl"`
Status string `json:"status"`
}
func (m Modules) Create(ctx context.Context, orgName string, input CreateModuleInput) (*types.Module, error) {
rawPayload, _ := json.Marshal(input)
res, err := m.Client.Do(ctx, http.MethodPost, m.basePath(orgName), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Module](res)
}
type UpdateModuleInput struct {
IsPublic *bool `json:"isPublic,omitempty"`
SourceUrl *string `json:"sourceUrl,omitempty"`
Status *string `json:"status,omitempty"`
}
func (m Modules) Update(ctx context.Context, orgName string, moduleName string, input UpdateModuleInput) (*types.Module, error) {
rawPayload, _ := json.Marshal(input)
res, err := m.Client.Do(ctx, http.MethodPut, m.path(orgName, moduleName), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Module](res)
}