Skip to content

Commit 100fd2b

Browse files
committed
added go templates
1 parent 5a12700 commit 100fd2b

26 files changed

+2264
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: go
2+
3+
install:
4+
- go get -d -v .
5+
6+
script:
7+
- go build -v ./
8+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Go API client for {{packageName}}
2+
3+
{{#appDescription}}
4+
{{{appDescription}}}
5+
{{/appDescription}}
6+
7+
## Overview
8+
This API client was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the [swagger-spec](https://github.com/swagger-api/swagger-spec) from a remote server, you can easily generate an API client.
9+
10+
- API version: {{appVersion}}
11+
- Package version: {{packageVersion}}
12+
{{^hideGenerationTimestamp}}
13+
- Build date: {{generatedDate}}
14+
{{/hideGenerationTimestamp}}
15+
- Build package: {{generatorClass}}
16+
{{#infoUrl}}
17+
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
18+
{{/infoUrl}}
19+
20+
## Installation
21+
Put the package under your project folder and add the following in import:
22+
```golang
23+
import "./{{packageName}}"
24+
```
25+
26+
## Documentation for API Endpoints
27+
28+
All URIs are relative to *{{basePath}}*
29+
30+
Class | Method | HTTP request | Description
31+
------------ | ------------- | ------------- | -------------
32+
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | [**{{operationId}}**]({{apiDocPath}}{{classname}}.md#{{operationIdLowerCase}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
33+
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
34+
35+
## Documentation For Models
36+
37+
{{#models}}{{#model}} - [{{{classname}}}]({{modelDocPath}}{{{classname}}}.md)
38+
{{/model}}{{/models}}
39+
40+
## Documentation For Authorization
41+
{{^authMethods}} Endpoints do not require authorization.
42+
{{/authMethods}}{{#authMethods}}{{#last}} Authentication schemes defined for the API:{{/last}}{{/authMethods}}
43+
{{#authMethods}}
44+
## {{{name}}}
45+
{{#isApiKey}}- **Type**: API key
46+
47+
Example
48+
```golang
49+
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
50+
Key: "APIKEY",
51+
Prefix: "Bearer", // Omit if not necessary.
52+
})
53+
r, err := client.Service.Operation(auth, args)
54+
```
55+
{{/isApiKey}}
56+
{{#isBasic}}- **Type**: HTTP basic authentication
57+
58+
Example
59+
```golang
60+
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
61+
UserName: "username",
62+
Password: "password",
63+
})
64+
r, err := client.Service.Operation(auth, args)
65+
```
66+
{{/isBasic}}
67+
{{#isOAuth}}- **Type**: OAuth
68+
- **Flow**: {{{flow}}}
69+
- **Authorization URL**: {{{authorizationUrl}}}
70+
- **Scopes**: {{^scopes}}N/A{{/scopes}}
71+
{{#scopes}} - **{{{scope}}}**: {{{description}}}
72+
{{/scopes}}
73+
74+
Example
75+
```golang
76+
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
77+
r, err := client.Service.Operation(auth, args)
78+
```
79+
80+
Or via OAuth2 module to automatically refresh tokens and perform user authentication.
81+
```golang
82+
import "golang.org/x/oauth2"
83+
84+
/* Perform OAuth2 round trip request and obtain a token */
85+
86+
tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
87+
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
88+
r, err := client.Service.Operation(auth, args)
89+
```
90+
{{/isOAuth}}
91+
{{/authMethods}}
92+
93+
## Author
94+
95+
{{#apiInfo}}{{#apis}}{{^hasMore}}{{infoEmail}}
96+
{{/hasMore}}{{/apis}}{{/apiInfo}}
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
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

Comments
 (0)