From 70d8041cb4a54ac8e7c8f25793d8cd405f94a1b4 Mon Sep 17 00:00:00 2001 From: Neil O'Toole Date: Wed, 3 Aug 2016 20:42:21 -0600 Subject: [PATCH] fixed config to support transport, and other options --- .../src/main/resources/go/api.mustache | 6 +-- .../src/main/resources/go/api_client.mustache | 41 ++++++++++++++----- .../main/resources/go/configuration.mustache | 26 ++++++------ 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index db14fa0f15..0863452af8 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -11,13 +11,13 @@ import ( ) type {{classname}} struct { - Configuration Configuration + Configuration *Configuration } func New{{classname}}() *{{classname}} { configuration := NewConfiguration() return &{{classname}}{ - Configuration: *configuration, + Configuration: configuration, } } @@ -26,7 +26,7 @@ func New{{classname}}WithBasePath(basePath string) *{{classname}} { configuration.BasePath = basePath return &{{classname}}{ - Configuration: *configuration, + Configuration: configuration, } } {{#operation}} diff --git a/modules/swagger-codegen/src/main/resources/go/api_client.mustache b/modules/swagger-codegen/src/main/resources/go/api_client.mustache index e11f4eaba8..c1f0841bff 100644 --- a/modules/swagger-codegen/src/main/resources/go/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api_client.mustache @@ -12,6 +12,7 @@ import ( ) type APIClient struct { + config *Configuration } func (c *APIClient) SelectHeaderContentType(contentTypes []string) string { @@ -36,9 +37,9 @@ func (c *APIClient) SelectHeaderAccept(accepts []string) string { return strings.Join(accepts, ",") } -func contains(source []string, containvalue string) bool { - for _, a := range source { - if strings.ToLower(a) == strings.ToLower(containvalue) { +func contains(haystack []string, needle string) bool { + for _, a := range haystack { + if strings.ToLower(a) == strings.ToLower(needle) { return true } } @@ -53,11 +54,8 @@ func (c *APIClient) CallAPI(path string, method string, fileName string, fileBytes []byte) (*resty.Response, error) { - //set debug flag - configuration := NewConfiguration() - resty.SetDebug(configuration.GetDebug()) - - request := prepareRequest(postBody, headerParams, queryParams, formParams, fileName, fileBytes) + rClient := c.prepareClient() + request := c.prepareRequest(rClient, postBody, headerParams, queryParams, formParams, fileName, fileBytes) switch strings.ToUpper(method) { case "GET": @@ -97,16 +95,39 @@ func (c *APIClient) ParameterToString(obj interface{},collectionFormat string) s return fmt.Sprintf("%v", obj) } -func prepareRequest(postBody interface{}, +func (c *APIClient) prepareClient() *resty.Client { + + rClient := resty.New() + + rClient.SetDebug(c.config.Debug) + if c.config.Transport != nil { + rClient.SetTransport(c.config.Transport) + } + + if c.config.Timeout != nil { + rClient.SetTimeout(*c.config.Timeout) + } + + return rClient +} + +func (c *APIClient) prepareRequest( + rClient *resty.Client, + postBody interface{}, headerParams map[string]string, queryParams url.Values, formParams map[string]string, fileName string, fileBytes []byte) *resty.Request { - request := resty.R() + + request := rClient.R() request.SetBody(postBody) + if c.config.UserAgent != "" { + request.SetHeader("User-Agent", c.config.UserAgent) + } + // add header parameter, if any if len(headerParams) > 0 { request.SetHeaders(headerParams) diff --git a/modules/swagger-codegen/src/main/resources/go/configuration.mustache b/modules/swagger-codegen/src/main/resources/go/configuration.mustache index d8d2260b2e..6f56b2138f 100644 --- a/modules/swagger-codegen/src/main/resources/go/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/go/configuration.mustache @@ -3,36 +3,42 @@ package {{packageName}} import ( "encoding/base64" + "net/http" + "time" ) + type Configuration struct { UserName string `json:"userName,omitempty"` Password string `json:"password,omitempty"` APIKeyPrefix map[string]string `json:"APIKeyPrefix,omitempty"` APIKey map[string]string `json:"APIKey,omitempty"` - debug bool `json:"debug,omitempty"` + Debug bool `json:"debug,omitempty"` DebugFile string `json:"debugFile,omitempty"` OAuthToken string `json:"oAuthToken,omitempty"` - Timeout int `json:"timeout,omitempty"` BasePath string `json:"basePath,omitempty"` Host string `json:"host,omitempty"` Scheme string `json:"scheme,omitempty"` AccessToken string `json:"accessToken,omitempty"` DefaultHeader map[string]string `json:"defaultHeader,omitempty"` UserAgent string `json:"userAgent,omitempty"` - APIClient APIClient `json:"APIClient,omitempty"` + APIClient *APIClient + Transport *http.Transport + Timeout *time.Duration `json:"timeout,omitempty"` } func NewConfiguration() *Configuration { - return &Configuration{ + cfg := &Configuration{ BasePath: "{{{basePath}}}", - UserName: "", - debug: false, DefaultHeader: make(map[string]string), APIKey: make(map[string]string), APIKeyPrefix: make(map[string]string), UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/go{{/httpUserAgent}}", + APIClient: &APIClient{}, } + + cfg.APIClient.config = cfg + return cfg } func (c *Configuration) GetBasicAuthEncodedString() string { @@ -50,11 +56,3 @@ func (c *Configuration) GetAPIKeyWithPrefix(APIKeyIdentifier string) string { return c.APIKey[APIKeyIdentifier] } - -func (c *Configuration) SetDebug(enable bool) { - c.debug = enable -} - -func (c *Configuration) GetDebug() bool { - return c.debug -}