added CallApiAsync method in api_client

This commit is contained in:
Guo Huang 2016-04-16 22:58:13 -07:00
parent 49337a2326
commit 3118f932c8
2 changed files with 114 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package {{packageName}}
import ( import (
"strings" "strings"
"github.com/go-resty/resty"
"errors"
) )
type ApiClient struct { type ApiClient struct {
@ -38,4 +40,59 @@ func contains(source []string, containvalue string) bool {
} }
} }
return false return false
}
func CallApiAsync(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) {
request := prepareRequest(postBody, headerParams, queryParams, formParams, fileParams)
switch strings.ToUpper(method) {
case "GET":
response, err := request.Get(path)
return response, err
case "POST":
response, err := request.Post(path)
return response, err
case "PUT":
response, err := request.Put(path)
return response, err
case "PATCH":
response, err := request.Patch(path)
return response, err
case "DELETE":
response, err := request.Delete(path)
return response, err
}
return nil, errors.New("Invalid method " + method)
}
func prepareRequest(postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string,
fileParams map[string]string) *resty.Request {
request := resty.R()
request.SetBody(postBody)
// add header parameter, if any
request.SetHeaders(headerParams)
// add query parameter, if any
request.SetQueryParams(queryParams)
// add form parameter, if any
request.SetFormData(formParams)
// add file parameter, if any
request.SetFiles(fileParams)
return request
} }

View File

@ -2,6 +2,8 @@ package swagger
import ( import (
"strings" "strings"
"github.com/go-resty/resty"
"errors"
) )
type ApiClient struct { type ApiClient struct {
@ -38,4 +40,59 @@ func contains(source []string, containvalue string) bool {
} }
} }
return false return false
}
func CallApiAsync(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) {
request := prepareRequest(postBody, headerParams, queryParams, formParams, fileParams)
switch strings.ToUpper(method) {
case "GET":
response, err := request.Get(path)
return response, err
case "POST":
response, err := request.Post(path)
return response, err
case "PUT":
response, err := request.Put(path)
return response, err
case "PATCH":
response, err := request.Patch(path)
return response, err
case "DELETE":
response, err := request.Delete(path)
return response, err
}
return nil, errors.New("Invalid method " + method)
}
func prepareRequest(postBody interface{},
headerParams map[string]string,
queryParams map[string]string,
formParams map[string]string,
fileParams map[string]string) *resty.Request {
request := resty.R()
request.SetBody(postBody)
// add header parameter, if any
request.SetHeaders(headerParams)
// add query parameter, if any
request.SetQueryParams(queryParams)
// add form parameter, if any
request.SetFormData(formParams)
// add file parameter, if any
request.SetFiles(fileParams)
return request
} }