-
Notifications
You must be signed in to change notification settings - Fork 11
/
api_v2.go
102 lines (79 loc) · 3.26 KB
/
api_v2.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
package backend
import (
"context"
"errors"
"fmt"
"strings"
"code.vegaprotocol.io/vega/libs/jsonrpc"
vgrand "code.vegaprotocol.io/vega/libs/rand"
"code.vegaprotocol.io/vega/wallet/api/interactor"
v2 "code.vegaprotocol.io/vega/wallet/service/v2"
"code.vegaprotocol.io/vega/wallet/service/v2/connections"
"code.vegaprotocol.io/vegawallet-desktop/app"
"code.vegaprotocol.io/vegawallet-desktop/os/notification"
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.uber.org/zap"
)
const NewInteractionEvent = "new_interaction"
var (
ErrTraceIDIsRequired = errors.New("a trace ID is required for an interaction")
ErrNameIsRequired = errors.New("a name is required for an interaction")
)
func (h *Handler) APIV2GenerateAPIToken(params connections.GenerateAPITokenParams) (connections.Token, error) {
handler := connections.NewGenerateAPITokenHandler(h.walletStore, h.tokenStore, v2.NewStdTime())
return handler.Handle(h.ctx, params)
}
func (h *Handler) APIV2DeleteAPIToken(token string) error {
return connections.DeleteAPIToken(h.tokenStore, token)
}
func (h *Handler) APIV2ListAPITokens() (connections.ListAPITokensResult, error) {
return connections.ListAPITokens(h.tokenStore)
}
func (h *Handler) APIV2DescribeAPIToken(token string) (connections.TokenDescription, error) {
return connections.DescribeAPIToken(h.tokenStore, token)
}
func (h *Handler) SubmitWalletAPIRequest(request jsonrpc.Request) (*jsonrpc.Response, error) {
h.log.Debug("Entering SubmitWalletAPIRequest", zap.String("method", request.Method))
defer h.log.Debug("Leaving SubmitWalletAPIRequest", zap.String("method", request.Method))
if err := h.ensureAppIsInitialised(); err != nil {
return nil, err
}
traceID := vgrand.RandomStr(64)
ctx := context.WithValue(h.ctx, jsonrpc.TraceIDKey, traceID)
return h.walletAdminAPI.DispatchRequest(ctx, request), nil
}
func (h *Handler) RespondToInteraction(interaction interactor.Interaction) error {
h.log.Debug("Entering RespondToInteraction")
defer h.log.Debug("Leaving RespondToInteraction")
if interaction.TraceID == "" {
return ErrTraceIDIsRequired
}
if interaction.Name == "" {
return ErrNameIsRequired
}
h.log.Debug(fmt.Sprintf("Received a response %q with trace ID %q", interaction.Name, interaction.TraceID))
if h.ctx.Err() != nil {
h.log.Error("The application context has been canceled, could not respond to the interaction")
return ErrContextCanceled
}
h.runningServiceManager.responseChan <- interaction
return nil
}
func (h *Handler) emitReceivedInteraction(log *zap.Logger, interaction interactor.Interaction) {
log.Debug("Received a new interaction",
zap.String("interaction", string(interaction.Name)),
zap.String("trace-id", interaction.TraceID),
)
if shouldEmitOSNotification(interaction.Name) {
message := strings.ToLower(strings.ReplaceAll(string(interaction.Name), "_", " "))
if err := notification.Notify(app.Name, message); err != nil {
log.Warn("Could not send the OS notification", zap.Error(err))
}
}
runtime.EventsEmit(h.ctx, NewInteractionEvent, interaction)
}
func shouldEmitOSNotification(interactionName interactor.InteractionName) bool {
return !(interactionName == interactor.LogName ||
interactionName == interactor.InteractionSessionBeganName ||
interactionName == interactor.InteractionSessionEndedName)
}