forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_client.go
58 lines (46 loc) · 1.3 KB
/
service_client.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
package bertybridge
import (
"context"
"github.com/pkg/errors"
"berty.tech/berty/v2/go/internal/grpcutil"
)
type PromiseBlock interface {
CallResolve(reply string)
CallReject(error error)
}
type ServiceClient interface {
InvokeBridgeMethodWithPromiseBlock(promise PromiseBlock, method string, b64message string)
InvokeBridgeMethod(method string, b64message string) (string, error)
}
type serviceClient struct {
client *grpcutil.LazyClient
}
func NewServiceClient(cl *grpcutil.LazyClient) ServiceClient {
return &serviceClient{cl}
}
func (s *serviceClient) InvokeBridgeMethodWithPromiseBlock(promise PromiseBlock, method string, b64message string) {
go func() {
res, err := s.InvokeBridgeMethod(method, b64message)
// if an internal error occurred generate a new bridge error
if err != nil {
err = errors.Wrap(err, "unable to invoke bridge method")
promise.CallReject(err)
return
}
promise.CallResolve(res)
}()
}
func (s *serviceClient) InvokeBridgeMethod(method string, b64message string) (string, error) {
in, err := grpcutil.NewLazyMessage().FromBase64(b64message)
if err != nil {
return "", err
}
desc := &grpcutil.LazyMethodDesc{
Name: method,
}
out, err := s.client.InvokeUnary(context.Background(), desc, in)
if err != nil {
return "", err
}
return out.Base64(), nil
}