From 70d8041cb4a54ac8e7c8f25793d8cd405f94a1b4 Mon Sep 17 00:00:00 2001 From: Neil O'Toole Date: Wed, 3 Aug 2016 20:42:21 -0600 Subject: [PATCH 1/2] 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 db14fa0f157..0863452af82 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 e11f4eaba8b..c1f0841bffd 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 d8d2260b2e6..6f56b2138fb 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 -} From 535de68701265001e59fdfede7bede87ff215470 Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 17 Aug 2016 23:05:03 +0800 Subject: [PATCH 2/2] update go petstore sample --- .../client/petstore/go/go-petstore/README.md | 2 +- .../petstore/go/go-petstore/api_client.go | 41 ++++++++++++++----- .../petstore/go/go-petstore/configuration.go | 26 ++++++------ .../client/petstore/go/go-petstore/pet_api.go | 6 +-- .../petstore/go/go-petstore/store_api.go | 6 +-- .../petstore/go/go-petstore/user_api.go | 6 +-- 6 files changed, 53 insertions(+), 34 deletions(-) diff --git a/samples/client/petstore/go/go-petstore/README.md b/samples/client/petstore/go/go-petstore/README.md index 0b9b726cfef..2e20331eb94 100644 --- a/samples/client/petstore/go/go-petstore/README.md +++ b/samples/client/petstore/go/go-petstore/README.md @@ -7,7 +7,7 @@ This API client was generated by the [swagger-codegen](https://github.com/swagge - API version: 1.0.0 - Package version: 1.0.0 -- Build date: 2016-08-11T12:21:56.063+08:00 +- Build date: 2016-08-17T22:53:45.063+08:00 - Build package: class io.swagger.codegen.languages.GoClientCodegen ## Installation diff --git a/samples/client/petstore/go/go-petstore/api_client.go b/samples/client/petstore/go/go-petstore/api_client.go index 4665a3dadc7..5c66c5580e0 100644 --- a/samples/client/petstore/go/go-petstore/api_client.go +++ b/samples/client/petstore/go/go-petstore/api_client.go @@ -33,6 +33,7 @@ import ( ) type APIClient struct { + config *Configuration } func (c *APIClient) SelectHeaderContentType(contentTypes []string) string { @@ -57,9 +58,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 } } @@ -74,11 +75,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": @@ -118,16 +116,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/samples/client/petstore/go/go-petstore/configuration.go b/samples/client/petstore/go/go-petstore/configuration.go index 36437df500d..549d4b7127c 100644 --- a/samples/client/petstore/go/go-petstore/configuration.go +++ b/samples/client/petstore/go/go-petstore/configuration.go @@ -24,36 +24,42 @@ package petstore 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: "http://petstore.swagger.io/v2", - UserName: "", - debug: false, DefaultHeader: make(map[string]string), APIKey: make(map[string]string), APIKeyPrefix: make(map[string]string), UserAgent: "Swagger-Codegen/1.0.0/go", + APIClient: &APIClient{}, } + + cfg.APIClient.config = cfg + return cfg } func (c *Configuration) GetBasicAuthEncodedString() string { @@ -71,11 +77,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 -} diff --git a/samples/client/petstore/go/go-petstore/pet_api.go b/samples/client/petstore/go/go-petstore/pet_api.go index d22a514d811..04cc352d2bb 100644 --- a/samples/client/petstore/go/go-petstore/pet_api.go +++ b/samples/client/petstore/go/go-petstore/pet_api.go @@ -32,13 +32,13 @@ import ( ) type PetApi struct { - Configuration Configuration + Configuration *Configuration } func NewPetApi() *PetApi { configuration := NewConfiguration() return &PetApi{ - Configuration: *configuration, + Configuration: configuration, } } @@ -47,7 +47,7 @@ func NewPetApiWithBasePath(basePath string) *PetApi { configuration.BasePath = basePath return &PetApi{ - Configuration: *configuration, + Configuration: configuration, } } diff --git a/samples/client/petstore/go/go-petstore/store_api.go b/samples/client/petstore/go/go-petstore/store_api.go index 14906cc387a..898dc59d264 100644 --- a/samples/client/petstore/go/go-petstore/store_api.go +++ b/samples/client/petstore/go/go-petstore/store_api.go @@ -30,13 +30,13 @@ import ( ) type StoreApi struct { - Configuration Configuration + Configuration *Configuration } func NewStoreApi() *StoreApi { configuration := NewConfiguration() return &StoreApi{ - Configuration: *configuration, + Configuration: configuration, } } @@ -45,7 +45,7 @@ func NewStoreApiWithBasePath(basePath string) *StoreApi { configuration.BasePath = basePath return &StoreApi{ - Configuration: *configuration, + Configuration: configuration, } } diff --git a/samples/client/petstore/go/go-petstore/user_api.go b/samples/client/petstore/go/go-petstore/user_api.go index 0e66b14bba9..262312d986a 100644 --- a/samples/client/petstore/go/go-petstore/user_api.go +++ b/samples/client/petstore/go/go-petstore/user_api.go @@ -30,13 +30,13 @@ import ( ) type UserApi struct { - Configuration Configuration + Configuration *Configuration } func NewUserApi() *UserApi { configuration := NewConfiguration() return &UserApi{ - Configuration: *configuration, + Configuration: configuration, } } @@ -45,7 +45,7 @@ func NewUserApiWithBasePath(basePath string) *UserApi { configuration.BasePath = basePath return &UserApi{ - Configuration: *configuration, + Configuration: configuration, } }