forked from loafle/openapi-generator-original
* Added support for application/xml content-type for GO language Issue #7463 * Added test scripts for Go lang "withXml" feature * Added samples for Go land "withXml" feature. * "withXml" feature for Go language is only available for client.
236 lines
8.3 KiB
Plaintext
236 lines
8.3 KiB
Plaintext
{{>partial_header}}
|
|
package {{packageName}}
|
|
|
|
{{#operations}}
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"golang.org/x/net/context"
|
|
{{#imports}} "{{import}}"
|
|
{{/imports}}
|
|
)
|
|
|
|
// Linger please
|
|
var (
|
|
_ context.Context
|
|
)
|
|
|
|
type {{classname}}Service service
|
|
{{#operation}}
|
|
|
|
/* {{{classname}}}Service{{#summary}} {{.}}{{/summary}}{{#notes}}
|
|
{{notes}}{{/notes}}
|
|
* @param ctx context.Context for authentication, logging, tracing, etc.
|
|
{{#allParams}}{{#required}}@param {{paramName}}{{#description}} {{.}}{{/description}}
|
|
{{/required}}{{/allParams}}{{#hasOptionalParams}}@param optional (nil or map[string]interface{}) with one or more of:
|
|
{{#allParams}}{{^required}} @param "{{paramName}}" ({{dataType}}){{#description}} {{.}}{{/description}}
|
|
{{/required}}{{/allParams}}{{/hasOptionalParams}}@return {{#returnType}}{{{returnType}}}{{/returnType}}*/
|
|
func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals map[string]interface{}{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*http.Response, error) {
|
|
var (
|
|
localVarHttpMethod = strings.ToUpper("{{httpMethod}}")
|
|
localVarPostBody interface{}
|
|
localVarFileName string
|
|
localVarFileBytes []byte
|
|
{{#returnType}}
|
|
successPayload {{returnType}}
|
|
{{/returnType}}
|
|
)
|
|
|
|
// create path and map variables
|
|
localVarPath := a.client.cfg.BasePath + "{{{path}}}"{{#pathParams}}
|
|
localVarPath = strings.Replace(localVarPath, "{"+"{{baseName}}"+"}", fmt.Sprintf("%v", {{paramName}}), -1){{/pathParams}}
|
|
|
|
localVarHeaderParams := make(map[string]string)
|
|
localVarQueryParams := url.Values{}
|
|
localVarFormParams := url.Values{}
|
|
{{#allParams}}
|
|
{{^required}}
|
|
{{#isPrimitiveType}}
|
|
if err := typeCheckParameter(localVarOptionals["{{paramName}}"], "{{{dataType}}}", "{{paramName}}"); err != nil {
|
|
return {{#returnType}}successPayload, {{/returnType}}nil, err
|
|
}
|
|
{{/isPrimitiveType}}
|
|
{{/required}}
|
|
{{#required}}
|
|
{{#minItems}}
|
|
if len({{paramName}}) < {{minItems}} {
|
|
return {{#returnType}}successPayload, {{/returnType}}nil, reportError("{{paramName}} must have at least {{minItems}} elements")
|
|
}
|
|
{{/minItems}}
|
|
{{#maxItems}}
|
|
if len({{paramName}}) > {{maxItems}} {
|
|
return {{#returnType}}successPayload, {{/returnType}}nil, reportError("{{paramName}} must have less than {{maxItems}} elements")
|
|
}
|
|
{{/maxItems}}
|
|
{{#minLength}}
|
|
if strlen({{paramName}}) < {{minLength}} {
|
|
return {{#returnType}}successPayload, {{/returnType}}nil, reportError("{{paramName}} must have at least {{minLength}} elements")
|
|
}
|
|
{{/minLength}}
|
|
{{#maxLength}}
|
|
if strlen({{paramName}}) > {{maxLength}} {
|
|
return {{#returnType}}successPayload, {{/returnType}}nil, reportError("{{paramName}} must have less than {{maxLength}} elements")
|
|
}
|
|
{{/maxLength}}
|
|
{{#minimum}}
|
|
{{#isString}}
|
|
{{paramName}}Txt, err := atoi({{paramName}})
|
|
if {{paramName}}Txt < {{minimum}} {
|
|
{{/isString}}
|
|
{{^isString}}
|
|
if {{paramName}} < {{minimum}} {
|
|
{{/isString}}
|
|
return {{#returnType}}successPayload, {{/returnType}}nil, reportError("{{paramName}} must be greater than {{minimum}}")
|
|
}
|
|
{{/minimum}}
|
|
{{#maximum}}
|
|
{{#isString}}
|
|
{{paramName}}Txt, err := atoi({{paramName}})
|
|
if {{paramName}}Txt > {{maximum}} {
|
|
{{/isString}}
|
|
{{^isString}}
|
|
if {{paramName}} > {{maximum}} {
|
|
{{/isString}}
|
|
return {{#returnType}}successPayload, {{/returnType}}nil, reportError("{{paramName}} must be less than {{maximum}}")
|
|
}
|
|
{{/maximum}}
|
|
{{/required}}
|
|
{{/allParams}}
|
|
|
|
{{#hasQueryParams}}
|
|
{{#queryParams}}
|
|
{{#required}}
|
|
localVarQueryParams.Add("{{baseName}}", parameterToString({{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
|
{{/required}}
|
|
{{^required}}
|
|
if localVarTempParam, localVarOk := localVarOptionals["{{paramName}}"].({{dataType}}); localVarOk {
|
|
localVarQueryParams.Add("{{baseName}}", parameterToString(localVarTempParam, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
|
}
|
|
{{/required}}
|
|
{{/queryParams}}
|
|
{{/hasQueryParams}}
|
|
// to determine the Content-Type header
|
|
{{=<% %>=}}
|
|
localVarHttpContentTypes := []string{<%#consumes%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/consumes%>}
|
|
<%={{ }}=%>
|
|
|
|
// set Content-Type header
|
|
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
|
|
if localVarHttpContentType != "" {
|
|
localVarHeaderParams["Content-Type"] = localVarHttpContentType
|
|
}
|
|
|
|
// to determine the Accept header
|
|
{{=<% %>=}}
|
|
localVarHttpHeaderAccepts := []string{<%#produces%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/produces%>}
|
|
<%={{ }}=%>
|
|
|
|
// set Accept header
|
|
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
|
|
if localVarHttpHeaderAccept != "" {
|
|
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
|
|
}
|
|
{{#hasHeaderParams}}
|
|
{{#headerParams}}
|
|
{{#required}}
|
|
localVarHeaderParams["{{baseName}}"] = parameterToString({{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")
|
|
{{/required}}
|
|
{{^required}}
|
|
if localVarTempParam, localVarOk := localVarOptionals["{{paramName}}"].({{dataType}}); localVarOk {
|
|
localVarHeaderParams["{{baseName}}"] = parameterToString(localVarTempParam, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")
|
|
}
|
|
{{/required}}
|
|
{{/headerParams}}
|
|
{{/hasHeaderParams}}
|
|
{{#hasFormParams}}
|
|
{{#formParams}}
|
|
{{#isFile}}
|
|
{{^required}}
|
|
var localVarFile ({{dataType}})
|
|
if localVarTempParam, localVarOk := localVarOptionals["{{paramName}}"].({{dataType}}); localVarOk {
|
|
localVarFile = localVarTempParam
|
|
}
|
|
{{/required}}
|
|
if localVarFile != nil {
|
|
fbs, _ := ioutil.ReadAll(localVarFile)
|
|
localVarFileBytes = fbs
|
|
localVarFileName = localVarFile.Name()
|
|
localVarFile.Close()
|
|
}
|
|
{{/isFile}}
|
|
{{^isFile}}
|
|
{{#required}}
|
|
localVarFormParams.Add("{{baseName}}", parameterToString({{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
|
{{/required}}
|
|
{{^required}}
|
|
if localVarTempParam, localVarOk := localVarOptionals["{{paramName}}"].({{dataType}}); localVarOk {
|
|
localVarFormParams.Add("{{baseName}}", parameterToString(localVarTempParam, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
|
}
|
|
{{/required}}
|
|
{{/isFile}}
|
|
{{/formParams}}
|
|
{{/hasFormParams}}
|
|
{{#hasBodyParam}}
|
|
{{#bodyParams}} // body params
|
|
{{#required}}
|
|
localVarPostBody = &{{paramName}}
|
|
{{/required}}
|
|
{{^required}}
|
|
if localVarTempParam, localVarOk := localVarOptionals["{{paramName}}"].({{dataType}}); localVarOk {
|
|
localVarPostBody = &localVarTempParam
|
|
}
|
|
{{/required}}
|
|
{{/bodyParams}}
|
|
{{/hasBodyParam}}
|
|
{{#authMethods}}
|
|
{{#isApiKey}}
|
|
if ctx != nil {
|
|
// API Key Authentication
|
|
if auth, ok := ctx.Value(ContextAPIKey).(APIKey); ok {
|
|
var key string
|
|
if auth.Prefix != "" {
|
|
key = auth.Prefix + " " + auth.Key
|
|
} else {
|
|
key = auth.Key
|
|
}
|
|
{{#isKeyInHeader}}localVarHeaderParams["{{keyParamName}}"] = key{{/isKeyInHeader}}{{#isKeyInQuery}}localVarQueryParams.Add("{{keyParamName}}", key){{/isKeyInQuery}}
|
|
}
|
|
}
|
|
{{/isApiKey}}
|
|
{{/authMethods}}
|
|
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
|
|
if err != nil {
|
|
return {{#returnType}}successPayload, {{/returnType}}nil, err
|
|
}
|
|
|
|
localVarHttpResponse, err := a.client.callAPI(r)
|
|
if err != nil || localVarHttpResponse == nil {
|
|
return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, err
|
|
}
|
|
defer localVarHttpResponse.Body.Close()
|
|
if localVarHttpResponse.StatusCode >= 300 {
|
|
bodyBytes, _ := ioutil.ReadAll(localVarHttpResponse.Body)
|
|
return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, reportError("Status: %v, Body: %s", localVarHttpResponse.Status, bodyBytes)
|
|
}
|
|
{{#returnType}}
|
|
{{#withXml}}
|
|
contentType := localVarHttpResponse.Header.Get("content-type")
|
|
if strings.Contains(contentType, "application/xml") {
|
|
if err = xml.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil {
|
|
return successPayload, localVarHttpResponse, err
|
|
}
|
|
|
|
return successPayload, localVarHttpResponse, err
|
|
}
|
|
{{/withXml}}
|
|
|
|
if err = json.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil {
|
|
return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, err
|
|
}
|
|
|
|
{{/returnType}}
|
|
return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, err
|
|
}
|
|
{{/operation}}{{/operations}} |