/
client.go
43 lines (35 loc) · 1.18 KB
/
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
package bertybridge
import (
"context"
"google.golang.org/grpc"
"berty.tech/berty/v2/go/internal/grpcutil"
)
type Client struct {
grpcClient *grpc.ClientConn
}
func (c *Client) Close() error {
return c.grpcClient.Close()
}
// UnaryRequest request make an unary request to the given method.
// the request need to be already serialized
func (c *Client) UnaryRequest(ctx context.Context, method string, req []byte) (res []byte, err error) {
codec := grpcutil.NewLazyCodec()
in := grpcutil.NewLazyMessage().FromBytes(req)
out := grpcutil.NewLazyMessage()
err = c.grpcClient.Invoke(ctx, method, in, out, grpc.ForceCodec(codec))
res = out.Bytes()
return
}
// UnaryRequestFromBase64 request make an unary request to the given method.
// the request need to be already serialized and encoded in a base64 string
func (c *Client) UnaryRequestFromBase64(method string, b64req string) (b64res string, err error) {
codec := grpcutil.NewLazyCodec()
in, err := grpcutil.NewLazyMessage().FromBase64(b64req)
if err != nil {
return "", err
}
out := grpcutil.NewLazyMessage()
err = c.grpcClient.Invoke(context.Background(), method, in, out, grpc.ForceCodec(codec))
b64res = out.Base64()
return
}