7
0
gaore-common-sdk-go/sdk/requests/rpc_request.go

65 lines
1.6 KiB
Go
Raw Normal View History

2020-08-04 00:51:19 +08:00
package requests
2020-08-05 12:14:49 +08:00
import (
2020-08-05 17:01:10 +08:00
"fmt"
2020-08-05 12:14:49 +08:00
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"io"
"strings"
)
2020-08-06 12:15:51 +08:00
type RpcRequest struct {
2020-08-04 00:51:19 +08:00
*baseRequest
}
2020-08-04 11:22:37 +08:00
2020-08-06 12:15:51 +08:00
func (request *RpcRequest) init() {
2020-08-04 12:23:34 +08:00
request.baseRequest = defaultBaseRequest()
request.baseRequest.AddHeaderParam("Content-Type", Form)
2020-08-04 12:23:34 +08:00
request.Method = POST
}
2020-08-06 12:15:51 +08:00
func (request *RpcRequest) BuildUrl() string {
var hostname string
if request.Domain.Func == nil {
hostname = request.Domain.Default
} else if hostname = request.Domain.Func(request.GetEnv()); hostname == "" {
hostname = request.Domain.Default
}
url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), hostname)
2020-08-05 17:01:10 +08:00
if len(request.Port) > 0 {
url = fmt.Sprintf("%s:%s", url, request.Port)
}
return url + request.BuildQueries()
2020-08-04 11:22:37 +08:00
}
func (request *RpcRequest) GetStyle() string {
return RPC
}
2020-08-06 12:15:51 +08:00
func (request *RpcRequest) BuildQueries() string {
2020-08-05 17:01:10 +08:00
path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/")
request.queries = "/" + path + "?" + utils.GetUrlFormedMap(request.QueryParams)
return request.queries
2020-08-04 11:22:37 +08:00
}
2020-08-04 12:23:34 +08:00
2020-08-06 12:15:51 +08:00
func (request *RpcRequest) GetActionName() string {
2020-08-04 12:23:34 +08:00
return request.actionName
}
func (request *RpcRequest) InitWithApiInfo(domain Host, version, urlPath string) {
2020-08-04 12:23:34 +08:00
request.init()
2020-08-05 17:01:10 +08:00
request.SetDomain(domain)
2020-08-04 12:23:34 +08:00
request.version = version
2020-08-05 17:01:10 +08:00
request.actionName = urlPath
2020-08-04 12:23:34 +08:00
}
2020-08-05 12:14:49 +08:00
2020-08-06 12:15:51 +08:00
func (request *RpcRequest) GetBodyReader() io.Reader {
2020-08-05 12:14:49 +08:00
if request.FormParams != nil && len(request.FormParams) > 0 {
formString := utils.GetUrlFormedMap(request.FormParams)
return strings.NewReader(formString)
} else {
return strings.NewReader("")
}
}