Merge branch 'neilotoole-issue-3461-configurable-transport'

This commit is contained in:
wing328 2016-08-17 23:05:20 +08:00
commit 77d3131e7e
9 changed files with 99 additions and 61 deletions

View File

@ -9,13 +9,13 @@ import (
) )
type {{classname}} struct { type {{classname}} struct {
Configuration Configuration Configuration *Configuration
} }
func New{{classname}}() *{{classname}} { func New{{classname}}() *{{classname}} {
configuration := NewConfiguration() configuration := NewConfiguration()
return &{{classname}}{ return &{{classname}}{
Configuration: *configuration, Configuration: configuration,
} }
} }
@ -24,7 +24,7 @@ func New{{classname}}WithBasePath(basePath string) *{{classname}} {
configuration.BasePath = basePath configuration.BasePath = basePath
return &{{classname}}{ return &{{classname}}{
Configuration: *configuration, Configuration: configuration,
} }
} }
{{#operation}} {{#operation}}

View File

@ -12,6 +12,7 @@ import (
) )
type APIClient struct { type APIClient struct {
config *Configuration
} }
func (c *APIClient) SelectHeaderContentType(contentTypes []string) string { func (c *APIClient) SelectHeaderContentType(contentTypes []string) string {
@ -36,9 +37,9 @@ func (c *APIClient) SelectHeaderAccept(accepts []string) string {
return strings.Join(accepts, ",") return strings.Join(accepts, ",")
} }
func contains(source []string, containvalue string) bool { func contains(haystack []string, needle string) bool {
for _, a := range source { for _, a := range haystack {
if strings.ToLower(a) == strings.ToLower(containvalue) { if strings.ToLower(a) == strings.ToLower(needle) {
return true return true
} }
} }
@ -53,11 +54,8 @@ func (c *APIClient) CallAPI(path string, method string,
fileName string, fileName string,
fileBytes []byte) (*resty.Response, error) { fileBytes []byte) (*resty.Response, error) {
//set debug flag rClient := c.prepareClient()
configuration := NewConfiguration() request := c.prepareRequest(rClient, postBody, headerParams, queryParams, formParams, fileName, fileBytes)
resty.SetDebug(configuration.GetDebug())
request := prepareRequest(postBody, headerParams, queryParams, formParams, fileName, fileBytes)
switch strings.ToUpper(method) { switch strings.ToUpper(method) {
case "GET": case "GET":
@ -97,16 +95,39 @@ func (c *APIClient) ParameterToString(obj interface{},collectionFormat string) s
return fmt.Sprintf("%v", obj) 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, headerParams map[string]string,
queryParams url.Values, queryParams url.Values,
formParams map[string]string, formParams map[string]string,
fileName string, fileName string,
fileBytes []byte) *resty.Request { fileBytes []byte) *resty.Request {
request := resty.R()
request := rClient.R()
request.SetBody(postBody) request.SetBody(postBody)
if c.config.UserAgent != "" {
request.SetHeader("User-Agent", c.config.UserAgent)
}
// add header parameter, if any // add header parameter, if any
if len(headerParams) > 0 { if len(headerParams) > 0 {
request.SetHeaders(headerParams) request.SetHeaders(headerParams)

View File

@ -3,36 +3,42 @@ package {{packageName}}
import ( import (
"encoding/base64" "encoding/base64"
"net/http"
"time"
) )
type Configuration struct { type Configuration struct {
UserName string `json:"userName,omitempty"` UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
APIKeyPrefix map[string]string `json:"APIKeyPrefix,omitempty"` APIKeyPrefix map[string]string `json:"APIKeyPrefix,omitempty"`
APIKey map[string]string `json:"APIKey,omitempty"` APIKey map[string]string `json:"APIKey,omitempty"`
debug bool `json:"debug,omitempty"` Debug bool `json:"debug,omitempty"`
DebugFile string `json:"debugFile,omitempty"` DebugFile string `json:"debugFile,omitempty"`
OAuthToken string `json:"oAuthToken,omitempty"` OAuthToken string `json:"oAuthToken,omitempty"`
Timeout int `json:"timeout,omitempty"`
BasePath string `json:"basePath,omitempty"` BasePath string `json:"basePath,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"` Scheme string `json:"scheme,omitempty"`
AccessToken string `json:"accessToken,omitempty"` AccessToken string `json:"accessToken,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"` DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,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 { func NewConfiguration() *Configuration {
return &Configuration{ cfg := &Configuration{
BasePath: "{{{basePath}}}", BasePath: "{{{basePath}}}",
UserName: "",
debug: false,
DefaultHeader: make(map[string]string), DefaultHeader: make(map[string]string),
APIKey: make(map[string]string), APIKey: make(map[string]string),
APIKeyPrefix: make(map[string]string), APIKeyPrefix: make(map[string]string),
UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/go{{/httpUserAgent}}", UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/go{{/httpUserAgent}}",
APIClient: &APIClient{},
} }
cfg.APIClient.config = cfg
return cfg
} }
func (c *Configuration) GetBasicAuthEncodedString() string { func (c *Configuration) GetBasicAuthEncodedString() string {
@ -50,11 +56,3 @@ func (c *Configuration) GetAPIKeyWithPrefix(APIKeyIdentifier string) string {
return c.APIKey[APIKeyIdentifier] return c.APIKey[APIKeyIdentifier]
} }
func (c *Configuration) SetDebug(enable bool) {
c.debug = enable
}
func (c *Configuration) GetDebug() bool {
return c.debug
}

View File

@ -7,7 +7,7 @@ This API client was generated by the [swagger-codegen](https://github.com/swagge
- API version: 1.0.0 - API version: 1.0.0
- Package 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 - Build package: class io.swagger.codegen.languages.GoClientCodegen
## Installation ## Installation

View File

@ -33,6 +33,7 @@ import (
) )
type APIClient struct { type APIClient struct {
config *Configuration
} }
func (c *APIClient) SelectHeaderContentType(contentTypes []string) string { func (c *APIClient) SelectHeaderContentType(contentTypes []string) string {
@ -57,9 +58,9 @@ func (c *APIClient) SelectHeaderAccept(accepts []string) string {
return strings.Join(accepts, ",") return strings.Join(accepts, ",")
} }
func contains(source []string, containvalue string) bool { func contains(haystack []string, needle string) bool {
for _, a := range source { for _, a := range haystack {
if strings.ToLower(a) == strings.ToLower(containvalue) { if strings.ToLower(a) == strings.ToLower(needle) {
return true return true
} }
} }
@ -74,11 +75,8 @@ func (c *APIClient) CallAPI(path string, method string,
fileName string, fileName string,
fileBytes []byte) (*resty.Response, error) { fileBytes []byte) (*resty.Response, error) {
//set debug flag rClient := c.prepareClient()
configuration := NewConfiguration() request := c.prepareRequest(rClient, postBody, headerParams, queryParams, formParams, fileName, fileBytes)
resty.SetDebug(configuration.GetDebug())
request := prepareRequest(postBody, headerParams, queryParams, formParams, fileName, fileBytes)
switch strings.ToUpper(method) { switch strings.ToUpper(method) {
case "GET": case "GET":
@ -118,16 +116,39 @@ func (c *APIClient) ParameterToString(obj interface{},collectionFormat string) s
return fmt.Sprintf("%v", obj) 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, headerParams map[string]string,
queryParams url.Values, queryParams url.Values,
formParams map[string]string, formParams map[string]string,
fileName string, fileName string,
fileBytes []byte) *resty.Request { fileBytes []byte) *resty.Request {
request := resty.R()
request := rClient.R()
request.SetBody(postBody) request.SetBody(postBody)
if c.config.UserAgent != "" {
request.SetHeader("User-Agent", c.config.UserAgent)
}
// add header parameter, if any // add header parameter, if any
if len(headerParams) > 0 { if len(headerParams) > 0 {
request.SetHeaders(headerParams) request.SetHeaders(headerParams)

View File

@ -24,36 +24,42 @@ package petstore
import ( import (
"encoding/base64" "encoding/base64"
"net/http"
"time"
) )
type Configuration struct { type Configuration struct {
UserName string `json:"userName,omitempty"` UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
APIKeyPrefix map[string]string `json:"APIKeyPrefix,omitempty"` APIKeyPrefix map[string]string `json:"APIKeyPrefix,omitempty"`
APIKey map[string]string `json:"APIKey,omitempty"` APIKey map[string]string `json:"APIKey,omitempty"`
debug bool `json:"debug,omitempty"` Debug bool `json:"debug,omitempty"`
DebugFile string `json:"debugFile,omitempty"` DebugFile string `json:"debugFile,omitempty"`
OAuthToken string `json:"oAuthToken,omitempty"` OAuthToken string `json:"oAuthToken,omitempty"`
Timeout int `json:"timeout,omitempty"`
BasePath string `json:"basePath,omitempty"` BasePath string `json:"basePath,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"` Scheme string `json:"scheme,omitempty"`
AccessToken string `json:"accessToken,omitempty"` AccessToken string `json:"accessToken,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"` DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,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 { func NewConfiguration() *Configuration {
return &Configuration{ cfg := &Configuration{
BasePath: "http://petstore.swagger.io/v2", BasePath: "http://petstore.swagger.io/v2",
UserName: "",
debug: false,
DefaultHeader: make(map[string]string), DefaultHeader: make(map[string]string),
APIKey: make(map[string]string), APIKey: make(map[string]string),
APIKeyPrefix: make(map[string]string), APIKeyPrefix: make(map[string]string),
UserAgent: "Swagger-Codegen/1.0.0/go", UserAgent: "Swagger-Codegen/1.0.0/go",
APIClient: &APIClient{},
} }
cfg.APIClient.config = cfg
return cfg
} }
func (c *Configuration) GetBasicAuthEncodedString() string { func (c *Configuration) GetBasicAuthEncodedString() string {
@ -71,11 +77,3 @@ func (c *Configuration) GetAPIKeyWithPrefix(APIKeyIdentifier string) string {
return c.APIKey[APIKeyIdentifier] return c.APIKey[APIKeyIdentifier]
} }
func (c *Configuration) SetDebug(enable bool) {
c.debug = enable
}
func (c *Configuration) GetDebug() bool {
return c.debug
}

View File

@ -32,13 +32,13 @@ import (
) )
type PetApi struct { type PetApi struct {
Configuration Configuration Configuration *Configuration
} }
func NewPetApi() *PetApi { func NewPetApi() *PetApi {
configuration := NewConfiguration() configuration := NewConfiguration()
return &PetApi{ return &PetApi{
Configuration: *configuration, Configuration: configuration,
} }
} }
@ -47,7 +47,7 @@ func NewPetApiWithBasePath(basePath string) *PetApi {
configuration.BasePath = basePath configuration.BasePath = basePath
return &PetApi{ return &PetApi{
Configuration: *configuration, Configuration: configuration,
} }
} }

View File

@ -30,13 +30,13 @@ import (
) )
type StoreApi struct { type StoreApi struct {
Configuration Configuration Configuration *Configuration
} }
func NewStoreApi() *StoreApi { func NewStoreApi() *StoreApi {
configuration := NewConfiguration() configuration := NewConfiguration()
return &StoreApi{ return &StoreApi{
Configuration: *configuration, Configuration: configuration,
} }
} }
@ -45,7 +45,7 @@ func NewStoreApiWithBasePath(basePath string) *StoreApi {
configuration.BasePath = basePath configuration.BasePath = basePath
return &StoreApi{ return &StoreApi{
Configuration: *configuration, Configuration: configuration,
} }
} }

View File

@ -30,13 +30,13 @@ import (
) )
type UserApi struct { type UserApi struct {
Configuration Configuration Configuration *Configuration
} }
func NewUserApi() *UserApi { func NewUserApi() *UserApi {
configuration := NewConfiguration() configuration := NewConfiguration()
return &UserApi{ return &UserApi{
Configuration: *configuration, Configuration: configuration,
} }
} }
@ -45,7 +45,7 @@ func NewUserApiWithBasePath(basePath string) *UserApi {
configuration.BasePath = basePath configuration.BasePath = basePath
return &UserApi{ return &UserApi{
Configuration: *configuration, Configuration: configuration,
} }
} }