Merge pull request #2796 from guohuang/multi

Updat api_client.go to use the new SetMultiValueQueryParams method
This commit is contained in:
wing328 2016-05-16 21:48:01 +08:00
commit 44d64a8793
6 changed files with 110 additions and 49 deletions

View File

@ -5,6 +5,7 @@ import (
"strings"
"fmt"
"errors"
"net/url"
{{#imports}}"{{import}}"
{{/imports}}
)
@ -50,7 +51,7 @@ func (a {{classname}}) {{nickname}}({{#allParams}}{{paramName}} {{{dataType}}}{{
}{{/required}}{{/allParams}}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -75,9 +76,22 @@ func (a {{classname}}) {{nickname}}({{#allParams}}{{paramName}} {{{dataType}}}{{
// add default headers if any
for key := range a.Configuration.DefaultHeader {
headerParams[key] = a.Configuration.DefaultHeader[key]
}{{#hasQueryParams}}{{#queryParams}}
queryParams["{{paramName}}"] = a.Configuration.APIClient.ParameterToString({{paramName}})
}
{{#hasQueryParams}}
{{#queryParams}}
{{#isListContainer}}
var collectionFormat = "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"
if collectionFormat == "multi" {
for _, value := range {{paramName}} {
queryParams.Add("{{paramName}}", value)
}
} else {
queryParams.Add("{{paramName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, collectionFormat))
}
{{/isListContainer}}
{{^isListContainer}}
queryParams.Add("{{paramName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, ""))
{{/isListContainer}}
{{/queryParams}}{{/hasQueryParams}}
// to determine the Content-Type header

View File

@ -6,7 +6,7 @@ import (
"path/filepath"
"reflect"
"strings"
"net/url"
"github.com/go-resty/resty"
)
@ -47,7 +47,7 @@ func contains(source []string, containvalue string) bool {
func (c *APIClient) CallAPI(path string, method string,
postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
queryParams url.Values,
formParams map[string]string,
fileName string,
fileBytes []byte) (*resty.Response, error) {
@ -79,17 +79,26 @@ func (c *APIClient) CallAPI(path string, method string,
return nil, fmt.Errorf("invalid method %v", method)
}
func (c *APIClient) ParameterToString(obj interface{}) string {
func (c *APIClient) ParameterToString(obj interface{},collectionFormat string) string {
if reflect.TypeOf(obj).String() == "[]string" {
return strings.Join(obj.([]string), ",")
} else {
return obj.(string)
switch collectionFormat {
case "pipes":
return strings.Join(obj.([]string), "|")
case "ssv":
return strings.Join(obj.([]string), " ")
case "tsv":
return strings.Join(obj.([]string), "\t")
case "csv" :
return strings.Join(obj.([]string), ",")
}
}
return obj.(string)
}
func prepareRequest(postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
queryParams url.Values,
formParams map[string]string,
fileName string,
fileBytes []byte) *resty.Request {
@ -104,7 +113,7 @@ func prepareRequest(postBody interface{},
// add query parameter, if any
if len(queryParams) > 0 {
request.SetQueryParams(queryParams)
request.SetMultiValueQueryParams(queryParams)
}
// add form parameter, if any

View File

@ -6,7 +6,7 @@ import (
"path/filepath"
"reflect"
"strings"
"net/url"
"github.com/go-resty/resty"
)
@ -47,7 +47,7 @@ func contains(source []string, containvalue string) bool {
func (c *APIClient) CallAPI(path string, method string,
postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
queryParams url.Values,
formParams map[string]string,
fileName string,
fileBytes []byte) (*resty.Response, error) {
@ -79,17 +79,26 @@ func (c *APIClient) CallAPI(path string, method string,
return nil, fmt.Errorf("invalid method %v", method)
}
func (c *APIClient) ParameterToString(obj interface{}) string {
func (c *APIClient) ParameterToString(obj interface{},collectionFormat string) string {
if reflect.TypeOf(obj).String() == "[]string" {
return strings.Join(obj.([]string), ",")
} else {
return obj.(string)
switch collectionFormat {
case "pipes":
return strings.Join(obj.([]string), "|")
case "ssv":
return strings.Join(obj.([]string), " ")
case "tsv":
return strings.Join(obj.([]string), "\t")
case "csv" :
return strings.Join(obj.([]string), ",")
}
}
return obj.(string)
}
func prepareRequest(postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
queryParams url.Values,
formParams map[string]string,
fileName string,
fileBytes []byte) *resty.Request {
@ -104,7 +113,7 @@ func prepareRequest(postBody interface{},
// add query parameter, if any
if len(queryParams) > 0 {
request.SetQueryParams(queryParams)
request.SetMultiValueQueryParams(queryParams)
}
// add form parameter, if any

View File

@ -4,6 +4,7 @@ import (
"strings"
"fmt"
"errors"
"net/url"
"os"
"io/ioutil"
"encoding/json"
@ -48,7 +49,7 @@ func (a PetApi) AddPet(body Pet) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -64,6 +65,7 @@ func (a PetApi) AddPet(body Pet) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ "application/json", "application/xml", }
@ -116,7 +118,7 @@ func (a PetApi) DeletePet(petId int64, apiKey string) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -132,6 +134,7 @@ func (a PetApi) DeletePet(petId int64, apiKey string) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -183,7 +186,7 @@ func (a PetApi) FindPetsByStatus(status []string) ([]Pet, *APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -198,8 +201,14 @@ func (a PetApi) FindPetsByStatus(status []string) ([]Pet, *APIResponse, error) {
for key := range a.Configuration.DefaultHeader {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
queryParams["status"] = a.Configuration.APIClient.ParameterToString(status)
var collectionFormat = "csv"
if collectionFormat == "multi" {
for _, value := range status {
queryParams.Add("status", value)
}
} else {
queryParams.Add("status", a.Configuration.APIClient.ParameterToString(status, collectionFormat))
}
// to determine the Content-Type header
@ -249,7 +258,7 @@ func (a PetApi) FindPetsByTags(tags []string) ([]Pet, *APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -264,8 +273,14 @@ func (a PetApi) FindPetsByTags(tags []string) ([]Pet, *APIResponse, error) {
for key := range a.Configuration.DefaultHeader {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
queryParams["tags"] = a.Configuration.APIClient.ParameterToString(tags)
var collectionFormat = "csv"
if collectionFormat == "multi" {
for _, value := range tags {
queryParams.Add("tags", value)
}
} else {
queryParams.Add("tags", a.Configuration.APIClient.ParameterToString(tags, collectionFormat))
}
// to determine the Content-Type header
@ -316,7 +331,7 @@ func (a PetApi) GetPetById(petId int64) (*Pet, *APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -331,6 +346,7 @@ func (a PetApi) GetPetById(petId int64) (*Pet, *APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -378,7 +394,7 @@ func (a PetApi) UpdatePet(body Pet) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -394,6 +410,7 @@ func (a PetApi) UpdatePet(body Pet) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ "application/json", "application/xml", }
@ -447,7 +464,7 @@ func (a PetApi) UpdatePetWithForm(petId int64, name string, status string) (*API
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -463,6 +480,7 @@ func (a PetApi) UpdatePetWithForm(petId int64, name string, status string) (*API
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ "application/x-www-form-urlencoded", }
@ -516,7 +534,7 @@ func (a PetApi) UploadFile(petId int64, additionalMetadata string, file *os.File
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -532,6 +550,7 @@ func (a PetApi) UploadFile(petId int64, additionalMetadata string, file *os.File
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ "multipart/form-data", }

View File

@ -4,6 +4,7 @@ import (
"strings"
"fmt"
"errors"
"net/url"
"encoding/json"
)
@ -47,7 +48,7 @@ func (a StoreApi) DeleteOrder(orderId string) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -58,6 +59,7 @@ func (a StoreApi) DeleteOrder(orderId string) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -100,7 +102,7 @@ func (a StoreApi) GetInventory() (*map[string]int32, *APIResponse, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -115,6 +117,7 @@ func (a StoreApi) GetInventory() (*map[string]int32, *APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -162,7 +165,7 @@ func (a StoreApi) GetOrderById(orderId int64) (*Order, *APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -173,6 +176,7 @@ func (a StoreApi) GetOrderById(orderId int64) (*Order, *APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -220,7 +224,7 @@ func (a StoreApi) PlaceOrder(body Order) (*Order, *APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -231,6 +235,7 @@ func (a StoreApi) PlaceOrder(body Order) (*Order, *APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }

View File

@ -4,6 +4,7 @@ import (
"strings"
"fmt"
"errors"
"net/url"
"encoding/json"
)
@ -46,7 +47,7 @@ func (a UserApi) CreateUser(body User) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -57,6 +58,7 @@ func (a UserApi) CreateUser(body User) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -107,7 +109,7 @@ func (a UserApi) CreateUsersWithArrayInput(body []User) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -118,6 +120,7 @@ func (a UserApi) CreateUsersWithArrayInput(body []User) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -168,7 +171,7 @@ func (a UserApi) CreateUsersWithListInput(body []User) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -179,6 +182,7 @@ func (a UserApi) CreateUsersWithListInput(body []User) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -230,7 +234,7 @@ func (a UserApi) DeleteUser(username string) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -241,6 +245,7 @@ func (a UserApi) DeleteUser(username string) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -289,7 +294,7 @@ func (a UserApi) GetUserByName(username string) (*User, *APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -300,6 +305,7 @@ func (a UserApi) GetUserByName(username string) (*User, *APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -352,7 +358,7 @@ func (a UserApi) LoginUser(username string, password string) (*string, *APIRespo
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -362,11 +368,8 @@ func (a UserApi) LoginUser(username string, password string) (*string, *APIRespo
for key := range a.Configuration.DefaultHeader {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
queryParams["username"] = a.Configuration.APIClient.ParameterToString(username)
queryParams["password"] = a.Configuration.APIClient.ParameterToString(password)
queryParams.Add("username", a.Configuration.APIClient.ParameterToString(username, ""))
queryParams.Add("password", a.Configuration.APIClient.ParameterToString(password, ""))
// to determine the Content-Type header
@ -411,7 +414,7 @@ func (a UserApi) LogoutUser() (*APIResponse, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -422,6 +425,7 @@ func (a UserApi) LogoutUser() (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -475,7 +479,7 @@ func (a UserApi) UpdateUser(username string, body User) (*APIResponse, error) {
}
headerParams := make(map[string]string)
queryParams := make(map[string]string)
queryParams := url.Values{}
formParams := make(map[string]string)
var postBody interface{}
var fileName string
@ -486,6 +490,7 @@ func (a UserApi) UpdateUser(username string, body User) (*APIResponse, error) {
headerParams[key] = a.Configuration.DefaultHeader[key]
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }