-
-
Notifications
You must be signed in to change notification settings - Fork 585
Expand file tree
/
Copy pathclient.go
More file actions
279 lines (261 loc) · 8.01 KB
/
client.go
File metadata and controls
279 lines (261 loc) · 8.01 KB
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
package codegen
import (
"fmt"
"path/filepath"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/codegen/service"
"goa.design/goa/v3/expr"
)
// ClientFiles returns the generated HTTP client files.
func ClientFiles(genpkg string, services *ServicesData) []*codegen.File {
root := services.Root
var files []*codegen.File
for _, svc := range root.API.HTTP.Services {
files = append(files, clientFile(genpkg, svc, services))
if f := websocketClientFile(genpkg, svc, services); f != nil {
files = append(files, f)
}
if f := sseClientFile(genpkg, svc, services); f != nil {
files = append(files, f)
}
}
for _, svc := range root.API.HTTP.Services {
if f := clientEncodeDecodeFile(genpkg, svc, services); f != nil {
files = append(files, f)
}
}
return files
}
// clientFile returns the client HTTP transport file
func clientFile(genpkg string, svc *expr.HTTPServiceExpr, services *ServicesData) *codegen.File {
data := services.Get(svc.Name())
svcName := data.Service.PathName
path := filepath.Join(codegen.Gendir, "http", svcName, "client", "client.go")
title := fmt.Sprintf("%s client HTTP transport", svc.Name())
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", []*codegen.ImportSpec{
{Path: "context"},
{Path: "fmt"},
{Path: "io"},
{Path: "mime/multipart"},
{Path: "net/http"},
{Path: "strconv"},
{Path: "strings"},
{Path: "time"},
{Path: "github.com/gorilla/websocket"},
codegen.GoaImport(""),
codegen.GoaNamedImport("http", "goahttp"),
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
}),
}
sections = append(sections, &codegen.SectionTemplate{
Name: "client-struct",
Source: readTemplate("client_struct"),
Data: data,
FuncMap: map[string]any{
"hasWebSocket": hasWebSocket,
"hasSSE": hasSSE,
},
})
for _, e := range data.Endpoints {
if e.MultipartRequestEncoder != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "multipart-request-encoder-type",
Source: readTemplate("multipart_request_encoder_type"),
Data: e.MultipartRequestEncoder,
})
}
}
sections = append(sections, &codegen.SectionTemplate{
Name: "http-client-init",
Source: readTemplate("client_init"),
Data: data,
FuncMap: map[string]any{
"hasWebSocket": hasWebSocket,
"hasSSE": hasSSE,
},
})
for _, e := range data.Endpoints {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-endpoint-init",
Source: readTemplate("endpoint_init"),
Data: e,
FuncMap: map[string]any{
"isWebSocketEndpoint": isWebSocketEndpoint,
"isSSEEndpoint": isSSEEndpoint,
"responseStructPkg": responseStructPkg,
},
})
}
return &codegen.File{Path: path, SectionTemplates: sections}
}
// clientEncodeDecodeFile returns the file containing the HTTP client encoding
// and decoding logic.
func clientEncodeDecodeFile(genpkg string, svc *expr.HTTPServiceExpr, services *ServicesData) *codegen.File {
data := services.Get(svc.Name())
svcName := data.Service.PathName
path := filepath.Join(codegen.Gendir, "http", svcName, "client", "encode_decode.go")
title := fmt.Sprintf("%s HTTP client encoders and decoders", svc.Name())
imports := []*codegen.ImportSpec{
{Path: "bytes"},
{Path: "context"},
{Path: "encoding/json"},
{Path: "fmt"},
{Path: "io"},
{Path: "mime/multipart"},
{Path: "net/http"},
{Path: "net/url"},
{Path: "os"},
{Path: "strconv"},
{Path: "strings"},
{Path: "unicode/utf8"},
codegen.GoaImport(""),
codegen.GoaNamedImport("http", "goahttp"),
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
}
sections := []*codegen.SectionTemplate{codegen.Header(title, "client", imports)}
for _, e := range data.Endpoints {
sections = append(sections, &codegen.SectionTemplate{
Name: "request-builder",
Source: readTemplate("request_builder"),
Data: e,
})
if e.RequestEncoder != "" && e.Payload.Ref != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "request-encoder",
Source: readTemplate("request_encoder", "client_type_conversion", "client_map_conversion"),
FuncMap: map[string]any{
"typeConversionData": typeConversionData,
"mapConversionData": mapConversionData,
"goTypeRef": func(dt expr.DataType) string {
return services.ServicesData.Get(svc.Name()).Scope.GoTypeRef(&expr.AttributeExpr{Type: dt})
},
"isBearer": isBearer,
"aliasedType": fieldType,
"isAlias": func(dt expr.DataType) bool {
_, ok := dt.(expr.UserType)
return ok
},
"underlyingType": func(dt expr.DataType) expr.DataType {
if ut, ok := dt.(expr.UserType); ok {
return ut.Attribute().Type
}
return dt
},
"requestStructPkg": requestStructPkg,
},
Data: e,
})
}
if e.MultipartRequestEncoder != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "multipart-request-encoder",
Source: readTemplate("multipart_request_encoder"),
Data: e.MultipartRequestEncoder,
})
}
if e.Result != nil || len(e.Errors) > 0 {
sections = append(sections, &codegen.SectionTemplate{
Name: "response-decoder",
Source: readTemplate("response_decoder", "single_response", "query_type_conversion", "element_slice_conversion", "slice_item_conversion"),
Data: e,
FuncMap: map[string]any{
"goTypeRef": func(dt expr.DataType) string {
return services.ServicesData.Get(svc.Name()).Scope.GoTypeRef(&expr.AttributeExpr{Type: dt})
},
"buildResponseData": buildResponseData,
},
})
}
if e.Method.SkipRequestBodyEncodeDecode {
sections = append(sections, &codegen.SectionTemplate{
Name: "build-stream-request",
Source: readTemplate("build_stream_request"),
Data: e,
FuncMap: map[string]any{
"requestStructPkg": requestStructPkg,
},
})
}
}
for _, h := range data.ClientTransformHelpers {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-transform-helper",
Source: readTemplate("transform_helper"),
Data: h,
})
}
return &codegen.File{Path: path, SectionTemplates: sections}
}
// typeConversionData produces the template data suitable for executing the
// "header_conversion" template.
func typeConversionData(dt, ft expr.DataType, varName, target string) map[string]any {
ut, isut := ft.(expr.UserType)
if isut {
ft = ut.Attribute().Type
}
return map[string]any{
"Type": dt,
"FieldType": ft,
"VarName": varName,
"Target": target,
"IsAliased": isut,
}
}
func mapConversionData(dt, ft expr.DataType, varName, sourceVar, sourceField string, newVar bool) map[string]any {
ut, isut := ft.(expr.UserType)
if isut {
ft = ut.Attribute().Type
}
return map[string]any{
"Type": dt,
"FieldType": ft,
"VarName": varName,
"SourceVar": sourceVar,
"SourceField": sourceField,
"NewVar": newVar,
"IsAliased": isut,
}
}
// buildResponseData produces the template data suitable for executing the
// "single_response" partial template.
func buildResponseData(data *ResponseData, serviceName string, method *service.MethodData) map[string]any {
return map[string]any{
"Data": data,
"ServiceName": serviceName,
"Method": method,
}
}
func fieldType(ft expr.DataType) expr.DataType {
ut, isut := ft.(expr.UserType)
if isut {
return ut.Attribute().Type
}
return ft
}
// isBearer returns true if the security scheme uses a Bearer scheme.
func isBearer(schemes []*service.SchemeData) bool {
for _, s := range schemes {
if s.Name != "Authorization" {
continue
}
if s.Type == "JWT" || s.Type == "OAuth2" {
return true
}
}
return false
}
func requestStructPkg(m *service.MethodData, def string) string {
if m.PayloadLoc != nil {
return m.PayloadLoc.PackageName()
}
return def
}
func responseStructPkg(m *service.MethodData, def string) string {
if m.ResultLoc != nil {
return m.ResultLoc.PackageName()
}
return def
}