/
client.go
56 lines (46 loc) · 1.39 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
package ai
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
grpcinject "github.com/monnoroch/go-inject/examples/weather/grpc"
aiproto "github.com/monnoroch/go-inject/examples/weather/proto/ai"
)
/// A wrapper type around Ai client.
type AiClient struct {
RawAiClient aiproto.AiClient
}
/// Ask AI service for weather at location and time specified in arguments.
func (self *AiClient) AskForWeather(
ctx context.Context,
location string,
timestamp int64,
) string {
requestMetadata, _ := metadata.FromIncomingContext(ctx)
ctx = metadata.NewOutgoingContext(ctx, requestMetadata)
answer, _ := self.RawAiClient.Ask(ctx, &aiproto.Question{
Question: fmt.Sprintf(
"What's the weather at location '%s' at time '%d'",
location,
timestamp,
),
})
return answer.GetAnswer()
}
/// Annotation used by the AI service client module.
type AiService struct{}
/// Annotation for private providers.
type private struct{}
/// A module for providing AI service client components.
type AiServiceClientModule struct{}
func (_ AiServiceClientModule) ProvideCachedGrpcClient(
connection *grpc.ClientConn, _ grpcinject.GrpcClient,
) (aiproto.AiClient, private) {
return aiproto.NewAiClient(connection), private{}
}
func (_ AiServiceClientModule) ProvideAiClient(
client aiproto.AiClient, _ private,
) (AiClient, AiService) {
return AiClient{RawAiClient: client}, AiService{}
}