-
Notifications
You must be signed in to change notification settings - Fork 66
/
bar_bar_method_argwithheaders.go
176 lines (149 loc) · 6.03 KB
/
bar_bar_method_argwithheaders.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
// Code generated by zanzibar
// @generated
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package workflow
import (
"context"
"net/textproto"
"github.com/uber/zanzibar/config"
zanzibar "github.com/uber/zanzibar/runtime"
clientsBarBar "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/clients/bar/bar"
endpointsBarBar "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/endpoints/bar/bar"
module "github.com/uber/zanzibar/examples/example-gateway/build/endpoints/bar/module"
"go.uber.org/zap"
)
// BarArgWithHeadersWorkflow defines the interface for BarArgWithHeaders workflow
type BarArgWithHeadersWorkflow interface {
Handle(
ctx context.Context,
reqHeaders zanzibar.Header,
r *endpointsBarBar.Bar_ArgWithHeaders_Args,
) (*endpointsBarBar.BarResponse, zanzibar.Header, error)
}
// NewBarArgWithHeadersWorkflow creates a workflow
func NewBarArgWithHeadersWorkflow(deps *module.Dependencies) BarArgWithHeadersWorkflow {
var whitelistedDynamicHeaders []string
if deps.Default.Config.ContainsKey("clients.bar.alternates") {
var alternateServiceDetail config.AlternateServiceDetail
deps.Default.Config.MustGetStruct("clients.bar.alternates", &alternateServiceDetail)
for _, routingConfig := range alternateServiceDetail.RoutingConfigs {
whitelistedDynamicHeaders = append(whitelistedDynamicHeaders, textproto.CanonicalMIMEHeaderKey(routingConfig.HeaderName))
}
}
return &barArgWithHeadersWorkflow{
Clients: deps.Client,
Logger: deps.Default.Logger,
whitelistedDynamicHeaders: whitelistedDynamicHeaders,
}
}
// barArgWithHeadersWorkflow calls thrift client Bar.ArgWithHeaders
type barArgWithHeadersWorkflow struct {
Clients *module.ClientDependencies
Logger *zap.Logger
whitelistedDynamicHeaders []string
}
// Handle calls thrift client.
func (w barArgWithHeadersWorkflow) Handle(
ctx context.Context,
reqHeaders zanzibar.Header,
r *endpointsBarBar.Bar_ArgWithHeaders_Args,
) (*endpointsBarBar.BarResponse, zanzibar.Header, error) {
clientRequest := convertToArgWithHeadersClientRequest(r)
clientHeaders := map[string]string{}
var ok bool
var h string
h, ok = reqHeaders.Get("X-Deputy-Forwarded")
if ok {
clientHeaders["X-Deputy-Forwarded"] = h
}
h, ok = reqHeaders.Get("X-Uuid")
if ok {
clientHeaders["x-uuid"] = h
}
for _, whitelistedHeader := range w.whitelistedDynamicHeaders {
headerVal, ok := reqHeaders.Get(whitelistedHeader)
if ok {
clientHeaders[whitelistedHeader] = headerVal
}
}
clientRespBody, _, err := w.Clients.Bar.ArgWithHeaders(
ctx, clientHeaders, clientRequest,
)
if err != nil {
switch errValue := err.(type) {
default:
w.Logger.Warn("Client failure: could not make client request",
zap.Error(errValue),
zap.String("client", "Bar"),
)
return nil, nil, err
}
}
// Filter and map response headers from client to server response.
resHeaders := zanzibar.ServerHTTPHeader{}
response := convertBarArgWithHeadersClientResponse(clientRespBody)
return response, resHeaders, nil
}
func convertToArgWithHeadersClientRequest(in *endpointsBarBar.Bar_ArgWithHeaders_Args) *clientsBarBar.Bar_ArgWithHeaders_Args {
out := &clientsBarBar.Bar_ArgWithHeaders_Args{}
out.Name = string(in.Name)
out.UserUUID = (*string)(in.UserUUID)
return out
}
func convertBarArgWithHeadersClientResponse(in *clientsBarBar.BarResponse) *endpointsBarBar.BarResponse {
out := &endpointsBarBar.BarResponse{}
out.StringField = string(in.StringField)
out.IntWithRange = int32(in.IntWithRange)
out.IntWithoutRange = int32(in.IntWithoutRange)
out.MapIntWithRange = make(map[endpointsBarBar.UUID]int32, len(in.MapIntWithRange))
for key1, value2 := range in.MapIntWithRange {
out.MapIntWithRange[endpointsBarBar.UUID(key1)] = int32(value2)
}
out.MapIntWithoutRange = make(map[string]int32, len(in.MapIntWithoutRange))
for key3, value4 := range in.MapIntWithoutRange {
out.MapIntWithoutRange[key3] = int32(value4)
}
out.BinaryField = []byte(in.BinaryField)
var convertBarResponseHelper5 func(in *clientsBarBar.BarResponse) (out *endpointsBarBar.BarResponse)
convertBarResponseHelper5 = func(in *clientsBarBar.BarResponse) (out *endpointsBarBar.BarResponse) {
if in != nil {
out = &endpointsBarBar.BarResponse{}
out.StringField = string(in.StringField)
out.IntWithRange = int32(in.IntWithRange)
out.IntWithoutRange = int32(in.IntWithoutRange)
out.MapIntWithRange = make(map[endpointsBarBar.UUID]int32, len(in.MapIntWithRange))
for key6, value7 := range in.MapIntWithRange {
out.MapIntWithRange[endpointsBarBar.UUID(key6)] = int32(value7)
}
out.MapIntWithoutRange = make(map[string]int32, len(in.MapIntWithoutRange))
for key8, value9 := range in.MapIntWithoutRange {
out.MapIntWithoutRange[key8] = int32(value9)
}
out.BinaryField = []byte(in.BinaryField)
out.NextResponse = convertBarResponseHelper5(in.NextResponse)
} else {
out = nil
}
return
}
out.NextResponse = convertBarResponseHelper5(in.NextResponse)
return out
}