forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
258 lines (226 loc) · 8.96 KB
/
operation.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package introduce
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/gorilla/mux"
client "github.com/Universal-Health-Chain/aries-framework-go/pkg/client/introduce"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/controller/command"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/controller/command/introduce"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/controller/internal/cmdutil"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/controller/rest"
)
// constants for operation introduce.
const (
OperationID = "/introduce"
Actions = OperationID + "/actions"
SendProposal = OperationID + "/send-proposal"
SendProposalWithOOBRequest = OperationID + "/send-proposal-with-oob-request"
SendRequest = OperationID + "/send-request"
AcceptProposalWithOOBRequest = OperationID + "/{piid}/accept-proposal-with-oob-request"
AcceptProposal = OperationID + "/{piid}/accept-proposal"
AcceptRequestWithPublicOOBRequest = OperationID + "/{piid}/accept-request-with-public-oob-request"
AcceptRequestWithRecipients = OperationID + "/{piid}/accept-request-with-recipients"
DeclineProposal = OperationID + "/{piid}/decline-proposal"
DeclineRequest = OperationID + "/{piid}/decline-request"
AcceptProblemReport = OperationID + "/{piid}/accept-problem-report"
)
// Operation is controller REST service controller for the introduce.
type Operation struct {
command *introduce.Command
handlers []rest.Handler
}
// New returns new introduce rest client protocol instance.
func New(ctx client.Provider, notifier command.Notifier) (*Operation, error) {
cmd, err := introduce.New(ctx, notifier)
if err != nil {
return nil, fmt.Errorf("introduce command : %w", err)
}
o := &Operation{command: cmd}
o.registerHandler()
return o, nil
}
// GetRESTHandlers get all controller API handler available for this protocol service.
func (c *Operation) GetRESTHandlers() []rest.Handler {
return c.handlers
}
// registerHandler register handlers to be exposed from this protocol service as REST API endpoints.
func (c *Operation) registerHandler() {
// Add more protocol endpoints here to expose them as controller API endpoints
c.handlers = []rest.Handler{
cmdutil.NewHTTPHandler(Actions, http.MethodGet, c.Actions),
cmdutil.NewHTTPHandler(SendProposal, http.MethodPost, c.SendProposal),
cmdutil.NewHTTPHandler(SendProposalWithOOBRequest, http.MethodPost, c.SendProposalWithOOBRequest),
cmdutil.NewHTTPHandler(SendRequest, http.MethodPost, c.SendRequest),
cmdutil.NewHTTPHandler(AcceptProposalWithOOBRequest, http.MethodPost, c.AcceptProposalWithOOBRequest),
cmdutil.NewHTTPHandler(AcceptProposal, http.MethodPost, c.AcceptProposal),
cmdutil.NewHTTPHandler(AcceptRequestWithPublicOOBRequest, http.MethodPost, c.AcceptRequestWithPublicOOBRequest),
cmdutil.NewHTTPHandler(AcceptRequestWithRecipients, http.MethodPost, c.AcceptRequestWithRecipients),
cmdutil.NewHTTPHandler(DeclineProposal, http.MethodPost, c.DeclineProposal),
cmdutil.NewHTTPHandler(DeclineRequest, http.MethodPost, c.DeclineRequest),
cmdutil.NewHTTPHandler(AcceptProblemReport, http.MethodPost, c.AcceptProblemReport),
}
}
// Actions swagger:route GET /introduce/actions introduce introduceActions
//
// Returns pending actions that have not yet to be executed or cancelled.
//
// Responses:
// default: genericError
// 200: introduceActionsResponse
func (c *Operation) Actions(rw http.ResponseWriter, _ *http.Request) {
rest.Execute(c.command.Actions, rw, nil)
}
// SendProposal swagger:route POST /introduce/send-proposal introduce introduceSendProposal
//
// Sends a proposal.
//
// Responses:
// default: genericError
// 200: introduceSendProposalResponse
func (c *Operation) SendProposal(rw http.ResponseWriter, req *http.Request) {
rest.Execute(c.command.SendProposal, rw, req.Body)
}
// SendProposalWithOOBRequest swagger:route POST /introduce/send-proposal-with-oob-request introduce introduceSendProposalWithOOBRequest
//
// Sends a proposal with OOBRequest.
//
// Responses:
// default: genericError
// 200: introduceSendProposalWithOOBRequestResponse
func (c *Operation) SendProposalWithOOBRequest(rw http.ResponseWriter, req *http.Request) {
rest.Execute(c.command.SendProposalWithOOBRequest, rw, req.Body)
}
// SendRequest swagger:route POST /introduce/send-request introduce introduceSendRequest
//
// Sends a request.
//
// Responses:
// default: genericError
// 200: introduceSendRequestResponse
func (c *Operation) SendRequest(rw http.ResponseWriter, req *http.Request) {
rest.Execute(c.command.SendRequest, rw, req.Body)
}
// AcceptProposalWithOOBRequest swagger:route POST /introduce/{piid}/accept-proposal-with-oob-request introduce introduceAcceptProposalWithOOBRequest
//
// Accepts a proposal with OOBRequest.
//
// Responses:
// default: genericError
// 200: introduceAcceptProposalWithOOBRequestResponse
func (c *Operation) AcceptProposalWithOOBRequest(rw http.ResponseWriter, req *http.Request) {
if ok, r := toCommandRequest(rw, req); ok {
rest.Execute(c.command.AcceptProposalWithOOBRequest, rw, r)
}
}
// AcceptProposal swagger:route POST /introduce/{piid}/accept-proposal introduce introduceAcceptProposal
//
// Accepts a proposal.
//
// Responses:
// default: genericError
// 200: introduceAcceptProposalResponse
func (c *Operation) AcceptProposal(rw http.ResponseWriter, req *http.Request) {
payload := fmt.Sprintf(`{"piid":%q}`, mux.Vars(req)["piid"])
rest.Execute(c.command.AcceptProposal, rw, bytes.NewBufferString(payload))
}
// AcceptRequestWithPublicOOBRequest swagger:route POST /introduce/{piid}/accept-request-with-public-oob-request introduce introduceAcceptRequestWithPublicOOBRequest
//
// Accept a request with public OOBRequest.
//
// Responses:
// default: genericError
// 200: introduceAcceptRequestWithPublicOOBRequestResponse
func (c *Operation) AcceptRequestWithPublicOOBRequest(rw http.ResponseWriter, req *http.Request) {
if ok, r := toCommandRequest(rw, req); ok {
rest.Execute(c.command.AcceptRequestWithPublicOOBRequest, rw, r)
}
}
// AcceptRequestWithRecipients swagger:route POST /introduce/{piid}/accept-request-with-recipients introduce introduceAcceptRequestWithRecipients
//
// Accept a request with recipients.
//
// Responses:
// default: genericError
// 200: introduceAcceptRequestWithRecipientsResponse
func (c *Operation) AcceptRequestWithRecipients(rw http.ResponseWriter, req *http.Request) {
if ok, r := toCommandRequest(rw, req); ok {
rest.Execute(c.command.AcceptRequestWithRecipients, rw, r)
}
}
// DeclineProposal swagger:route POST /introduce/{piid}/decline-proposal introduce introduceDeclineProposal
//
// Declines a proposal.
//
// Responses:
// default: genericError
// 200: introduceDeclineProposalResponse
func (c *Operation) DeclineProposal(rw http.ResponseWriter, req *http.Request) {
rest.Execute(c.command.DeclineProposal, rw, bytes.NewBufferString(fmt.Sprintf(`{
"piid":%q,
"reason":%q
}`, mux.Vars(req)["piid"], req.URL.Query().Get("reason"))))
}
// DeclineRequest swagger:route POST /introduce/{piid}/decline-request introduce introduceDeclineRequest
//
// Declines a request.
//
// Responses:
// default: genericError
// 200: introduceDeclineRequestResponse
func (c *Operation) DeclineRequest(rw http.ResponseWriter, req *http.Request) {
rest.Execute(c.command.DeclineRequest, rw, bytes.NewBufferString(fmt.Sprintf(`{
"piid":%q,
"reason":%q
}`, mux.Vars(req)["piid"], req.URL.Query().Get("reason"))))
}
// AcceptProblemReport swagger:route POST /introduce/{piid}/accept-problem-report introduce introduceAcceptProblemReport
//
// Accepts a problem report.
//
// Responses:
// default: genericError
// 200: introduceAcceptProblemReportResponse
func (c *Operation) AcceptProblemReport(rw http.ResponseWriter, req *http.Request) {
rest.Execute(c.command.AcceptProblemReport, rw, bytes.NewBufferString(fmt.Sprintf(`{
"piid":%q
}`, mux.Vars(req)["piid"])))
}
func toCommandRequest(rw http.ResponseWriter, req *http.Request) (bool, io.Reader) {
var buf bytes.Buffer
if req.Body != nil {
// nolint: errcheck
_, _ = io.Copy(&buf, req.Body)
}
if !isJSONMap(buf.Bytes()) {
rest.SendHTTPStatusError(rw,
http.StatusBadRequest,
introduce.InvalidRequestErrorCode,
errors.New("payload was not provided"),
)
return false, nil
}
ending := fmt.Sprintf(`"piid":%q}`, mux.Vars(req)["piid"])
payload := strings.TrimSpace(buf.String())
if payload == "{}" {
payload = "{" + ending
} else {
payload = buf.String()[:buf.Len()-1] + "," + ending
}
return true, bytes.NewBufferString(payload)
}
func isJSONMap(data []byte) bool {
var v struct{}
return isJSON(data, &v)
}
func isJSON(data []byte, v interface{}) bool {
return json.Unmarshal(data, &v) == nil
}