123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package requests
- import (
- "bytes"
- "fmt"
- "io"
- "net/url"
- "sort"
- "strings"
- "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
- )
- type RoaRequest struct {
- *baseRequest
- pathPattern string
- PathParams map[string]string
- }
- func (*RoaRequest) GetStyle() string {
- return ROA
- }
- func (request *RoaRequest) GetBodyReader() io.Reader {
- if request.FormParams != nil && len(request.FormParams) > 0 {
- formString := utils.GetUrlFormedMap(request.FormParams)
- return strings.NewReader(formString)
- } else if len(request.Content) > 0 {
- return bytes.NewReader(request.Content)
- } else {
- return nil
- }
- }
- func (request *RoaRequest) BuildQueries() string {
- return request.buildQueries()
- }
- func (request *RoaRequest) buildPath() string {
- path := request.pathPattern
- for key, value := range request.PathParams {
- path = strings.Replace(path, "["+key+"]", value, 1)
- }
- return path
- }
- func (request *RoaRequest) buildQueries() string {
-
- path := request.buildPath()
- queryParams := request.QueryParams
-
- var queryKeys []string
- for key := range queryParams {
- queryKeys = append(queryKeys, key)
- }
- sort.Strings(queryKeys)
-
- urlBuilder := bytes.Buffer{}
- urlBuilder.WriteString(path)
- if len(queryKeys) > 0 {
- urlBuilder.WriteString("?")
- }
- for i := 0; i < len(queryKeys); i++ {
- queryKey := queryKeys[i]
- urlBuilder.WriteString(queryKey)
- if value := queryParams[queryKey]; len(value) > 0 {
- urlBuilder.WriteString("=")
- urlBuilder.WriteString(value)
- }
- if i < len(queryKeys)-1 {
- urlBuilder.WriteString("&")
- }
- }
- result := urlBuilder.String()
- result = popStandardUrlencode(result)
- return result
- }
- func (request *RoaRequest) buildQueryString() string {
- queryParams := request.QueryParams
-
- q := url.Values{}
- for key, value := range queryParams {
- q.Add(key, value)
- }
- return q.Encode()
- }
- func popStandardUrlencode(stringToSign string) (result string) {
- result = strings.Replace(stringToSign, "+", "%20", -1)
- result = strings.Replace(result, "*", "%2A", -1)
- result = strings.Replace(result, "%7E", "~", -1)
- return
- }
- func (request *RoaRequest) BuildUrl() string {
-
- scheme := strings.ToLower(request.Scheme)
- domain := request.Domain
- port := request.Port
- path := request.buildPath()
- url := fmt.Sprintf("%s://%s:%s%s", scheme, domain, port, path)
- querystring := request.buildQueryString()
- if len(querystring) > 0 {
- url = fmt.Sprintf("%s?%s", url, querystring)
- }
- return url
- }
- func (request *RoaRequest) addPathParam(key, value string) {
- request.PathParams[key] = value
- }
- func (request *RoaRequest) InitWithApiInfo(product, version, action, uriPattern, serviceCode, endpointType string) {
- request.baseRequest = defaultBaseRequest()
- request.PathParams = make(map[string]string)
- request.Headers["x-acs-version"] = version
- request.pathPattern = uriPattern
- request.locationServiceCode = serviceCode
- request.locationEndpointType = endpointType
- request.product = product
-
- request.actionName = action
- }
- func (request *RoaRequest) initWithCommonRequest(commonRequest *CommonRequest) {
- request.baseRequest = commonRequest.baseRequest
- request.PathParams = commonRequest.PathParams
- request.product = commonRequest.Product
-
- request.Headers["x-acs-version"] = commonRequest.Version
- request.actionName = commonRequest.ApiName
- request.pathPattern = commonRequest.PathPattern
- request.locationServiceCode = commonRequest.ServiceCode
- request.locationEndpointType = ""
- }
|