/
api.mustache
136 lines (124 loc) · 5.12 KB
/
api.mustache
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
package {{packageName}}
{{#operations}}
import (
"strings"
"fmt"
"errors"
"net/url"
{{#imports}}"{{import}}"
{{/imports}}
)
type {{classname}} struct {
Configuration Configuration
}
func New{{classname}}() *{{classname}} {
configuration := NewConfiguration()
return &{{classname}}{
Configuration: *configuration,
}
}
func New{{classname}}WithBasePath(basePath string) *{{classname}} {
configuration := NewConfiguration()
configuration.BasePath = basePath
return &{{classname}}{
Configuration: *configuration,
}
}
{{#operation}}
/**
* {{summary}}{{#notes}}
* {{notes}}{{/notes}}
*
{{#allParams}} * @param {{paramName}} {{description}}
{{/allParams}} * @return {{#returnType}}{{^isListContainer}}*{{/isListContainer}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
*/
func (a {{classname}}) {{nickname}}({{#allParams}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) ({{#returnType}}{{^isListContainer}}*{{/isListContainer}}{{{returnType}}}, {{/returnType}}*APIResponse, error) {
var httpMethod = "{{httpMethod}}"
// create path and map variables
path := a.Configuration.BasePath + "{{path}}"{{#pathParams}}
path = strings.Replace(path, "{"+"{{baseName}}"+"}", fmt.Sprintf("%v", {{paramName}}), -1){{/pathParams}}
{{#allParams}}{{#required}}
// verify the required parameter '{{paramName}}' is set
if &{{paramName}} == nil {
return {{#returnType}}{{#isListContainer}}*{{/isListContainer}}new({{{returnType}}}), {{/returnType}}nil, errors.New("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}")
}{{/required}}{{/allParams}}
headerParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
var fileBytes []byte
{{#authMethods}}
// authentication ({{name}}) required
{{#isApiKey}}{{#isKeyInHeader}}
// set key with prefix in header
headerParams["{{keyParamName}}"] = a.Configuration.GetAPIKeyWithPrefix("{{keyParamName}}")
{{/isKeyInHeader}}{{#isKeyInQuery}}
// set key with prefix in querystring{{#hasKeyParamName}}
queryParams["{{keyParamName}}"] = a.Configuration.GetAPIKeyWithPrefix("{{keyParamName}}")
{{/hasKeyParamName}}{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}
// http basic authentication required
if a.Configuration.Username != "" || a.Configuration.Password != ""{
headerParams["Authorization"] = "Basic " + a.Configuration.GetBasicAuthEncodedString()
}{{/isBasic}}{{#isOAuth}}
// oauth required
if a.Configuration.AccessToken != ""{
headerParams["Authorization"] = "Bearer " + a.Configuration.AccessToken
}{{/isOAuth}}{{/authMethods}}
// add default headers if any
for key := range a.Configuration.DefaultHeader {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
{{#hasQueryParams}}
{{#queryParams}}
{{#isListContainer}}
var collectionFormat = "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"
if collectionFormat == "multi" {
for _, value := range {{paramName}} {
queryParams.Add("{{paramName}}", value)
}
} else {
queryParams.Add("{{paramName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, collectionFormat))
}
{{/isListContainer}}
{{^isListContainer}}
queryParams.Add("{{paramName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, ""))
{{/isListContainer}}
{{/queryParams}}{{/hasQueryParams}}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ {{#consumes}}"{{{mediaType}}}", {{/consumes}} }
// set Content-Type header
localVarHttpContentType := a.Configuration.APIClient.SelectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
headerParams["Content-Type"] = localVarHttpContentType
}
// to determine the Accept header
localVarHttpHeaderAccepts := []string{
{{#produces}}"{{{mediaType}}}",
{{/produces}} }
// set Accept header
localVarHttpHeaderAccept := a.Configuration.APIClient.SelectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
headerParams["Accept"] = localVarHttpHeaderAccept
}{{#hasHeaderParams}}
{{#headerParams}} // header params "{{baseName}}"
headerParams["{{baseName}}"] = {{paramName}}
{{/headerParams}}{{/hasHeaderParams}}{{#hasFormParams}}
{{#formParams}}{{#isFile}}
fbs, _ := ioutil.ReadAll(file)
fileBytes = fbs
fileName = file.Name(){{/isFile}}
{{^isFile}} formParams["{{paramName}}"] = {{paramName}}{{/isFile}}{{/formParams}}{{/hasFormParams}}{{#hasBodyParam}}
{{#bodyParams}} // body params
postBody = &{{paramName}}
{{/bodyParams}}{{/hasBodyParam}}
{{#returnType}} var successPayload = new({{returnType}}){{/returnType}}
httpResponse, err := a.Configuration.APIClient.CallAPI(path, httpMethod, postBody, headerParams, queryParams, formParams, fileName, fileBytes)
if err != nil {
return {{#returnType}}{{#isListContainer}}*{{/isListContainer}}successPayload, {{/returnType}}NewAPIResponse(httpResponse.RawResponse), err
}
{{#returnType}}
err = json.Unmarshal(httpResponse.Body(), &successPayload){{/returnType}}
return {{#returnType}}{{#isListContainer}}*{{/isListContainer}}successPayload, {{/returnType}}NewAPIResponse(httpResponse.RawResponse), err
}
{{/operation}}{{/operations}}