This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
119 lines (110 loc) · 3.87 KB
/
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
/*
* Copyright (C) 2020. Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package api
import (
"encoding/json"
"github.com/labstack/echo/v4"
logging "github.com/nuts-foundation/nuts-network/logging"
"github.com/nuts-foundation/nuts-network/pkg"
"github.com/nuts-foundation/nuts-network/pkg/model"
"io"
"io/ioutil"
"net/http"
)
// ApiWrapper is needed to connect the implementation to the echo ServiceWrapper
type ApiWrapper struct {
Service pkg.NetworkClient
}
func (a ApiWrapper) ListDocuments(ctx echo.Context) error {
documents, err := a.Service.ListDocuments()
if err != nil {
logging.Log().Errorf("Error while listing documents: %v", err)
return ctx.String(http.StatusInternalServerError, err.Error())
}
results := make([]Document, len(documents))
for i, document := range documents {
results[i] = Document{}
results[i].fromModel(document.Document)
}
return ctx.JSON(http.StatusOK, results)
}
func (a ApiWrapper) GetDocument(ctx echo.Context, hashAsString string) error {
hash, err := model.ParseHash(hashAsString)
if err != nil {
return ctx.String(http.StatusBadRequest, err.Error())
}
document, err := a.Service.GetDocument(hash)
if err != nil {
logging.Log().Errorf("Error while retrieving document (hash=%s): %v", hash, err)
return ctx.String(http.StatusInternalServerError, err.Error())
}
if document == nil {
return ctx.String(http.StatusNotFound, "document not found")
}
return writeDocument(ctx, http.StatusOK, document.Document)
}
func (a ApiWrapper) GetDocumentContents(ctx echo.Context, hashAsString string) error {
hash, err := model.ParseHash(hashAsString)
if err != nil {
return ctx.String(http.StatusBadRequest, err.Error())
}
reader, err := a.Service.GetDocumentContents(hash)
if err != nil {
return ctx.String(http.StatusInternalServerError, err.Error())
}
if reader == nil {
return ctx.String(http.StatusNotFound, "document or contents not found")
}
defer reader.Close()
ctx.Response().Header().Set(echo.HeaderContentType, "application/octet-stream")
ctx.Response().WriteHeader(http.StatusOK)
_, err = io.Copy(ctx.Response().Writer, reader)
return err
}
func (a ApiWrapper) AddDocument(ctx echo.Context) error {
inputDocument, err := parseDocument(ctx)
if err != nil {
logging.Log().Warnf("Unable to parse document: %v", err)
return ctx.String(http.StatusBadRequest, err.Error())
}
outputDocument, err := a.Service.AddDocumentWithContents(model.UnmarshalDocumentTime(inputDocument.Timestamp), inputDocument.Type, inputDocument.Contents)
if err != nil {
logging.Log().Warnf("Unable to add document: %v", err)
return ctx.String(http.StatusInternalServerError, err.Error())
}
return writeDocument(ctx, http.StatusCreated, *outputDocument)
}
func writeDocument(ctx echo.Context, statusCode int, document model.Document) error {
return ctx.JSON(statusCode, Document{
Hash: document.Hash.String(),
Timestamp: model.MarshalDocumentTime(document.Timestamp),
Type: document.Type,
})
}
func parseDocument(ctx echo.Context) (DocumentWithContents, error) {
bytes, err := ioutil.ReadAll(ctx.Request().Body)
if err != nil {
return DocumentWithContents{}, err
}
document := DocumentWithContents{}
err = json.Unmarshal(bytes, &document)
if err != nil {
return DocumentWithContents{}, err
}
return document, err
}