2020-08-04 00:51:19 +08:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
2020-08-06 10:35:24 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-05 12:14:49 +08:00
|
|
|
"io"
|
2020-08-06 10:35:24 +08:00
|
|
|
"log"
|
|
|
|
"reflect"
|
2020-08-04 00:51:19 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
RPC = "RPC"
|
|
|
|
ROA = "ROA"
|
|
|
|
|
|
|
|
HTTP = "HTTP"
|
|
|
|
HTTPS = "HTTPS"
|
|
|
|
|
2020-08-06 10:35:24 +08:00
|
|
|
JSON = "JSON"
|
|
|
|
XML = "XML"
|
|
|
|
|
2020-08-04 00:51:19 +08:00
|
|
|
DefaultHttpPort = "80"
|
|
|
|
|
|
|
|
GET = "GET"
|
|
|
|
PUT = "PUT"
|
|
|
|
POST = "POST"
|
|
|
|
DELETE = "DELETE"
|
|
|
|
PATCH = "PATCH"
|
|
|
|
HEAD = "HEAD"
|
|
|
|
OPTIONS = "OPTIONS"
|
|
|
|
|
|
|
|
Json = "application/json"
|
|
|
|
Xml = "application/xml"
|
|
|
|
Raw = "application/octet-stream"
|
|
|
|
Form = "application/x-www-form-urlencoded"
|
|
|
|
|
|
|
|
Header = "Header"
|
|
|
|
Query = "Query"
|
|
|
|
Body = "Body"
|
|
|
|
Path = "Path"
|
|
|
|
|
|
|
|
HeaderSeparator = "\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AcsRequest interface {
|
|
|
|
GetReadTimeout() time.Duration
|
|
|
|
GetConnectTimeout() time.Duration
|
|
|
|
SetReadTimeout(readTimeOut time.Duration)
|
|
|
|
SetConnectTimeout(connectTimeOut time.Duration)
|
|
|
|
SetHTTPSInsecure(isInsecure bool)
|
|
|
|
GetHTTPSInsecure() *bool
|
|
|
|
GetQueryParams() map[string]string
|
|
|
|
GetFormParams() map[string]string
|
|
|
|
GetMethod() string
|
2020-08-04 11:22:37 +08:00
|
|
|
GetScheme() string
|
|
|
|
GetDomain() string
|
2020-08-04 12:23:34 +08:00
|
|
|
GetActionName() string
|
2020-08-05 17:01:10 +08:00
|
|
|
GetAcceptFormat() string
|
2020-08-05 12:14:49 +08:00
|
|
|
GetHeaders() map[string]string
|
2020-08-04 11:22:37 +08:00
|
|
|
|
|
|
|
BuildUrl() string
|
|
|
|
BuildQueries() string
|
|
|
|
|
|
|
|
SetScheme(scheme string)
|
2020-08-06 10:35:24 +08:00
|
|
|
SetContent(content []byte)
|
2020-08-04 11:22:37 +08:00
|
|
|
SetDomain(host string)
|
2020-08-04 00:51:19 +08:00
|
|
|
SetStringToSign(stringToSign string)
|
2020-08-05 12:14:49 +08:00
|
|
|
GetBodyReader() io.Reader
|
|
|
|
|
|
|
|
addHeaderParam(key, value string)
|
|
|
|
addQueryParam(key, value string)
|
|
|
|
addFormParam(key, value string)
|
2020-08-04 00:51:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type baseRequest struct {
|
|
|
|
Scheme string
|
|
|
|
Method string
|
2020-08-04 11:22:37 +08:00
|
|
|
Port string
|
2020-08-04 00:51:19 +08:00
|
|
|
Domain string
|
|
|
|
From string
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
ConnectTimeout time.Duration
|
|
|
|
isInsecure *bool
|
|
|
|
|
|
|
|
AcceptFormat string
|
2020-08-04 12:23:34 +08:00
|
|
|
actionName string
|
2020-08-04 00:51:19 +08:00
|
|
|
|
|
|
|
userAgent map[string]string
|
|
|
|
product string
|
|
|
|
version string
|
|
|
|
|
|
|
|
QueryParams map[string]string
|
|
|
|
Headers map[string]string
|
|
|
|
FormParams map[string]string
|
|
|
|
Content []byte
|
|
|
|
|
|
|
|
queries string
|
|
|
|
stringToSign string
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:35:24 +08:00
|
|
|
func (request *baseRequest) SetContent(content []byte) {
|
|
|
|
request.Content = content
|
|
|
|
}
|
|
|
|
|
2020-08-05 17:01:10 +08:00
|
|
|
func (request *baseRequest) GetAcceptFormat() string {
|
|
|
|
return request.AcceptFormat
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
func (request *baseRequest) GetHeaders() map[string]string {
|
|
|
|
return request.Headers
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:23:34 +08:00
|
|
|
func (request *baseRequest) GetActionName() string {
|
|
|
|
return request.actionName
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:22:37 +08:00
|
|
|
func (request *baseRequest) SetScheme(scheme string) {
|
|
|
|
request.Scheme = scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) SetDomain(host string) {
|
|
|
|
request.Domain = host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) GetScheme() string {
|
|
|
|
return request.Scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) GetDomain() string {
|
|
|
|
return request.Domain
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:51:19 +08:00
|
|
|
func (request *baseRequest) GetMethod() string {
|
|
|
|
return request.Method
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) GetFormParams() map[string]string {
|
|
|
|
return request.FormParams
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) GetQueryParams() map[string]string {
|
|
|
|
return request.QueryParams
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) SetHTTPSInsecure(isInsecure bool) {
|
|
|
|
request.isInsecure = &isInsecure
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) GetHTTPSInsecure() *bool {
|
|
|
|
return request.isInsecure
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) GetReadTimeout() time.Duration {
|
|
|
|
return request.ReadTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) GetConnectTimeout() time.Duration {
|
|
|
|
return request.ConnectTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) SetReadTimeout(readTimeOut time.Duration) {
|
|
|
|
request.ReadTimeout = readTimeOut
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) SetConnectTimeout(connectTimeOut time.Duration) {
|
|
|
|
request.ConnectTimeout = connectTimeOut
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) SetStringToSign(stringToSign string) {
|
|
|
|
request.stringToSign = stringToSign
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
func (request *baseRequest) addHeaderParam(key, val string) {
|
|
|
|
request.Headers[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) addQueryParam(key, val string) {
|
|
|
|
request.QueryParams[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (request *baseRequest) addFormParam(key, val string) {
|
|
|
|
request.FormParams[key] = val
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:51:19 +08:00
|
|
|
func defaultBaseRequest() (request *baseRequest) {
|
|
|
|
request = &baseRequest{
|
2020-08-05 17:01:10 +08:00
|
|
|
Scheme: HTTP,
|
2020-08-06 10:35:24 +08:00
|
|
|
AcceptFormat: JSON,
|
2020-08-04 00:51:19 +08:00
|
|
|
Method: GET,
|
|
|
|
QueryParams: make(map[string]string),
|
|
|
|
Headers: map[string]string{
|
2020-08-06 10:35:24 +08:00
|
|
|
"gr-sdk-client": "golang/1.14",
|
|
|
|
"gr-sdk-invoke-type": "normal",
|
|
|
|
"Accept-Encoding": Json,
|
2020-08-04 00:51:19 +08:00
|
|
|
},
|
|
|
|
FormParams: make(map[string]string),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-08-04 11:22:37 +08:00
|
|
|
|
|
|
|
func InitParam(request AcsRequest) (err error) {
|
2020-08-06 10:35:24 +08:00
|
|
|
reflectValue := reflect.ValueOf(request).Elem()
|
|
|
|
reflectType := reflectValue.Type()
|
|
|
|
for i := 0; i < reflectType.NumField(); i++ {
|
|
|
|
field := reflectType.Field(i)
|
|
|
|
name, isContiansNameTag := field.Tag.Lookup("field")
|
|
|
|
fieldPosition, _ := field.Tag.Lookup("position")
|
|
|
|
log.Println(name, field)
|
|
|
|
if isContiansNameTag {
|
|
|
|
err = addParam(request, fieldPosition, name, reflectValue.Field(i).String())
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 11:22:37 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-06 10:35:24 +08:00
|
|
|
|
|
|
|
func addParam(request AcsRequest, position, key, value string) (err error) {
|
|
|
|
if len(value) > 0 {
|
|
|
|
switch position {
|
|
|
|
case Header:
|
|
|
|
request.addHeaderParam(key, value)
|
|
|
|
case Query:
|
|
|
|
request.addQueryParam(key, value)
|
|
|
|
case Body:
|
|
|
|
request.addFormParam(key, value)
|
|
|
|
default:
|
|
|
|
errmsg := fmt.Sprintf("unsupport positions add param `%s`", position)
|
|
|
|
err = errors.New(errmsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|