Merge branch 'issue-3461-configurable-transport' of https://github.com/neilotoole/swagger-codegen into neilotoole-issue-3461-configurable-transport

This commit is contained in:
wing328 2016-08-17 22:52:44 +08:00
commit 661c911d27
3 changed files with 46 additions and 27 deletions

View File

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

View File

@ -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)

View File

@ -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
}