/
client.go
61 lines (50 loc) · 1.69 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package blockchain
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"github.com/monnoroch/go-inject"
"github.com/monnoroch/go-inject/auto"
grpcinject "github.com/monnoroch/go-inject/examples/weather/grpc"
blockchainproto "github.com/monnoroch/go-inject/examples/weather/proto/blockchain"
)
/// A wrapper type around blockchain client.
type BlockchainClient struct {
RawBlockchainClient blockchainproto.BlockchainClient
}
/// Make payment using the blockchain service.
func (self *BlockchainClient) Pay(
ctx context.Context,
userId int64,
) bool {
requestMetadata, _ := metadata.FromIncomingContext(ctx)
ctx = metadata.NewOutgoingContext(ctx, requestMetadata)
_, err := self.RawBlockchainClient.Pay(ctx, &blockchainproto.PayRequest{
From: userId,
To: 12345, // our app's user id, not enough funding to make it a flag
AmountMicroEth: 5,
})
return err == nil
}
func (self BlockchainClient) ProvideAutoInjectAnnotations() interface{} {
return struct {
RawBlockchainClient private
}{}
}
/// Annotation used by the AI service client module.
type BlockchainService struct{}
/// Annotation for private providers.
type private struct{}
/// A module for providing AI service client components.
type blockchainServiceClientModule struct{}
func (_ blockchainServiceClientModule) ProvideCachedGrpcClient(
connection *grpc.ClientConn, _ grpcinject.GrpcClient,
) (blockchainproto.BlockchainClient, private) {
return blockchainproto.NewBlockchainClient(connection), private{}
}
func BlockchainServiceClientModule() inject.Module {
return inject.CombineModules(
blockchainServiceClientModule{},
autoinject.AutoInjectModule(new(BlockchainClient)),
)
}