This repository was archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
148 lines (122 loc) · 2.76 KB
/
document.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package tipe
import (
"context"
"net/http"
)
type Documents interface {
Create(context.Context, CreateDocumentOptions) error
Get(context.Context, interface{}, GetDocumentOptions) error
Update(context.Context, UpdateDocumentOptions) error
}
type docService service
type CreateDocumentOptions struct {
Fields map[string]interface{}
Name string
Refs map[string]interface{}
SkuID string
Template string
}
// Create a Tipe Document
func (ds *docService) Create(ctx context.Context, opts CreateDocumentOptions) error {
payload := map[string]interface{}{
"name": opts.Name,
"skuId": opts.SkuID,
"template": opts.Template,
}
if opts.Fields != nil {
payload["fields"] = opts.Fields
}
if opts.Refs != nil {
payload["refs"] = opts.Refs
}
// Create request
req, err := ds.client.newRequest(
http.MethodPost,
ds.client.host,
formatPath(ds.client.project, "createDocument"),
payload,
)
if err != nil {
return err
}
resp := &Response{}
// Get the document from Tipe
if err := ds.client.do(req.WithContext(ctx), resp); err != nil {
return err
}
return nil
}
type GetDocumentOptions struct {
// ID of the Document
ID string
// SkuID of the Document
SkuID string
}
// Get a Tipe document by id
func (ds *docService) Get(ctx context.Context, doc interface{}, opts GetDocumentOptions) error {
payload := map[string]interface{}{}
var command string
switch {
case opts.ID != "":
command = "documentById"
payload["id"] = opts.ID
case opts.SkuID != "":
command = "documentBySkuId"
payload["skuId"] = opts.SkuID
}
// Create request
req, err := ds.client.newRequest(
http.MethodPost,
ds.client.host,
formatPath(ds.client.project, command),
payload,
)
if err != nil {
return err
}
resp := &Response{doc}
// Get the document from Tipe
if err := ds.client.do(req.WithContext(ctx), resp); err != nil {
return err
}
return nil
}
type UpdateDocumentOptions struct {
ID string
Fields map[string]interface{}
Name string
Refs map[string]interface{}
SkuID string
Status string
Template string
}
// Create a Tipe Document
func (ds *docService) Update(ctx context.Context, opts UpdateDocumentOptions) error {
payload := map[string]interface{}{
"fields": opts.Fields,
"id": opts.ID,
"name": opts.Name,
"refs": opts.Refs,
"skuId": opts.SkuID,
"template": opts.Template,
}
if opts.Status != "" {
payload["status"] = opts.Status
}
// Create request
req, err := ds.client.newRequest(
http.MethodPut,
ds.client.host,
formatPath(ds.client.project, "updateDocument"),
payload,
)
if err != nil {
return err
}
resp := &Response{}
// Get the document from Tipe
if err := ds.client.do(req.WithContext(ctx), resp); err != nil {
return err
}
return nil
}