/
client.go
243 lines (210 loc) · 7.88 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Code generated by truss.
// Rerunning truss will overwrite this file.
// DO NOT EDIT!
// Package grpc provides a gRPC client for the Ambition service.
package grpc
import (
"github.com/pkg/errors"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"github.com/go-kit/kit/endpoint"
grpctransport "github.com/go-kit/kit/transport/grpc"
// This Service
pb "github.com/adamryman/ambition-model/ambition-service"
"github.com/adamryman/ambition-model/ambition-service/svc"
)
// New returns an service backed by a gRPC client connection. It is the
// responsibility of the caller to dial, and later close, the connection.
func New(conn *grpc.ClientConn, options ...ClientOption) (pb.AmbitionServer, error) {
var cc clientConfig
for _, f := range options {
err := f(&cc)
if err != nil {
return nil, errors.Wrap(err, "cannot apply option")
}
}
clientOptions := []grpctransport.ClientOption{
grpctransport.ClientBefore(
contextValuesToGRPCMetadata(cc.headers)),
}
var createactionEndpoint endpoint.Endpoint
{
createactionEndpoint = grpctransport.NewClient(
conn,
"ambition.Ambition",
"CreateAction",
EncodeGRPCCreateActionRequest,
DecodeGRPCCreateActionResponse,
pb.Action{},
clientOptions...,
).Endpoint()
}
var createoccurrenceEndpoint endpoint.Endpoint
{
createoccurrenceEndpoint = grpctransport.NewClient(
conn,
"ambition.Ambition",
"CreateOccurrence",
EncodeGRPCCreateOccurrenceRequest,
DecodeGRPCCreateOccurrenceResponse,
pb.Occurrence{},
clientOptions...,
).Endpoint()
}
var readactionEndpoint endpoint.Endpoint
{
readactionEndpoint = grpctransport.NewClient(
conn,
"ambition.Ambition",
"ReadAction",
EncodeGRPCReadActionRequest,
DecodeGRPCReadActionResponse,
pb.Action{},
clientOptions...,
).Endpoint()
}
var readactionsEndpoint endpoint.Endpoint
{
readactionsEndpoint = grpctransport.NewClient(
conn,
"ambition.Ambition",
"ReadActions",
EncodeGRPCReadActionsRequest,
DecodeGRPCReadActionsResponse,
pb.ActionsResponse{},
clientOptions...,
).Endpoint()
}
var readoccurrencesbydateEndpoint endpoint.Endpoint
{
readoccurrencesbydateEndpoint = grpctransport.NewClient(
conn,
"ambition.Ambition",
"ReadOccurrencesByDate",
EncodeGRPCReadOccurrencesByDateRequest,
DecodeGRPCReadOccurrencesByDateResponse,
pb.OccurrencesResponse{},
clientOptions...,
).Endpoint()
}
var readoccurrencesEndpoint endpoint.Endpoint
{
readoccurrencesEndpoint = grpctransport.NewClient(
conn,
"ambition.Ambition",
"ReadOccurrences",
EncodeGRPCReadOccurrencesRequest,
DecodeGRPCReadOccurrencesResponse,
pb.OccurrencesResponse{},
clientOptions...,
).Endpoint()
}
return svc.Endpoints{
CreateActionEndpoint: createactionEndpoint,
CreateOccurrenceEndpoint: createoccurrenceEndpoint,
ReadActionEndpoint: readactionEndpoint,
ReadActionsEndpoint: readactionsEndpoint,
ReadOccurrencesByDateEndpoint: readoccurrencesbydateEndpoint,
ReadOccurrencesEndpoint: readoccurrencesEndpoint,
}, nil
}
// GRPC Client Decode
// DecodeGRPCCreateActionResponse is a transport/grpc.DecodeResponseFunc that converts a
// gRPC createaction reply to a user-domain createaction response. Primarily useful in a client.
func DecodeGRPCCreateActionResponse(_ context.Context, grpcReply interface{}) (interface{}, error) {
reply := grpcReply.(*pb.Action)
return reply, nil
}
// DecodeGRPCCreateOccurrenceResponse is a transport/grpc.DecodeResponseFunc that converts a
// gRPC createoccurrence reply to a user-domain createoccurrence response. Primarily useful in a client.
func DecodeGRPCCreateOccurrenceResponse(_ context.Context, grpcReply interface{}) (interface{}, error) {
reply := grpcReply.(*pb.Occurrence)
return reply, nil
}
// DecodeGRPCReadActionResponse is a transport/grpc.DecodeResponseFunc that converts a
// gRPC readaction reply to a user-domain readaction response. Primarily useful in a client.
func DecodeGRPCReadActionResponse(_ context.Context, grpcReply interface{}) (interface{}, error) {
reply := grpcReply.(*pb.Action)
return reply, nil
}
// DecodeGRPCReadActionsResponse is a transport/grpc.DecodeResponseFunc that converts a
// gRPC readactions reply to a user-domain readactions response. Primarily useful in a client.
func DecodeGRPCReadActionsResponse(_ context.Context, grpcReply interface{}) (interface{}, error) {
reply := grpcReply.(*pb.ActionsResponse)
return reply, nil
}
// DecodeGRPCReadOccurrencesByDateResponse is a transport/grpc.DecodeResponseFunc that converts a
// gRPC readoccurrencesbydate reply to a user-domain readoccurrencesbydate response. Primarily useful in a client.
func DecodeGRPCReadOccurrencesByDateResponse(_ context.Context, grpcReply interface{}) (interface{}, error) {
reply := grpcReply.(*pb.OccurrencesResponse)
return reply, nil
}
// DecodeGRPCReadOccurrencesResponse is a transport/grpc.DecodeResponseFunc that converts a
// gRPC readoccurrences reply to a user-domain readoccurrences response. Primarily useful in a client.
func DecodeGRPCReadOccurrencesResponse(_ context.Context, grpcReply interface{}) (interface{}, error) {
reply := grpcReply.(*pb.OccurrencesResponse)
return reply, nil
}
// GRPC Client Encode
// EncodeGRPCCreateActionRequest is a transport/grpc.EncodeRequestFunc that converts a
// user-domain createaction request to a gRPC createaction request. Primarily useful in a client.
func EncodeGRPCCreateActionRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.Action)
return req, nil
}
// EncodeGRPCCreateOccurrenceRequest is a transport/grpc.EncodeRequestFunc that converts a
// user-domain createoccurrence request to a gRPC createoccurrence request. Primarily useful in a client.
func EncodeGRPCCreateOccurrenceRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.CreateOccurrenceRequest)
return req, nil
}
// EncodeGRPCReadActionRequest is a transport/grpc.EncodeRequestFunc that converts a
// user-domain readaction request to a gRPC readaction request. Primarily useful in a client.
func EncodeGRPCReadActionRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.Action)
return req, nil
}
// EncodeGRPCReadActionsRequest is a transport/grpc.EncodeRequestFunc that converts a
// user-domain readactions request to a gRPC readactions request. Primarily useful in a client.
func EncodeGRPCReadActionsRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.User)
return req, nil
}
// EncodeGRPCReadOccurrencesByDateRequest is a transport/grpc.EncodeRequestFunc that converts a
// user-domain readoccurrencesbydate request to a gRPC readoccurrencesbydate request. Primarily useful in a client.
func EncodeGRPCReadOccurrencesByDateRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.OccurrencesByDateReq)
return req, nil
}
// EncodeGRPCReadOccurrencesRequest is a transport/grpc.EncodeRequestFunc that converts a
// user-domain readoccurrences request to a gRPC readoccurrences request. Primarily useful in a client.
func EncodeGRPCReadOccurrencesRequest(_ context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.Action)
return req, nil
}
type clientConfig struct {
headers []string
}
// ClientOption is a function that modifies the client config
type ClientOption func(*clientConfig) error
func CtxValuesToSend(keys ...string) ClientOption {
return func(o *clientConfig) error {
o.headers = keys
return nil
}
}
func contextValuesToGRPCMetadata(keys []string) grpctransport.RequestFunc {
return func(ctx context.Context, md *metadata.MD) context.Context {
var pairs []string
for _, k := range keys {
if v, ok := ctx.Value(k).(string); ok {
pairs = append(pairs, k, v)
}
}
if pairs != nil {
*md = metadata.Join(*md, metadata.Pairs(pairs...))
}
return ctx
}
}