Removed fileParams as the file content will be posted via postBody, fixed file upload issue

This commit is contained in:
Guo Huang 2016-04-19 11:46:57 -07:00
parent d0123f40b7
commit fe1afc35e6
7 changed files with 63 additions and 144 deletions

View File

@ -104,6 +104,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
importMapping = new HashMap<String, String>();
importMapping.put("time.Time", "time");
importMapping.put("*os.File", "os");
importMapping.put("ioutil", "io/ioutil");
cliOptions.clear();
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "Go package name (convention: lowercase).")

View File

@ -58,7 +58,6 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
{{#authMethods}}// authentication ({{name}}) required
@ -123,23 +122,24 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{
{{/headerParams}}{{/hasHeaderParams}}
{{#hasFormParams}}
{{#formParams}}
{{#isFile}}
fileParams["{{vendorExtensions.x-exportParamName}}"] = {{paramName}}.Name()
{{#isFile}}fileBytes, _ := ioutil.ReadAll(file)
postBody = fileBytes
{{/isFile}}
{{^isFile}}
formParams["{{vendorExtensions.x-exportParamName}}"] = {{paramName}}
{{/isFile}}
{{/formParams}}
{{/hasFormParams}}
{{#hasBodyParam}}{{#bodyParams}}
{{/hasFormParams}}{{#hasBodyParam}}{{#bodyParams}}
// body params
postBody = &{{paramName}}
{{/bodyParams}}{{/hasBodyParam}}
{{#returnType}} var successPayload = new({{returnType}}){{/returnType}}
{{#returnType}} httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
{{/returnType}}
{{^returnType}} _, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
{{/returnType}}
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
if err != nil {
return {{#returnType}}*successPayload, {{/returnType}}err
}

View File

@ -48,10 +48,9 @@ func (c *ApiClient) CallApi(path string, method string,
postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string,
fileParams map[string]string) (*resty.Response, error) {
formParams map[string]string) (*resty.Response, error) {
request := prepareRequest(postBody, headerParams, queryParams, formParams, fileParams)
request := prepareRequest(postBody, headerParams, queryParams, formParams)
switch strings.ToUpper(method) {
case "GET":
@ -77,7 +76,7 @@ func (c *ApiClient) CallApi(path string, method string,
func (c *ApiClient) ParameterToString (obj interface{}) string {
if reflect.TypeOf(obj).String() == "[]string" {
return strings.Join(obj.([]string), ",")
} else{
} else {
return obj.(string)
}
}
@ -85,8 +84,7 @@ func (c *ApiClient) ParameterToString (obj interface{}) string {
func prepareRequest(postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string,
fileParams map[string]string) *resty.Request {
formParams map[string]string) *resty.Request {
request := resty.R()
@ -102,15 +100,5 @@ func prepareRequest(postBody interface{},
request.SetQueryParams(queryParams)
}
// add form parameter, if any
if len(fileParams) > 0 {
request.SetFormData(formParams)
}
// add file parameter, if any
if len(fileParams) > 0 {
request.SetFiles(fileParams)
}
return request
}

View File

@ -48,10 +48,9 @@ func (c *ApiClient) CallApi(path string, method string,
postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string,
fileParams map[string]string) (*resty.Response, error) {
formParams map[string]string) (*resty.Response, error) {
request := prepareRequest(postBody, headerParams, queryParams, formParams, fileParams)
request := prepareRequest(postBody, headerParams, queryParams, formParams)
switch strings.ToUpper(method) {
case "GET":
@ -77,7 +76,7 @@ func (c *ApiClient) CallApi(path string, method string,
func (c *ApiClient) ParameterToString (obj interface{}) string {
if reflect.TypeOf(obj).String() == "[]string" {
return strings.Join(obj.([]string), ",")
} else{
} else {
return obj.(string)
}
}
@ -85,8 +84,7 @@ func (c *ApiClient) ParameterToString (obj interface{}) string {
func prepareRequest(postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string,
fileParams map[string]string) *resty.Request {
formParams map[string]string) *resty.Request {
request := resty.R()
@ -102,15 +100,5 @@ func prepareRequest(postBody interface{},
request.SetQueryParams(queryParams)
}
// add form parameter, if any
if len(fileParams) > 0 {
request.SetFormData(formParams)
}
// add file parameter, if any
if len(fileParams) > 0 {
request.SetFiles(fileParams)
}
return request
}

View File

@ -7,6 +7,7 @@ import (
"errors"
"bytes"
"os"
"io/ioutil"
)
type PetApi struct {
@ -49,7 +50,6 @@ func (a PetApi) AddPet (body Pet) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (petstore_auth) required
@ -91,10 +91,8 @@ func (a PetApi) AddPet (body Pet) (error) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -123,7 +121,6 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (petstore_auth) required
@ -162,10 +159,8 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error) {
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -192,7 +187,6 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (petstore_auth) required
@ -230,10 +224,8 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) {
var successPayload = new([]Pet)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}
@ -262,7 +254,6 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (petstore_auth) required
@ -300,10 +291,8 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) {
var successPayload = new([]Pet)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}
@ -333,7 +322,6 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (api_key) required
@ -369,10 +357,8 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) {
var successPayload = new(Pet)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}
@ -401,7 +387,6 @@ func (a PetApi) UpdatePet (body Pet) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (petstore_auth) required
@ -443,10 +428,8 @@ func (a PetApi) UpdatePet (body Pet) (error) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -476,7 +459,6 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (petstore_auth) required
@ -512,14 +494,12 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err
headerParams["Accept"] = localVarHttpHeaderAccept
}
formParams["Name"] = name
formParams["Status"] = status
formParams["Name"] = name
formParams["Status"] = status
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -549,7 +529,6 @@ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.Fil
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (petstore_auth) required
@ -584,14 +563,13 @@ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.Fil
headerParams["Accept"] = localVarHttpHeaderAccept
}
formParams["AdditionalMetadata"] = additionalMetadata
fileParams["File"] = file.Name()
formParams["AdditionalMetadata"] = additionalMetadata
fileBytes, _ := ioutil.ReadAll(file)
postBody = fileBytes
var successPayload = new(ApiResponse)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}

View File

@ -49,7 +49,6 @@ func (a StoreApi) DeleteOrder (orderId string) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -80,10 +79,8 @@ func (a StoreApi) DeleteOrder (orderId string) (error) {
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -105,7 +102,6 @@ func (a StoreApi) GetInventory () (map[string]int32, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
// authentication (api_key) required
@ -140,10 +136,8 @@ func (a StoreApi) GetInventory () (map[string]int32, error) {
var successPayload = new(map[string]int32)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}
@ -173,7 +167,6 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -204,10 +197,8 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error) {
var successPayload = new(Order)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}
@ -236,7 +227,6 @@ func (a StoreApi) PlaceOrder (body Order) (Order, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -270,10 +260,8 @@ func (a StoreApi) PlaceOrder (body Order) (Order, error) {
postBody = &body
var successPayload = new(Order)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}

View File

@ -48,7 +48,6 @@ func (a UserApi) CreateUser (body User) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -82,10 +81,8 @@ func (a UserApi) CreateUser (body User) (error) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -112,7 +109,6 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -146,10 +142,8 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -176,7 +170,6 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -210,10 +203,8 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -241,7 +232,6 @@ func (a UserApi) DeleteUser (username string) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -272,10 +262,8 @@ func (a UserApi) DeleteUser (username string) (error) {
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -303,7 +291,6 @@ func (a UserApi) GetUserByName (username string) (User, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -334,10 +321,8 @@ func (a UserApi) GetUserByName (username string) (User, error) {
var successPayload = new(User)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}
@ -371,7 +356,6 @@ func (a UserApi) LoginUser (username string, password string) (string, error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -404,10 +388,8 @@ func (a UserApi) LoginUser (username string, password string) (string, error) {
var successPayload = new(string)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return *successPayload, err
}
@ -431,7 +413,6 @@ func (a UserApi) LogoutUser () (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -462,10 +443,8 @@ func (a UserApi) LogoutUser () (error) {
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}
@ -498,7 +477,6 @@ func (a UserApi) UpdateUser (username string, body User) (error) {
headerParams := make(map[string]string)
queryParams := make(map[string]string)
formParams := make(map[string]string)
fileParams := make(map[string]string)
var postBody interface{}
@ -532,10 +510,8 @@ func (a UserApi) UpdateUser (username string, body User) (error) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams)
if err != nil && httpResponse.StatusCode() != 200{
_, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
if err != nil {
return err
}