fixed file upload issue

This commit is contained in:
Guo Huang
2016-04-20 13:49:02 -07:00
parent 10c7c41e82
commit 7636a772c6
6 changed files with 66 additions and 33 deletions

View File

@@ -67,6 +67,7 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
{{#authMethods}}// authentication ({{name}}) required
{{#isApiKey}}{{#isKeyInHeader}}
@@ -130,8 +131,8 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{
{{/headerParams}}{{/hasHeaderParams}}
{{#hasFormParams}}
{{#formParams}}
{{#isFile}}fileBytes, _ := ioutil.ReadAll(file)
postBody = fileBytes
{{#isFile}}fbs, _ := ioutil.ReadAll(file)
fileBytes = fbs
{{/isFile}}
{{^isFile}}formParams["{{paramName}}"] = {{paramName}}
{{/isFile}}
@@ -140,7 +141,7 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{
postBody = &{{paramName}}
{{/bodyParams}}{{/hasBodyParam}}
{{#returnType}} var successPayload = new({{returnType}}){{/returnType}}
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {

View File

@@ -5,6 +5,7 @@ import (
"github.com/go-resty/resty"
"errors"
"reflect"
"bytes"
)
type ApiClient struct {
@@ -48,13 +49,14 @@ func (c *ApiClient) CallApi(path string, method string,
postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string) (*resty.Response, error) {
formParams map[string]string,
file []byte) (*resty.Response, error) {
//set debug flag
configuration := NewConfiguration()
resty.SetDebug(configuration.Debug)
request := prepareRequest(postBody, headerParams, queryParams, formParams)
request := prepareRequest(postBody, headerParams, queryParams, formParams,file)
switch strings.ToUpper(method) {
case "GET":
@@ -106,7 +108,8 @@ func (c *ApiClient) SetErrorApiResponse(errorMessage string) *ApiResponse{
func prepareRequest(postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string) *resty.Request {
formParams map[string]string,
file []byte) *resty.Request {
request := resty.R()
@@ -127,5 +130,8 @@ func prepareRequest(postBody interface{},
request.SetFormData(formParams)
}
if len(file) > 0 {
request.SetFileReader("file", "test-img.png", bytes.NewReader(file))
}
return request
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/go-resty/resty"
"errors"
"reflect"
"bytes"
)
type ApiClient struct {
@@ -48,13 +49,14 @@ func (c *ApiClient) CallApi(path string, method string,
postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string) (*resty.Response, error) {
formParams map[string]string,
file []byte) (*resty.Response, error) {
//set debug flag
configuration := NewConfiguration()
resty.SetDebug(configuration.Debug)
request := prepareRequest(postBody, headerParams, queryParams, formParams)
request := prepareRequest(postBody, headerParams, queryParams, formParams,file)
switch strings.ToUpper(method) {
case "GET":
@@ -106,7 +108,8 @@ func (c *ApiClient) SetErrorApiResponse(errorMessage string) *ApiResponse{
func prepareRequest(postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string) *resty.Request {
formParams map[string]string,
file []byte) *resty.Request {
request := resty.R()
@@ -127,5 +130,8 @@ func prepareRequest(postBody interface{},
request.SetFormData(formParams)
}
if len(file) > 0 {
request.SetFileReader("file", "test-img.png", bytes.NewReader(file))
}
return request
}

View File

@@ -50,6 +50,7 @@ func (a PetApi) AddPet (body Pet) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (petstore_auth) required
@@ -89,7 +90,7 @@ func (a PetApi) AddPet (body Pet) (error, ApiResponse) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -121,6 +122,7 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (petstore_auth) required
@@ -158,7 +160,7 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error, ApiResponse) {
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -188,6 +190,7 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (petstore_auth) required
@@ -224,7 +227,7 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error, ApiResponse) {
var successPayload = new([]Pet)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -256,6 +259,7 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (petstore_auth) required
@@ -292,7 +296,7 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error, ApiResponse) {
var successPayload = new([]Pet)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -325,6 +329,7 @@ func (a PetApi) GetPetById (petId int64) (Pet, error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (api_key) required
@@ -359,7 +364,7 @@ func (a PetApi) GetPetById (petId int64) (Pet, error, ApiResponse) {
var successPayload = new(Pet)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -391,6 +396,7 @@ func (a PetApi) UpdatePet (body Pet) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (petstore_auth) required
@@ -430,7 +436,7 @@ func (a PetApi) UpdatePet (body Pet) (error, ApiResponse) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -463,6 +469,7 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (petstore_auth) required
@@ -501,7 +508,7 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err
formParams["status"] = status
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -534,6 +541,7 @@ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.Fil
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (petstore_auth) required
@@ -568,11 +576,11 @@ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.Fil
}
formParams["additionalMetadata"] = additionalMetadata
fileBytes, _ := ioutil.ReadAll(file)
postBody = fileBytes
fbs, _ := ioutil.ReadAll(file)
fileBytes = fbs
var successPayload = new(ApiResponse)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {

View File

@@ -49,6 +49,7 @@ func (a StoreApi) DeleteOrder (orderId string) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -78,7 +79,7 @@ func (a StoreApi) DeleteOrder (orderId string) (error, ApiResponse) {
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -103,6 +104,7 @@ func (a StoreApi) GetInventory () (map[string]int32, error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// authentication (api_key) required
@@ -136,7 +138,7 @@ func (a StoreApi) GetInventory () (map[string]int32, error, ApiResponse) {
var successPayload = new(map[string]int32)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -169,6 +171,7 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -198,7 +201,7 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error, ApiResponse) {
var successPayload = new(Order)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -230,6 +233,7 @@ func (a StoreApi) PlaceOrder (body Order) (Order, error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -261,7 +265,7 @@ func (a StoreApi) PlaceOrder (body Order) (Order, error, ApiResponse) {
postBody = &body
var successPayload = new(Order)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {

View File

@@ -48,6 +48,7 @@ func (a UserApi) CreateUser (body User) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -79,7 +80,7 @@ func (a UserApi) CreateUser (body User) (error, ApiResponse) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -109,6 +110,7 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -140,7 +142,7 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error, ApiResponse) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -170,6 +172,7 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -201,7 +204,7 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error, ApiResponse) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -232,6 +235,7 @@ func (a UserApi) DeleteUser (username string) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -261,7 +265,7 @@ func (a UserApi) DeleteUser (username string) (error, ApiResponse) {
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -292,6 +296,7 @@ func (a UserApi) GetUserByName (username string) (User, error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -321,7 +326,7 @@ func (a UserApi) GetUserByName (username string) (User, error, ApiResponse) {
var successPayload = new(User)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -358,6 +363,7 @@ func (a UserApi) LoginUser (username string, password string) (string, error, Ap
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -389,7 +395,7 @@ func (a UserApi) LoginUser (username string, password string) (string, error, Ap
var successPayload = new(string)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -416,6 +422,7 @@ func (a UserApi) LogoutUser () (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -445,7 +452,7 @@ func (a UserApi) LogoutUser () (error, ApiResponse) {
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {
@@ -481,6 +488,7 @@ func (a UserApi) UpdateUser (username string, body User) (error, ApiResponse) {
queryParams := make(map[string]string)
formParams := make(map[string]string)
var postBody interface{}
var fileBytes []byte
// add default headers if any
@@ -512,7 +520,7 @@ func (a UserApi) UpdateUser (username string, body User) (error, ApiResponse) {
postBody = &body
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams)
httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileBytes)
if err != nil {