diff --git a/api.mustache b/api.mustache index a7bb7a7f876..1aa817ca8d1 100644 --- a/api.mustache +++ b/api.mustache @@ -7,7 +7,7 @@ import ( "net/http" "net/url" "strings" - "golang.org/x/net/context" + "context" {{#imports}} "{{import}}" {{/imports}} ) @@ -20,22 +20,30 @@ var ( type {{classname}}Service service {{#operation}} -/* {{{classname}}}Service{{#summary}} {{.}}{{/summary}}{{#notes}} +/* +{{{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) { + * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). +{{#allParams}}{{#required}} * @param {{paramName}}{{#description}} {{.}}{{/description}} +{{/required}}{{/allParams}}{{#hasOptionalParams}} * @param optional nil or *{{{nickname}}}Opts - Optional Parameters: +{{#allParams}}{{^required}} * @param "{{vendorExtensions.x-exportParamName}}" ({{#isPrimitiveType}}optional.{{vendorExtensions.x-optionalDataType}}{{/isPrimitiveType}}{{^isPrimitiveType}}optional.Interface of {{dataType}}{{/isPrimitiveType}}) - {{#description}} {{.}}{{/description}} +{{/required}}{{/allParams}}{{/hasOptionalParams}} +{{#returnType}}@return {{{returnType}}}{{/returnType}} +*/ +{{#hasOptionalParams}} + +type {{{nickname}}}Opts struct { {{#allParams}}{{^required}} +{{#isPrimitiveType}} {{vendorExtensions.x-exportParamName}} optional.{{vendorExtensions.x-optionalDataType}}{{/isPrimitiveType}}{{^isPrimitiveType}} {{vendorExtensions.x-exportParamName}} optional.Interface{{/isPrimitiveType}}{{/required}}{{/allParams}} +} + +{{/hasOptionalParams}} +func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*http.Response, error) { var ( localVarHttpMethod = strings.ToUpper("{{httpMethod}}") localVarPostBody interface{} localVarFileName string localVarFileBytes []byte -{{#returnType}} - successPayload {{returnType}} -{{/returnType}} + {{#returnType}}localVarReturnValue {{{returnType}}}{{/returnType}} ) // create path and map variables @@ -46,32 +54,25 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} 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") + return {{#returnType}}localVarReturnValue, {{/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") + return {{#returnType}}localVarReturnValue, {{/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") + return {{#returnType}}localVarReturnValue, {{/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") + return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have less than {{maxLength}} elements") } {{/maxLength}} {{#minimum}} @@ -82,7 +83,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} {{^isString}} if {{paramName}} < {{minimum}} { {{/isString}} - return {{#returnType}}successPayload, {{/returnType}}nil, reportError("{{paramName}} must be greater than {{minimum}}") + return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must be greater than {{minimum}}") } {{/minimum}} {{#maximum}} @@ -93,7 +94,7 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} {{^isString}} if {{paramName}} > {{maximum}} { {{/isString}} - return {{#returnType}}successPayload, {{/returnType}}nil, reportError("{{paramName}} must be less than {{maximum}}") + return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must be less than {{maximum}}") } {{/maximum}} {{/required}} @@ -105,8 +106,8 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} 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}}")) + if localVarOptionals != nil && localVarOptionals.{{vendorExtensions.x-exportParamName}}.IsSet() { + localVarQueryParams.Add("{{baseName}}", parameterToString(localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value(), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")) } {{/required}} {{/queryParams}} @@ -138,8 +139,8 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} localVarHeaderParams["{{baseName}}"] = parameterToString({{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}") {{/required}} {{^required}} - if localVarTempParam, localVarOk := localVarOptionals["{{paramName}}"].({{dataType}}); localVarOk { - localVarHeaderParams["{{baseName}}"] = parameterToString(localVarTempParam, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}") + if localVarOptionals != nil && localVarOptionals.{{vendorExtensions.x-exportParamName}}.IsSet() { + localVarHeaderParams["{{baseName}}"] = parameterToString(localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value(), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}") } {{/required}} {{/headerParams}} @@ -148,9 +149,13 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} {{#formParams}} {{#isFile}} {{^required}} - var localVarFile ({{dataType}}) - if localVarTempParam, localVarOk := localVarOptionals["{{paramName}}"].({{dataType}}); localVarOk { - localVarFile = localVarTempParam + var localVarFile {{dataType}} + if localVarOptionals != nil && localVarOptionals.{{vendorExtensions.x-exportParamName}}.IsSet() { + localVarFileOk := false + localVarFile, localVarFileOk = localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value().({{dataType}}) + if !localVarFileOk { + return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} should be {{dataType}}") + } } {{/required}} if localVarFile != nil { @@ -165,8 +170,8 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} 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}}")) + if localVarOptionals != nil && localVarOptionals.{{vendorExtensions.x-exportParamName}}.IsSet() { + localVarFormParams.Add("{{baseName}}", parameterToString(localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value(), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")) } {{/required}} {{/isFile}} @@ -178,8 +183,13 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} localVarPostBody = &{{paramName}} {{/required}} {{^required}} - if localVarTempParam, localVarOk := localVarOptionals["{{paramName}}"].({{dataType}}); localVarOk { - localVarPostBody = &localVarTempParam + if localVarOptionals != nil && localVarOptionals.{{vendorExtensions.x-exportParamName}}.IsSet() { + {{#isPrimitiveType}}localVarPostBody = &localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value(){{/isPrimitiveType}} + {{^isPrimitiveType}}localVarOptional{{vendorExtensions.x-exportParamName}}, localVarOptional{{vendorExtensions.x-exportParamName}}ok := localVarOptionals.{{vendorExtensions.x-exportParamName}}.Value().({{{dataType}}}) + if !localVarOptional{{vendorExtensions.x-exportParamName}}ok { + return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} should be {{dataType}}") + } + localVarPostBody = &localVarOptional{{vendorExtensions.x-exportParamName}}{{/isPrimitiveType}} } {{/required}} {{/bodyParams}} @@ -195,42 +205,63 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams} } else { key = auth.Key } - {{#isKeyInHeader}}localVarHeaderParams["{{keyParamName}}"] = key{{/isKeyInHeader}}{{#isKeyInQuery}}localVarQueryParams.Add("{{keyParamName}}", key){{/isKeyInQuery}} + {{#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 + return {{#returnType}}localVarReturnValue, {{/returnType}}nil, err } localVarHttpResponse, err := a.client.callAPI(r) if err != nil || localVarHttpResponse == nil { - return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, err + return {{#returnType}}localVarReturnValue, {{/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 + localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body) + localVarHttpResponse.Body.Close() + if err != nil { + return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err } - {{/withXml}} - if err = json.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil { - return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, err + {{#returnType}} + if localVarHttpResponse.StatusCode < 300 { + // If we succeed, return the data, otherwise pass on to decode error. + err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err == nil { + return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err + } + } + {{/returnType}} + + if localVarHttpResponse.StatusCode >= 300 { + newErr := GenericSwaggerError{ + body: localVarBody, + error: localVarHttpResponse.Status, + } + {{#responses}}{{#dataType}} + if localVarHttpResponse.StatusCode == {{{code}}} { + localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body) + if err != nil { + return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err + } + + var v {{{dataType}}} + err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type")); + if err != nil { + newErr.error = err.Error() + return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, newErr + } + newErr.model = v + return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, newErr + } + {{/dataType}}{{/responses}} + return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, newErr } -{{/returnType}} - return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, err + return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, nil } {{/operation}}{{/operations}} \ No newline at end of file diff --git a/api_doc.mustache b/api_doc.mustache index 635a91e3ed8..76e823ebb83 100644 --- a/api_doc.mustache +++ b/api_doc.mustache @@ -20,16 +20,16 @@ Method | HTTP request | Description {{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}} Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **ctx** | **context.Context** | context for logging, tracing, authentication, etc.{{/-last}}{{/allParams}}{{#allParams}}{{#required}} + **ctx** | **context.Context** | context for authentication, logging, cancellation, deadlines, tracing, etc.{{/-last}}{{/allParams}}{{#allParams}}{{#required}} **{{paramName}}** | {{#isFile}}**{{dataType}}**{{/isFile}}{{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/required}}{{/allParams}}{{#hasOptionalParams}} - **optional** | **map[string]interface{}** | optional parameters | nil if no parameters + **optional** | ***{{{nickname}}}Opts** | optional parameters | nil if no parameters ### Optional Parameters -Optional parameters are passed through a map[string]interface{}. +Optional parameters are passed through a pointer to a {{{nickname}}}Opts struct {{#allParams}}{{#-last}} Name | Type | Description | Notes ------------- | ------------- | ------------- | -------------{{/-last}}{{/allParams}}{{#allParams}} - **{{paramName}}** | {{#isFile}}**{{dataType}}**{{/isFile}}{{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/allParams}}{{/hasOptionalParams}} +{{^required}} **{{paramName}}** | {{#isFile}}**optional.Interface of {{dataType}}**{{/isFile}}{{#isPrimitiveType}}**optional.{{vendorExtensions.x-optionalDataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isFile}}[**optional.Interface of {{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/required}}{{/allParams}}{{/hasOptionalParams}} ### Return type diff --git a/client.mustache b/client.mustache index 38c7a7e2720..c2b2d9ea5e6 100644 --- a/client.mustache +++ b/client.mustache @@ -20,7 +20,7 @@ import ( "time" "unicode/utf8" - "golang.org/x/net/context" + "context" "golang.org/x/oauth2" ) @@ -303,6 +303,21 @@ func (c *APIClient) prepareRequest( return localVarRequest, nil } +func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) { + if strings.Contains(contentType, "application/xml") { + if err = xml.Unmarshal(b, v); err != nil { + return err + } + return nil + } else if strings.Contains(contentType, "application/json") { + if err = json.Unmarshal(b, v); err != nil { + return err + } + return nil + } + return errors.New("undefined response type") +} + // Add a file to the multipart request func addFile(w *multipart.Writer, fieldName, path string) error { file, err := os.Open(path) @@ -337,6 +352,8 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e _, err = bodyBuf.Write(b) } else if s, ok := body.(string); ok { _, err = bodyBuf.WriteString(s) + } else if s, ok := body.(*string); ok { + _, err = bodyBuf.WriteString(*s) } else if jsonCheck.MatchString(contentType) { err = json.NewEncoder(bodyBuf).Encode(body) } else if xmlCheck.MatchString(contentType) { @@ -427,3 +444,25 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } + +// GenericSwaggerError Provides access to the body, error and model on returned errors. +type GenericSwaggerError struct { + body []byte + error string + model interface{} +} + +// Error returns non-empty string if there was an error. +func (e GenericSwaggerError) Error() string { + return e.error +} + +// Body returns the raw bytes of the response +func (e GenericSwaggerError) Body() []byte { + return e.body +} + +// Model returns the unpacked model of the error +func (e GenericSwaggerError) Model() interface{} { + return e.model +} \ No newline at end of file diff --git a/model.mustache b/model.mustache index 65996a1a9fb..cc8c45cb27d 100644 --- a/model.mustache +++ b/model.mustache @@ -12,7 +12,6 @@ const ( {{#allowableValues}} {{#enumVars}} {{^-first}} - {{/-first}} {{name}} {{{classname}}} = "{{{value}}}" {{/enumVars}} @@ -22,7 +21,6 @@ const ( type {{classname}} struct { {{#vars}} {{^-first}} - {{/-first}} {{#description}} // {{{description}}}