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

72 lines
1.6 KiB
Go
Raw Permalink 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.baseRequest.AddHeaderParam("Content-Type", Json)
2024-05-30 18:40:57 +08:00
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 STREAM
2024-05-30 18:40:57 +08:00
}
func (request *JsonRequest) BuildQueries() string {
path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/")
2024-10-12 15:27:57 +08:00
mod := "&"
if !strings.Contains(path, "?") {
mod = "?"
}
request.queries = "/" + path + mod + utils.GetUrlFormedMap(request.QueryParams)
2024-05-30 18:40:57 +08:00
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.JsonParams != nil && len(request.JsonParams) > 0 {
body, err := json.Marshal(request.JsonParams)
2024-05-30 18:40:57 +08:00
if err == nil {
return bytes.NewReader(body)
}
}
return strings.NewReader("")
}