-
Notifications
You must be signed in to change notification settings - Fork 39
/
documents_api.go
312 lines (276 loc) · 9.37 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
package v2
import (
"net/http"
"github.com/centrifuge/go-centrifuge/documents"
"github.com/centrifuge/go-centrifuge/httpapi/coreapi"
"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"
)
// DocumentRequest is an alias to coreapi Document request.
// Aliased here to fix the swagger generation issues.
type DocumentRequest = coreapi.CreateDocumentRequest
// CreateDocumentRequest defines the payload for creating documents.
type CreateDocumentRequest struct {
DocumentRequest
DocumentID byteutils.OptionalHex `json:"document_id" swaggertype:"primitive,string"` // if provided, creates the next version of the document.
}
// UpdateDocumentRequest defines the payload to patch an existing document.
type UpdateDocumentRequest struct {
DocumentRequest
}
// 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.DocumentRequest, 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)
}
// 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.DocumentRequest, 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)
}