forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
150 lines (130 loc) · 4.79 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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mediator
import (
"fmt"
"net/http"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/controller/command/mediator"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/controller/internal/cmdutil"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/controller/rest"
"github.com/Universal-Health-Chain/aries-framework-go/pkg/kms"
)
// constants for the mediator operations.
const (
RouteOperationID = "/mediator"
RegisterPath = RouteOperationID + "/register"
UnregisterPath = RouteOperationID + "/unregister"
GetConnectionsPath = RouteOperationID + "/connections"
ReconnectPath = RouteOperationID + "/reconnect"
StatusPath = RouteOperationID + "/status"
BatchPickupPath = RouteOperationID + "/batchpickup"
ReconnectAllPath = RouteOperationID + "/reconnect-all"
)
// provider contains dependencies for the route protocol and is typically created by using aries.Context().
type provider interface {
Service(id string) (interface{}, error)
KMS() kms.KeyManager
ServiceEndpoint() string
}
// Operation contains basic common operations provided by controller REST API.
type Operation struct {
handlers []rest.Handler
command *mediator.Command
}
// New returns new common operations rest client instance.
func New(ctx provider, autoAccept bool) (*Operation, error) {
routeCmd, err := mediator.New(ctx, autoAccept)
if err != nil {
return nil, fmt.Errorf("create route command : %w", err)
}
o := &Operation{command: routeCmd}
o.registerHandler()
return o, nil
}
// GetRESTHandlers get all controller API handler available for this service.
func (o *Operation) GetRESTHandlers() []rest.Handler {
return o.handlers
}
// registerHandler register handlers to be exposed from this protocol service as REST API endpoints.
func (o *Operation) registerHandler() {
// Add more protocol endpoints here to expose them as controller API endpoints
o.handlers = []rest.Handler{
cmdutil.NewHTTPHandler(RegisterPath, http.MethodPost, o.Register),
cmdutil.NewHTTPHandler(UnregisterPath, http.MethodDelete, o.Unregister),
cmdutil.NewHTTPHandler(GetConnectionsPath, http.MethodGet, o.Connections),
cmdutil.NewHTTPHandler(ReconnectPath, http.MethodPost, o.Reconnect),
cmdutil.NewHTTPHandler(StatusPath, http.MethodPost, o.Status),
cmdutil.NewHTTPHandler(BatchPickupPath, http.MethodPost, o.BatchPickup),
cmdutil.NewHTTPHandler(ReconnectAllPath, http.MethodGet, o.ReconnectAll),
}
}
// Register swagger:route POST /mediator/register mediator registerRouteRequest
//
// Registers the agent with the router.
//
// Responses:
// default: genericError
// 200: registerRouteRes
func (o *Operation) Register(rw http.ResponseWriter, req *http.Request) {
rest.Execute(o.command.Register, rw, req.Body)
}
// Unregister swagger:route DELETE /mediator/unregister mediator unregisterRouter
//
// Unregisters the agent with the router.
//
// Responses:
// default: genericError
// 200: unregisterRouteRes
func (o *Operation) Unregister(rw http.ResponseWriter, req *http.Request) {
rest.Execute(o.command.Unregister, rw, req.Body)
}
// Connections swagger:route GET /mediator/connections mediator connectionsRequest
//
// Retrieves the router`s connections.
//
// Responses:
// default: genericError
// 200: getConnectionsResponse
func (o *Operation) Connections(rw http.ResponseWriter, req *http.Request) {
rest.Execute(o.command.Connections, rw, req.Body)
}
// Reconnect swagger:route POST /mediator/reconnect mediator reconnectRouteRequest
//
// Reconnect the agent with the router to re-establish lost connection.
//
// Responses:
// default: genericError
func (o *Operation) Reconnect(rw http.ResponseWriter, req *http.Request) {
rest.Execute(o.command.Reconnect, rw, req.Body)
}
// Status swagger:route POST /mediator/status mediator statusRequest
//
// Status returns details about pending messages for given connection.
//
// Responses:
// default: genericError
// 200: statusResponse
func (o *Operation) Status(rw http.ResponseWriter, req *http.Request) {
rest.Execute(o.command.Status, rw, req.Body)
}
// BatchPickup swagger:route POST /mediator/batchpickup mediator batchPickupRequest
//
// BatchPickup dispatches pending messages for given connection.
//
// Responses:
// default: genericError
// 200: batchPickupResponse
func (o *Operation) BatchPickup(rw http.ResponseWriter, req *http.Request) {
rest.Execute(o.command.BatchPickup, rw, req.Body)
}
// ReconnectAll swagger:route GET /mediator/reconnect-all mediator reconnectAll
//
// Re-establishes network connections for all mediator connections.
//
// Responses:
// default: genericError
func (o *Operation) ReconnectAll(rw http.ResponseWriter, req *http.Request) {
rest.Execute(o.command.ReconnectAll, rw, req.Body)
}