mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 03:18:53 +00:00
added CallApiAsync method in api_client
This commit is contained in:
parent
49337a2326
commit
3118f932c8
@ -2,6 +2,8 @@ package {{packageName}}
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"github.com/go-resty/resty"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type ApiClient struct {
|
||||
@ -38,4 +40,59 @@ func contains(source []string, containvalue string) bool {
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
@ -2,6 +2,8 @@ package swagger
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"github.com/go-resty/resty"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type ApiClient struct {
|
||||
@ -38,4 +40,59 @@ func contains(source []string, containvalue string) bool {
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user