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

67 lines
1.5 KiB
Go
Raw Normal View History

2024-05-30 18:40:57 +08:00
package requests
import (
"bytes"
"encoding/json"
"fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"io"
"strings"
)
type JsonRequest struct {
*baseRequest
}
func (request *JsonRequest) init() {
request.baseRequest = defaultBaseRequest()
request.Method = POST
}
func (request *JsonRequest) 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)
if len(request.Port) > 0 {
url = fmt.Sprintf("%s:%s", url, request.Port)
}
return url + request.BuildQueries()
}
func (request *JsonRequest) GetStyle() string {
return RPC
}
func (request *JsonRequest) BuildQueries() string {
path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/")
request.queries = "/" + path + "?" + utils.GetUrlFormedMap(request.QueryParams)
return request.queries
}
func (request *JsonRequest) GetActionName() string {
return request.actionName
}
func (request *JsonRequest) InitWithApiInfo(domain Host, version, urlPath string) {
request.init()
request.SetDomain(domain)
request.version = version
request.actionName = urlPath
}
func (request *JsonRequest) GetBodyReader() io.Reader {
if request.FormParams != nil && len(request.FormParams) > 0 {
body, err := json.Marshal(request.FormParams)
if err == nil {
return bytes.NewReader(body)
}
}
return strings.NewReader("")
}