1
+
2
+ { {> partial_header} }
3
+ package { {packageName} }
4
+
5
+ { {#operations} }
6
+ import (
7
+ "context"
8
+ "io/ioutil"
9
+ "net/http"
10
+ "net/url"
11
+ "strings"
12
+ { {#imports} } "{ {import} }"
13
+ { {/imports} }
14
+ )
15
+
16
+ // Linger please
17
+ var (
18
+ _ context.Context
19
+ )
20
+
21
+ type { {classname} }Service service
22
+ { {#operation} }
23
+
24
+ /*
25
+ { {{classname} }}Service{ {#summary} } { {.} }{ {/summary} }{ {#notes} }
26
+ { {notes} }{ {/notes} }
27
+ * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
28
+ { {#allParams} }{ {#required} } * @param { {paramName} }{ {#description} } { {.} }{ {/description} }
29
+ { {/required} }{ {/allParams} }{ {#hasOptionalParams} } * @param optional nil or *{ {{classname} }}{ {{nickname} }}Opts - Optional Parameters:
30
+ { {#allParams} }{ {^required} } * @param "{ {vendorExtensions.x-exportParamName} }" ({ {#isPrimitiveType} }optional.{ {vendorExtensions.x-optionalDataType} }{ {/isPrimitiveType} }{ {^isPrimitiveType} }optional.Interface of { {dataType} }{ {/isPrimitiveType} }) - { {#description} } { {.} }{ {/description} }
31
+ { {/required} }{ {/allParams} }{ {/hasOptionalParams} }
32
+ { {#returnType} }@return { {{returnType} }}{ {/returnType} }
33
+ */
34
+ { {#hasOptionalParams} }
35
+
36
+ type { {{classname} }}{ {{nickname} }}Opts struct { {{#allParams} }{ {^required} }
37
+ { {#isPrimitiveType} } { {vendorExtensions.x-exportParamName} } optional.{ {vendorExtensions.x-optionalDataType} }{ {/isPrimitiveType} }{ {^isPrimitiveType} } { {vendorExtensions.x-exportParamName} } optional.Interface{ {/isPrimitiveType} }{ {/required} }{ {/allParams} }
38
+ }
39
+
40
+ { {/hasOptionalParams} }
41
+ func (a *{ {{classname} }}Service) { {{nickname} }}(ctx context.Context{ {#hasParams} }, { {/hasParams} }{ {#allParams} }{ {#required} }{ {paramName} } { {{dataType} }}{ {#hasMore} }, { {/hasMore} }{ {/required} }{ {/allParams} }{ {#hasOptionalParams} }localVarOptionals *{ {{classname} }}{ {{nickname} }}Opts{ {/hasOptionalParams} }) ({ {#returnType} }{ {{returnType} }}, { {/returnType} }*http.Response, error) {
42
+ var (
43
+ localVarHttpMethod = strings.ToUpper(" {{httpMethod}}" )
44
+ localVarPostBody interface{}
45
+ localVarFileName string
46
+ localVarFileBytes []byte
47
+ { {#returnType} }localVarReturnValue { {{returnType} }}{ {/returnType} }
48
+ )
49
+
50
+ // create path and map variables
51
+ localVarPath := a.client.cfg.BasePath + "{ {{path} }}"{ {#pathParams} }
52
+ localVarPath = strings.Replace(localVarPath, "{ " +" {{baseName} }"+"}", fmt.Sprintf("%v", { {paramName} }), -1){ {/pathParams} }
53
+
54
+ localVarHeaderParams := make(map[string]string)
55
+ localVarQueryParams := url.Values{ }
56
+ localVarFormParams := url.Values{ }
57
+ { {#allParams} }
58
+ { {#required} }
59
+ { {#minItems} }
60
+ if len({ {paramName} }) < { {minItems} } {
61
+ return {{#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } must have at least { {minItems} } elements")
62
+ }
63
+ { {/minItems} }
64
+ { {#maxItems} }
65
+ if len({ {paramName} }) > { {maxItems} } {
66
+ return {{#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } must have less than { {maxItems} } elements")
67
+ }
68
+ { {/maxItems} }
69
+ { {#minLength} }
70
+ if strlen({ {paramName} }) < { {minLength} } {
71
+ return {{#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } must have at least { {minLength} } elements")
72
+ }
73
+ { {/minLength} }
74
+ { {#maxLength} }
75
+ if strlen({ {paramName} }) > { {maxLength} } {
76
+ return {{#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } must have less than { {maxLength} } elements")
77
+ }
78
+ { {/maxLength} }
79
+ { {#minimum} }
80
+ { {#isString} }
81
+ { {paramName} }Txt, err := atoi({ {paramName} })
82
+ if { {paramName} }Txt < { {minimum} } {
83
+ {{/isString} }
84
+ { {^isString} }
85
+ if { {paramName} } < { {minimum} } {
86
+ {{/isString} }
87
+ return { {#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } must be greater than { {minimum} }")
88
+ }
89
+ { {/minimum} }
90
+ { {#maximum} }
91
+ { {#isString} }
92
+ { {paramName} }Txt, err := atoi({ {paramName} })
93
+ if { {paramName} }Txt > { {maximum} } {
94
+ {{/isString} }
95
+ { {^isString} }
96
+ if { {paramName} } > { {maximum} } {
97
+ {{/isString} }
98
+ return { {#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } must be less than { {maximum} }")
99
+ }
100
+ { {/maximum} }
101
+ { {/required} }
102
+ { {/allParams} }
103
+
104
+ { {#hasQueryParams} }
105
+ { {#queryParams} }
106
+ { {#required} }
107
+ localVarQueryParams.Add("{ {baseName} }", parameterToString({ {paramName} }, "{ {#collectionFormat} }{ {collectionFormat} }{ {/collectionFormat} }"))
108
+ { {/required} }
109
+ { {^required} }
110
+ if localVarOptionals != nil && localVarOptionals.{ {vendorExtensions.x-exportParamName} }.IsSet() {
111
+ localVarQueryParams.Add(" {{baseName}}" , parameterToString(localVarOptionals.{{vendorExtensions.x-exportParamName} }.Value(), "{ {#collectionFormat} }{ {collectionFormat} }{ {/collectionFormat} }"))
112
+ }
113
+ { {/required} }
114
+ { {/queryParams} }
115
+ { {/hasQueryParams} }
116
+ // to determine the Content-Type header
117
+ { {=<% %>= } }
118
+ localVarHttpContentTypes := []string{ <% #consumes%> " <%&mediaType%>" <% ^-last%> , <% /-last%><% /consumes%> }
119
+ <%={ { } }=%>
120
+
121
+ // set Content-Type header
122
+ localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
123
+ if localVarHttpContentType != "" {
124
+ localVarHeaderParams[" Content-Type" ] = localVarHttpContentType
125
+ }
126
+
127
+ // to determine the Accept header
128
+ { {=<% %>= } }
129
+ localVarHttpHeaderAccepts := []string{ <% #produces%> " <%&mediaType%>" <% ^-last%> , <% /-last%><% /produces%> }
130
+ <%={ { } }=%>
131
+
132
+ // set Accept header
133
+ localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
134
+ if localVarHttpHeaderAccept != "" {
135
+ localVarHeaderParams[" Accept" ] = localVarHttpHeaderAccept
136
+ }
137
+ { {#hasHeaderParams} }
138
+ { {#headerParams} }
139
+ { {#required} }
140
+ localVarHeaderParams["{ {baseName} }"] = parameterToString({ {paramName} }, "{ {#collectionFormat} }{ {collectionFormat} }{ {/collectionFormat} }")
141
+ { {/required} }
142
+ { {^required} }
143
+ if localVarOptionals != nil && localVarOptionals.{ {vendorExtensions.x-exportParamName} }.IsSet() {
144
+ localVarHeaderParams[" {{baseName}}" ] = parameterToString(localVarOptionals.{{vendorExtensions.x-exportParamName} }.Value(), "{ {#collectionFormat} }{ {collectionFormat} }{ {/collectionFormat} }")
145
+ }
146
+ { {/required} }
147
+ { {/headerParams} }
148
+ { {/hasHeaderParams} }
149
+ { {#hasFormParams} }
150
+ { {#formParams} }
151
+ { {#isFile} }
152
+ { {#required} }
153
+ localVarFile := { {paramName} }
154
+ { {/required} }
155
+ { {^required} }
156
+ var localVarFile { {dataType} }
157
+ if localVarOptionals != nil && localVarOptionals.{ {vendorExtensions.x-exportParamName} }.IsSet() {
158
+ localVarFileOk := false
159
+ localVarFile, localVarFileOk = localVarOptionals.{{vendorExtensions.x-exportParamName} }.Value().({ {dataType} })
160
+ if !localVarFileOk {
161
+ return {{#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } should be { {dataType} }")
162
+ }
163
+ }
164
+ { {/required} }
165
+ if localVarFile != nil {
166
+ fbs, _ := ioutil.ReadAll(localVarFile)
167
+ localVarFileBytes = fbs
168
+ localVarFileName = localVarFile.Name()
169
+ localVarFile.Close()
170
+ }
171
+ { {/isFile} }
172
+ { {^isFile} }
173
+ { {#required} }
174
+ localVarFormParams.Add("{ {baseName} }", parameterToString({ {paramName} }, "{ {#collectionFormat} }{ {collectionFormat} }{ {/collectionFormat} }"))
175
+ { {/required} }
176
+ { {^required} }
177
+ if localVarOptionals != nil && localVarOptionals.{ {vendorExtensions.x-exportParamName} }.IsSet() {
178
+ localVarFormParams.Add(" {{baseName}}" , parameterToString(localVarOptionals.{{vendorExtensions.x-exportParamName} }.Value(), "{ {#collectionFormat} }{ {collectionFormat} }{ {/collectionFormat} }"))
179
+ }
180
+ { {/required} }
181
+ { {/isFile} }
182
+ { {/formParams} }
183
+ { {/hasFormParams} }
184
+ { {#hasBodyParam} }
185
+ { {#bodyParams} } // body params
186
+ { {#required} }
187
+ localVarPostBody = & { {paramName} }
188
+ { {/required} }
189
+ { {^required} }
190
+ if localVarOptionals != nil && localVarOptionals.{ {vendorExtensions.x-exportParamName} }.IsSet() {
191
+ {{#isPrimitiveType} }localVarPostBody = &localVarOptionals.{ {vendorExtensions.x-exportParamName} }.Value(){ {/isPrimitiveType} }
192
+ { {^isPrimitiveType} }localVarOptional{ {vendorExtensions.x-exportParamName} }, localVarOptional{ {vendorExtensions.x-exportParamName} }ok := localVarOptionals.{ {vendorExtensions.x-exportParamName} }.Value().({ {{dataType} }})
193
+ if !localVarOptional{ {vendorExtensions.x-exportParamName} }ok {
194
+ return {{#returnType} }localVarReturnValue, { {/returnType} }nil, reportError("{ {paramName} } should be { {dataType} }")
195
+ }
196
+ localVarPostBody = &localVarOptional{ {vendorExtensions.x-exportParamName} }{ {/isPrimitiveType} }
197
+ }
198
+ { {/required} }
199
+ { {/bodyParams} }
200
+ { {/hasBodyParam} }
201
+ { {#authMethods} }
202
+ { {#isApiKey} }
203
+ if ctx != nil {
204
+ // API Key Authentication
205
+ if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
206
+ var key string
207
+ if auth.Prefix != " " {
208
+ key = auth.Prefix + " " + auth.Key
209
+ } else {
210
+ key = auth.Key
211
+ }
212
+ { {#isKeyInHeader} }localVarHeaderParams["{ {keyParamName} }"] = key{ {/isKeyInHeader} }
213
+ { {#isKeyInQuery} }localVarQueryParams.Add("{ {keyParamName} }", key){ {/isKeyInQuery} }
214
+ }
215
+ }
216
+ { {/isApiKey} }
217
+ { {/authMethods} }
218
+ r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
219
+ if err != nil {
220
+ return {{#returnType} }localVarReturnValue, { {/returnType} }nil, err
221
+ }
222
+
223
+ localVarHttpResponse, err := a.client.callAPI(r)
224
+ if err != nil || localVarHttpResponse == nil {
225
+ return {{#returnType} }localVarReturnValue, { {/returnType} }localVarHttpResponse, err
226
+ }
227
+
228
+ localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
229
+ localVarHttpResponse.Body.Close()
230
+ if err != nil {
231
+ return {{#returnType} }localVarReturnValue, { {/returnType} }localVarHttpResponse, err
232
+ }
233
+
234
+ { {#returnType} }
235
+ if localVarHttpResponse.StatusCode < 300 {
236
+ // If we succeed, return the data, otherwise pass on to decode error.
237
+ err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get(" Content-Type" ));
238
+ if err == nil {
239
+ return {{#returnType} }localVarReturnValue, { {/returnType} }localVarHttpResponse, err
240
+ }
241
+ }
242
+ { {/returnType} }
243
+
244
+ if localVarHttpResponse.StatusCode >= 300 {
245
+ newErr := GenericSwaggerError{
246
+ body: localVarBody,
247
+ error: localVarHttpResponse.Status,
248
+ }
249
+ { {#responses} }{ {#dataType} }
250
+ if localVarHttpResponse.StatusCode == { {{code} }} {
251
+ var v {{{dataType} }}
252
+ err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
253
+ if err != nil {
254
+ newErr.error = err.Error()
255
+ return {{#returnType} }localVarReturnValue, { {/returnType} }localVarHttpResponse, newErr
256
+ }
257
+ newErr.model = v
258
+ return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHttpResponse, newErr
259
+ }
260
+ { {/dataType} }{ {/responses} }
261
+ return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHttpResponse, newErr
262
+ }
263
+
264
+ return { {#returnType} }localVarReturnValue, { {/returnType} }localVarHttpResponse, nil
265
+ }
266
+ { {/operation} }{ {/operations} }
0 commit comments