From af19a70f723a551778e86e6ab5d1697f45698f54 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Mon, 18 Apr 2016 17:09:47 -0700 Subject: [PATCH] updated api_client.go to ensure there is value before setting the request --- .../src/main/resources/go/api_client.mustache | 25 ++++++++++++------- .../petstore/go/go-petstore/api_client.go | 25 ++++++++++++------- .../client/petstore/go/go-petstore/pet_api.go | 10 +------- .../petstore/go/go-petstore/store_api.go | 4 --- .../petstore/go/go-petstore/user_api.go | 8 ------ 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/go/api_client.mustache b/modules/swagger-codegen/src/main/resources/go/api_client.mustache index 5ff56a31cfc..a4f06b30828 100644 --- a/modules/swagger-codegen/src/main/resources/go/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api_client.mustache @@ -42,16 +42,17 @@ func contains(source []string, containvalue string) bool { return false } + 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) { - verb := strings.ToUpper(method) - request := prepareRequest(verb, postBody, headerParams, queryParams, formParams, fileParams) - switch verb { + request := prepareRequest(postBody, headerParams, queryParams, formParams, fileParams) + + switch strings.ToUpper(method) { case "GET": response, err := request.Get(path) return response, err @@ -72,7 +73,7 @@ func (c *ApiClient) CallApi(path string, method string, return nil, errors.New("Invalid method " + method) } -func prepareRequest(verb string, postBody interface{}, +func prepareRequest(postBody interface{}, headerParams map[string]string, queryParams map[string]string, formParams map[string]string, @@ -83,16 +84,22 @@ func prepareRequest(verb string, postBody interface{}, request.SetBody(postBody) // add header parameter, if any - request.SetHeaders(headerParams) - + if len(headerParams) > 0 { + request.SetHeaders(headerParams) + } + // add query parameter, if any - request.SetQueryParams(queryParams) + if len(queryParams) > 0 { + request.SetQueryParams(queryParams) + } // add form parameter, if any - request.SetFormData(formParams) + if len(fileParams) > 0 { + request.SetFormData(formParams) + } // add file parameter, if any - if verb != "GET"{ + if len(fileParams) > 0 { request.SetFiles(fileParams) } diff --git a/samples/client/petstore/go/go-petstore/api_client.go b/samples/client/petstore/go/go-petstore/api_client.go index f178cbddcff..e8bc409811d 100644 --- a/samples/client/petstore/go/go-petstore/api_client.go +++ b/samples/client/petstore/go/go-petstore/api_client.go @@ -42,16 +42,17 @@ func contains(source []string, containvalue string) bool { return false } + 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) { - verb := strings.ToUpper(method) - request := prepareRequest(verb, postBody, headerParams, queryParams, formParams, fileParams) - switch verb { + request := prepareRequest(postBody, headerParams, queryParams, formParams, fileParams) + + switch strings.ToUpper(method) { case "GET": response, err := request.Get(path) return response, err @@ -72,7 +73,7 @@ func (c *ApiClient) CallApi(path string, method string, return nil, errors.New("Invalid method " + method) } -func prepareRequest(verb string, postBody interface{}, +func prepareRequest(postBody interface{}, headerParams map[string]string, queryParams map[string]string, formParams map[string]string, @@ -83,16 +84,22 @@ func prepareRequest(verb string, postBody interface{}, request.SetBody(postBody) // add header parameter, if any - request.SetHeaders(headerParams) - + if len(headerParams) > 0 { + request.SetHeaders(headerParams) + } + // add query parameter, if any - request.SetQueryParams(queryParams) + if len(queryParams) > 0 { + request.SetQueryParams(queryParams) + } // add form parameter, if any - request.SetFormData(formParams) + if len(fileParams) > 0 { + request.SetFormData(formParams) + } // add file parameter, if any - if verb != "GET"{ + if len(fileParams) > 0 { request.SetFiles(fileParams) } diff --git a/samples/client/petstore/go/go-petstore/pet_api.go b/samples/client/petstore/go/go-petstore/pet_api.go index af0c05e45b7..9b0debc95ac 100644 --- a/samples/client/petstore/go/go-petstore/pet_api.go +++ b/samples/client/petstore/go/go-petstore/pet_api.go @@ -75,7 +75,6 @@ func (a PetApi) AddPet (body Pet) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -95,7 +94,7 @@ func (a PetApi) AddPet (body Pet) (error) { httpResponse, err := a.Configuration.ApiClient.CallApi(path, httpMethod, postBody, headerParams, queryParams, formParams, fileParams) - if err != nil && httpResponse.StatusCode() != 0{ + if err != nil && httpResponse.StatusCode() != 200{ return err } @@ -148,7 +147,6 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -219,7 +217,6 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -290,7 +287,6 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -360,7 +356,6 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -432,7 +427,6 @@ func (a PetApi) UpdatePet (body Pet) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -507,7 +501,6 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -581,7 +574,6 @@ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.Fil if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/json", diff --git a/samples/client/petstore/go/go-petstore/store_api.go b/samples/client/petstore/go/go-petstore/store_api.go index 0166b084f79..5f4694cd9db 100644 --- a/samples/client/petstore/go/go-petstore/store_api.go +++ b/samples/client/petstore/go/go-petstore/store_api.go @@ -67,7 +67,6 @@ func (a StoreApi) DeleteOrder (orderId string) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -129,7 +128,6 @@ func (a StoreApi) GetInventory () (map[string]int32, error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/json", @@ -193,7 +191,6 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -257,7 +254,6 @@ func (a StoreApi) PlaceOrder (body Order) (Order, error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", diff --git a/samples/client/petstore/go/go-petstore/user_api.go b/samples/client/petstore/go/go-petstore/user_api.go index 6729a5cd265..a53788439f1 100644 --- a/samples/client/petstore/go/go-petstore/user_api.go +++ b/samples/client/petstore/go/go-petstore/user_api.go @@ -66,7 +66,6 @@ func (a UserApi) CreateUser (body User) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -131,7 +130,6 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -196,7 +194,6 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -262,7 +259,6 @@ func (a UserApi) DeleteUser (username string) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -325,7 +321,6 @@ func (a UserApi) GetUserByName (username string) (User, error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -396,7 +391,6 @@ func (a UserApi) LoginUser (username string, password string) (string, error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -455,7 +449,6 @@ func (a UserApi) LogoutUser () (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml", @@ -523,7 +516,6 @@ func (a UserApi) UpdateUser (username string, body User) (error) { if localVarHttpContentType != "" { headerParams["Content-Type"] = localVarHttpContentType } - // to determine the Accept header localVarHttpHeaderAccepts := []string { "application/xml",