-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription_service_http.pb.go
328 lines (275 loc) · 10.1 KB
/
subscription_service_http.pb.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Code generated by protoc-gen-go-server. DO NOT EDIT.
// See https://github.com/eolymp/contracts/tree/main/cmd/protoc-gen-go-server for more details.
package community
import (
context "context"
fmt "fmt"
mux "github.com/gorilla/mux"
websocket "golang.org/x/net/websocket"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
metadata "google.golang.org/grpc/metadata"
status "google.golang.org/grpc/status"
protojson "google.golang.org/protobuf/encoding/protojson"
proto "google.golang.org/protobuf/proto"
ioutil "io/ioutil"
http "net/http"
)
// _SubscriptionService_HTTPReadQueryString parses body into proto.Message
func _SubscriptionService_HTTPReadQueryString(r *http.Request, v proto.Message) error {
query := r.URL.Query().Get("q")
if query == "" {
return nil
}
if err := (protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal([]byte(query), v)); err != nil {
return err
}
return nil
}
// _SubscriptionService_HTTPReadRequestBody parses body into proto.Message
func _SubscriptionService_HTTPReadRequestBody(r *http.Request, v proto.Message) error {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if err := (protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(data, v)); err != nil {
return err
}
return nil
}
// _SubscriptionService_HTTPWriteResponse writes proto.Message to HTTP response
func _SubscriptionService_HTTPWriteResponse(w http.ResponseWriter, v proto.Message, h, t metadata.MD) {
data, err := protojson.Marshal(v)
if err != nil {
_SubscriptionService_HTTPWriteErrorResponse(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
if v := append(h.Get("cache-control"), t.Get("cache-control")...); len(v) > 0 {
w.Header().Set("Cache-Control", v[len(v)-1])
}
if v := append(h.Get("etag"), t.Get("etag")...); len(v) > 0 {
w.Header().Set("ETag", v[len(v)-1])
}
if v := append(h.Get("last-modified"), t.Get("last-modified")...); len(v) > 0 {
w.Header().Set("Last-Modified", v[len(v)-1])
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data)
}
// _SubscriptionService_HTTPWriteErrorResponse writes error to HTTP response with error status code
func _SubscriptionService_HTTPWriteErrorResponse(w http.ResponseWriter, e error) {
s := status.Convert(e)
w.Header().Set("Content-Type", "application/json")
switch s.Code() {
case codes.OK:
w.WriteHeader(http.StatusOK)
case codes.Canceled:
w.WriteHeader(http.StatusGatewayTimeout)
case codes.Unknown:
w.WriteHeader(http.StatusInternalServerError)
case codes.InvalidArgument:
w.WriteHeader(http.StatusBadRequest)
case codes.DeadlineExceeded:
w.WriteHeader(http.StatusGatewayTimeout)
case codes.NotFound:
w.WriteHeader(http.StatusNotFound)
case codes.AlreadyExists:
w.WriteHeader(http.StatusConflict)
case codes.PermissionDenied:
w.WriteHeader(http.StatusForbidden)
case codes.ResourceExhausted:
w.WriteHeader(http.StatusTooManyRequests)
case codes.FailedPrecondition:
w.WriteHeader(http.StatusPreconditionFailed)
case codes.Aborted:
w.WriteHeader(http.StatusServiceUnavailable)
case codes.OutOfRange:
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
case codes.Unimplemented:
w.WriteHeader(http.StatusNotImplemented)
case codes.Internal:
w.WriteHeader(http.StatusInternalServerError)
case codes.Unavailable:
w.WriteHeader(http.StatusServiceUnavailable)
case codes.DataLoss:
w.WriteHeader(http.StatusInternalServerError)
case codes.Unauthenticated:
w.WriteHeader(http.StatusUnauthorized)
default:
w.WriteHeader(http.StatusInternalServerError)
}
data, err := protojson.Marshal(s.Proto())
if err != nil {
panic(err)
}
_, _ = w.Write(data)
}
// _SubscriptionService_WebsocketErrorResponse writes error to websocket connection
func _SubscriptionService_WebsocketErrorResponse(conn *websocket.Conn, e error) {
switch status.Convert(e).Code() {
case codes.OK:
conn.WriteClose(1000)
case codes.Canceled:
conn.WriteClose(1000)
case codes.Unknown:
conn.WriteClose(1011)
case codes.InvalidArgument:
conn.WriteClose(1003)
case codes.DeadlineExceeded:
conn.WriteClose(1000)
case codes.NotFound:
conn.WriteClose(1000)
case codes.AlreadyExists:
conn.WriteClose(1000)
case codes.PermissionDenied:
conn.WriteClose(1000)
case codes.ResourceExhausted:
conn.WriteClose(1000)
case codes.FailedPrecondition:
conn.WriteClose(1000)
case codes.Aborted:
conn.WriteClose(1000)
case codes.OutOfRange:
conn.WriteClose(1000)
case codes.Unimplemented:
conn.WriteClose(1011)
case codes.Internal:
conn.WriteClose(1011)
case codes.Unavailable:
conn.WriteClose(1011)
case codes.DataLoss:
conn.WriteClose(1011)
case codes.Unauthenticated:
conn.WriteClose(1000)
default:
conn.WriteClose(1000)
}
}
// _SubscriptionService_WebsocketCodec implements protobuf codec for websockets package
var _SubscriptionService_WebsocketCodec = websocket.Codec{
Marshal: func(v interface{}) ([]byte, byte, error) {
m, ok := v.(proto.Message)
if !ok {
panic(fmt.Errorf("invalid message type %T", v))
}
d, err := protojson.Marshal(m)
if err != nil {
return nil, 0, err
}
return d, websocket.TextFrame, err
},
Unmarshal: func(d []byte, t byte, v interface{}) error {
m, ok := v.(proto.Message)
if !ok {
panic(fmt.Errorf("invalid message type %T", v))
}
return protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(d, m)
},
}
// RegisterSubscriptionServiceHttpHandlers adds handlers for for SubscriptionServiceClient
// This constructor creates http.Handler, the actual implementation might change at any moment
func RegisterSubscriptionServiceHttpHandlers(router *mux.Router, prefix string, cli SubscriptionServiceClient) {
router.Handle(prefix+"/subscriptions/{subscription_id}", _SubscriptionService_DescribeSubscription_Rule0(cli)).
Methods("GET").
Name("eolymp.community.SubscriptionService.DescribeSubscription")
router.Handle(prefix+"/subscriptions", _SubscriptionService_ListSubscriptions_Rule0(cli)).
Methods("GET").
Name("eolymp.community.SubscriptionService.ListSubscriptions")
}
func _SubscriptionService_DescribeSubscription_Rule0(cli SubscriptionServiceClient) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
in := &DescribeSubscriptionInput{}
if err := _SubscriptionService_HTTPReadQueryString(r, in); err != nil {
err = status.Error(codes.InvalidArgument, err.Error())
_SubscriptionService_HTTPWriteErrorResponse(w, err)
return
}
vars := mux.Vars(r)
in.SubscriptionId = vars["subscription_id"]
var header, trailer metadata.MD
out, err := cli.DescribeSubscription(r.Context(), in, grpc.Header(&header), grpc.Trailer(&trailer))
if err != nil {
_SubscriptionService_HTTPWriteErrorResponse(w, err)
return
}
_SubscriptionService_HTTPWriteResponse(w, out, header, trailer)
})
}
func _SubscriptionService_ListSubscriptions_Rule0(cli SubscriptionServiceClient) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
in := &ListSubscriptionsInput{}
if err := _SubscriptionService_HTTPReadQueryString(r, in); err != nil {
err = status.Error(codes.InvalidArgument, err.Error())
_SubscriptionService_HTTPWriteErrorResponse(w, err)
return
}
var header, trailer metadata.MD
out, err := cli.ListSubscriptions(r.Context(), in, grpc.Header(&header), grpc.Trailer(&trailer))
if err != nil {
_SubscriptionService_HTTPWriteErrorResponse(w, err)
return
}
_SubscriptionService_HTTPWriteResponse(w, out, header, trailer)
})
}
type _SubscriptionServiceHandler = func(ctx context.Context, in proto.Message) (proto.Message, error)
type _SubscriptionServiceMiddleware = func(ctx context.Context, method string, in proto.Message, handler _SubscriptionServiceHandler) (out proto.Message, err error)
type SubscriptionServiceInterceptor struct {
middleware []_SubscriptionServiceMiddleware
client SubscriptionServiceClient
}
// NewSubscriptionServiceInterceptor constructs additional middleware for a server based on annotations in proto files
func NewSubscriptionServiceInterceptor(cli SubscriptionServiceClient, middleware ..._SubscriptionServiceMiddleware) *SubscriptionServiceInterceptor {
return &SubscriptionServiceInterceptor{client: cli, middleware: middleware}
}
func (i *SubscriptionServiceInterceptor) DescribeSubscription(ctx context.Context, in *DescribeSubscriptionInput, opts ...grpc.CallOption) (*DescribeSubscriptionOutput, error) {
handler := func(ctx context.Context, in proto.Message) (proto.Message, error) {
message, ok := in.(*DescribeSubscriptionInput)
if !ok && in != nil {
panic(fmt.Errorf("request input type is invalid: want *DescribeSubscriptionInput, got %T", in))
}
return i.client.DescribeSubscription(ctx, message, opts...)
}
for _, mw := range i.middleware {
mw := mw
next := handler
handler = func(ctx context.Context, in proto.Message) (proto.Message, error) {
return mw(ctx, "eolymp.community.SubscriptionService.DescribeSubscription", in, next)
}
}
out, err := handler(ctx, in)
if err != nil {
return nil, err
}
message, ok := out.(*DescribeSubscriptionOutput)
if !ok && out != nil {
panic(fmt.Errorf("output type is invalid: want *DescribeSubscriptionOutput, got %T", out))
}
return message, err
}
func (i *SubscriptionServiceInterceptor) ListSubscriptions(ctx context.Context, in *ListSubscriptionsInput, opts ...grpc.CallOption) (*ListSubscriptionsOutput, error) {
handler := func(ctx context.Context, in proto.Message) (proto.Message, error) {
message, ok := in.(*ListSubscriptionsInput)
if !ok && in != nil {
panic(fmt.Errorf("request input type is invalid: want *ListSubscriptionsInput, got %T", in))
}
return i.client.ListSubscriptions(ctx, message, opts...)
}
for _, mw := range i.middleware {
mw := mw
next := handler
handler = func(ctx context.Context, in proto.Message) (proto.Message, error) {
return mw(ctx, "eolymp.community.SubscriptionService.ListSubscriptions", in, next)
}
}
out, err := handler(ctx, in)
if err != nil {
return nil, err
}
message, ok := out.(*ListSubscriptionsOutput)
if !ok && out != nil {
panic(fmt.Errorf("output type is invalid: want *ListSubscriptionsOutput, got %T", out))
}
return message, err
}