/
documents_api.go
433 lines (383 loc) · 13 KB
/
documents_api.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package v2
import (
"net/http"
"github.com/centrifuge/go-centrifuge/documents"
"github.com/centrifuge/go-centrifuge/httpapi/coreapi"
"github.com/centrifuge/go-centrifuge/identity"
"github.com/centrifuge/go-centrifuge/jobs"
"github.com/centrifuge/go-centrifuge/utils/byteutils"
"github.com/centrifuge/go-centrifuge/utils/httputils"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
// CreateDocumentRequest defines the payload for creating documents.
type CreateDocumentRequest struct {
coreapi.CreateDocumentRequest
DocumentID byteutils.OptionalHex `json:"document_id" swaggertype:"primitive,string"` // if provided, creates the next version of the document.
}
// CloneDocumentRequest defines the payload for creating documents.
type CloneDocumentRequest struct {
Scheme string `json:"scheme" enums:"generic,entity"`
}
// UpdateDocumentRequest defines the payload to patch an existing document.
type UpdateDocumentRequest struct {
coreapi.CreateDocumentRequest
}
// CreateDocument creates a document.
// @summary Creates a new document.
// @description Creates a new document.
// @id create_document_v2
// @tags Documents
// @accept json
// @param authorization header string true "Hex encoded centrifuge ID of the account for the intended API action"
// @param body body v2.CreateDocumentRequest true "Document Create request"
// @produce json
// @Failure 400 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @Failure 403 {object} httputils.HTTPError
// @success 201 {object} coreapi.DocumentResponse
// @router /v2/documents [post]
func (h handler) CreateDocument(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
ctx := r.Context()
var req CreateDocumentRequest
err = unmarshalBody(r, &req)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
payload, err := toDocumentsPayload(req.CreateDocumentRequest, req.DocumentID.Bytes())
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
doc, err := h.srv.CreateDocument(ctx, payload)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
resp, err := toDocumentResponse(doc, h.srv.tokenRegistry, jobs.NilJobID())
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusCreated)
render.JSON(w, r, resp)
}
// CloneDocument creates a new cloned document from an existing Template document.
// @summary Creates a new cloned document from an existing Template document.
// @description Creates a new cloned document from an existing Template document.
// @id clone_document_v2
// @tags Documents
// @accept json
// @param authorization header string true "Hex encoded centrifuge ID of the account for the intended API action"
// @param body body v2.CloneDocumentRequest true "Document Clone request"
// @param document_id path string true "Document Identifier"
// @produce json
// @Failure 400 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @Failure 403 {object} httputils.HTTPError
// @success 201 {object} coreapi.DocumentResponse
// @router /v2/documents/{document_id}/clone [post]
func (h handler) CloneDocument(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
templateID, err := hexutil.Decode(chi.URLParam(r, coreapi.DocumentIDParam))
if err != nil {
code = http.StatusBadRequest
log.Error(err)
err = coreapi.ErrInvalidDocumentID
return
}
ctx := r.Context()
var req CloneDocumentRequest
err = unmarshalBody(r, &req)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
payload := documents.ClonePayload{
Scheme: req.Scheme,
TemplateID: templateID,
}
doc, err := h.srv.CloneDocument(ctx, payload)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
resp, err := toDocumentResponse(doc, h.srv.tokenRegistry, jobs.NilJobID())
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusCreated)
render.JSON(w, r, resp)
}
// Update updates a pending document.
// @summary Updates a pending document.
// @description Updates a pending document.
// @id update_document_v2
// @tags Documents
// @accept json
// @param authorization header string true "Hex encoded centrifuge ID of the account for the intended API action"
// @param body body v2.UpdateDocumentRequest true "Document Update request"
// @param document_id path string true "Document Identifier"
// @produce json
// @Failure 400 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @Failure 403 {object} httputils.HTTPError
// @success 200 {object} coreapi.DocumentResponse
// @router /v2/documents/{document_id} [patch]
func (h handler) UpdateDocument(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
docID, err := hexutil.Decode(chi.URLParam(r, coreapi.DocumentIDParam))
if err != nil {
code = http.StatusBadRequest
log.Error(err)
err = coreapi.ErrInvalidDocumentID
return
}
ctx := r.Context()
var req UpdateDocumentRequest
err = unmarshalBody(r, &req)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
payload, err := toDocumentsPayload(req.CreateDocumentRequest, docID)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
doc, err := h.srv.UpdateDocument(ctx, payload)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
resp, err := toDocumentResponse(doc, h.srv.tokenRegistry, jobs.NilJobID())
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusOK)
render.JSON(w, r, resp)
}
// Commit creates a document.
// @summary Commits a pending document.
// @description Commits a pending document.
// @id commit_document_v2
// @tags Documents
// @accept json
// @param authorization header string true "Hex encoded centrifuge ID of the account for the intended API action"
// @param document_id path string true "Document Identifier"
// @produce json
// @Failure 400 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @Failure 403 {object} httputils.HTTPError
// @success 202 {object} coreapi.DocumentResponse
// @router /v2/documents/{document_id}/commit [post]
func (h handler) Commit(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
docID, err := hexutil.Decode(chi.URLParam(r, coreapi.DocumentIDParam))
if err != nil {
code = http.StatusBadRequest
log.Error(err)
err = coreapi.ErrInvalidDocumentID
return
}
ctx := r.Context()
doc, jobID, err := h.srv.Commit(ctx, docID)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
resp, err := toDocumentResponse(doc, h.srv.tokenRegistry, jobID)
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusAccepted)
render.JSON(w, r, resp)
}
func (h handler) getDocumentWithStatus(w http.ResponseWriter, r *http.Request, st documents.Status) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
docID, err := hexutil.Decode(chi.URLParam(r, coreapi.DocumentIDParam))
if err != nil {
code = http.StatusBadRequest
log.Error(err)
err = coreapi.ErrInvalidDocumentID
return
}
ctx := r.Context()
doc, err := h.srv.GetDocument(ctx, docID, st)
if err != nil {
code = http.StatusNotFound
log.Error(err)
err = coreapi.ErrDocumentNotFound
return
}
resp, err := toDocumentResponse(doc, h.srv.tokenRegistry, jobs.NilJobID())
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusOK)
render.JSON(w, r, resp)
}
// GetPendingDocument returns the pending document associated with docID.
// @summary Returns the pending document associated with docID.
// @description Returns the pending document associated with docID.
// @id get_pending_document
// @tags Documents
// @param authorization header string true "Hex encoded centrifuge ID of the account for the intended API action"
// @param document_id path string true "Document Identifier"
// @produce json
// @Failure 403 {object} httputils.HTTPError
// @Failure 400 {object} httputils.HTTPError
// @Failure 404 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @success 200 {object} coreapi.DocumentResponse
// @router /v2/documents/{document_id}/pending [get]
func (h handler) GetPendingDocument(w http.ResponseWriter, r *http.Request) {
h.getDocumentWithStatus(w, r, documents.Pending)
}
// GetCommittedDocument returns the latest committed document associated with docID.
// @summary Returns the latest committed document associated with docID.
// @description Returns the latest committed document associated with docID.
// @id get_committed_document
// @tags Documents
// @param authorization header string true "Hex encoded centrifuge ID of the account for the intended API action"
// @param document_id path string true "Document Identifier"
// @produce json
// @Failure 403 {object} httputils.HTTPError
// @Failure 400 {object} httputils.HTTPError
// @Failure 404 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @success 200 {object} coreapi.DocumentResponse
// @router /v2/documents/{document_id}/committed [get]
func (h handler) GetCommittedDocument(w http.ResponseWriter, r *http.Request) {
h.getDocumentWithStatus(w, r, documents.Committed)
}
// GetDocumentVersion returns the specific version of the document.
// @summary Returns the specific version of the document.
// @description Returns the specific version of the document.
// @id get_document_version_v2
// @tags Documents
// @param authorization header string true "Hex encoded centrifuge ID of the account for the intended API action"
// @param document_id path string true "Document Identifier"
// @param version_id path string true "Document Version Identifier"
// @produce json
// @Failure 403 {object} httputils.HTTPError
// @Failure 400 {object} httputils.HTTPError
// @Failure 404 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @success 200 {object} coreapi.DocumentResponse
// @router /v2/documents/{document_id}/versions/{version_id} [get]
func (h handler) GetDocumentVersion(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
ids := make([][]byte, 2, 2)
for i, idStr := range []string{chi.URLParam(r, coreapi.DocumentIDParam), chi.URLParam(r, coreapi.VersionIDParam)} {
var id []byte
id, err = hexutil.Decode(idStr)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
err = coreapi.ErrInvalidDocumentID
return
}
ids[i] = id
}
doc, err := h.srv.GetDocumentVersion(r.Context(), ids[0], ids[1])
if err != nil {
code = http.StatusNotFound
log.Error(err)
err = coreapi.ErrDocumentNotFound
return
}
resp, err := toDocumentResponse(doc, h.srv.tokenRegistry, jobs.NilJobID())
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusOK)
render.JSON(w, r, resp)
}
// RemoveCollaboratorsRequest contains the list of collaborators that are to be removed from the document
type RemoveCollaboratorsRequest struct {
Collaborators []identity.DID `json:"collaborators" swaggertype:"array,string"`
}
// RemoveCollaborators removes the collaborators from the document.
// @summary Removes the collaborators from the document.
// @description Removes the collaborators from the document.
// @id remove_collaborators
// @tags Documents
// @accept json
// @param authorization header string true "Hex encoded centrifuge ID of the account for the intended API action"
// @param body body v2.RemoveCollaboratorsRequest true "Remove Collaborators request"
// @param document_id path string true "Document Identifier"
// @produce json
// @Failure 400 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @Failure 403 {object} httputils.HTTPError
// @success 200 {object} coreapi.DocumentResponse
// @router /v2/documents/{document_id}/collaborators [delete]
func (h handler) RemoveCollaborators(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
docID, err := hexutil.Decode(chi.URLParam(r, coreapi.DocumentIDParam))
if err != nil {
code = http.StatusBadRequest
log.Error(err)
err = coreapi.ErrInvalidDocumentID
return
}
var req RemoveCollaboratorsRequest
err = unmarshalBody(r, &req)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
doc, err := h.srv.RemoveCollaborators(r.Context(), docID, req.Collaborators)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
resp, err := toDocumentResponse(doc, h.srv.tokenRegistry, jobs.NilJobID())
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusOK)
render.JSON(w, r, resp)
}